c Practical Slips
c Practical Slips
C practical slips
Slip 1
1 Write a C program to calculate area and perimeter of a
rectangle. [5 marks]
Ans-
#include <stdio.h>
int main()
{
int l = 10, b = 10, area, peri;
area=l*b;
peri=2 * (l + b);
printf("Area of rectangle is : %d", area);
printf("\n Perimeter of rectangle is : %d", peri);
return 0;
}
2 Write a C program to calculate the sum of factors of a
number. [10 marks]
Ans-
#include<stdio.h>
int main()
{
int no, sum=0, i;
printf("Enter any number :");
scanf("%d", &no);
for( i=1; i<=no; i++)
{
if(no%i==0)
sum=sum+i;
}
printf("\nSum of the factors of is: %d", sum);
return 0;
}
*********************************************************
Slip 2
• Write a C program to accept a character and check if it
is uppercase or lowercase. [5 marks]
Ans-
#include<stdio.h>
int main()
{
char ch;
printf("Enter any one alphabet/ charactor:");
scanf("%c",&ch);
if(ch>=65 && ch<=90)
printf("Given charactr is upper case");
else if(ch>=97 && ch<=122)
printf(" Given charactr is lower case");
else
printf("Invalid input");
return 0;
}
• Write a C program to display n terms of the Fibonacci
series. [10 marks]
Ans-
#include<stdio.h>
int main()
{
int num1=0, num2=1, result,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are
already printed
{
result=num1+num2;
printf(" %d",result);
num1=num2;
num2=result;
}
return 0;
}
*********************************************************
Slip 3
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("Please enter correct input \n");
break; } }
*********************************************************
Slip 4
return 0;
}
**********************************************************
Slip 5
int year;
clrscr();
printf("Enter any year, for ex. 2004: ”);
scanf(“%d ”, &year);
if (year %4==0 && year %400 ==0 || year % 100 !=0)
printf("year %d is leap year”, year);
else
printf("year %d is NOT leap year”, year);
getch();
return 0;
}
*********************************************************
Slip 6
#include <stdio.h>
#include <conio.h>
int main()
{
int a, b;
clrscr();
printf("Enter any two numbers: ”);
scanf(“%d %d”, &a, &b);
if(a > b)
printf("a = %d is greater than b= %d”, a,b);
else
printf("b = %d is greater than a= %d”, b, a);
getch();
return 0;
}
*********************************************************
Slip 7
*********************************************************
Slip 8
**********************************************************
Slip 9
• Write a C program to interchange two numbers and
**********************************************************
Slip 10
return 0;
}
*********************************************************
Slip 11
i.Write a C program to find whether a given year is a leap
year or not. [5 marks]
#include<stdio.h>
#include<conio.h>
void main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if(((year%4==0) && ((year%400==0) || (year%100!==0))
{
printf("%d is a leap year", &year);
} else {
printf("%d is not a leap year", &year);
}
getch();
}
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
*********************************************************
slip 12
i. Write a C program to read the age of a candidate and
determine
whether the candidate is eligible for casting his/her own
vote.[5 marks]
Ans-
#include <stdio.h>
void main()
{
int age;
#include <stdio.h>
int main()
{
int i, num, sum = 0;
/* Input a number from user */
printf("Enter any number to check perfect number: ");
scanf("%d", &num);
/* Calculate sum of all proper divisors */
for(i = 1; i <= num / 2; i++)
{
/* If i is a divisor of num */
if(num%i == 0)
{
sum += i;
}
}
/* Check whether the sum of proper divisors is equal to
num */
if(sum == num && num > 0)
{
printf("%d is PERFECT NUMBER", num);
}
else
{
printf("%d is NOT PERFECT NUMBER", num);
}
return 0;
}
*********************************************************
Slip 13
int age;
-------------------------------------------------------------------------------------
-------------
ii.Write a C program to check if a number is perfect (number
= sum of its factors)
[10 marks]
#include <stdio.h>
int main()
{
int i, num, sum = 0;
/* Input a number from user */
printf("Enter any number to check perfect number: ");
scanf("%d", &num);
/* Calculate sum of all proper divisors */
for(i = 1; i <= num / 2; i++)
{
/* If i is a divisor of num */
if(num%i == 0)
{
sum += i;
}
}
/* Check whether the sum of proper divisors is equal to
num */
if(sum == num && num > 0)
{
printf("%d is PERFECT NUMBER", num);
}
else
{
printf("%d is NOT PERFECT NUMBER", num);
}
return 0;
}
Slip 14
i. Write a C program to accept a number and check if it is
positive,
negative or zero.[5 marks]
#include<stdio.h>
int main()
{
int num;
scanf("%d",&num);
if(num == 0)
printf("Neither positive nor negative, it is zero");
else if(num < 0)
printf("Negative");
else
printf("Positive");
return 0;
}
#include <stdio.h>
void main()
{
int cdigit;
case 1:
printf("one\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("invalid digit. \nPlease try again ....\n");
break; }
}
*********************************************************
Slip 15
int main(){
int x = 7;
return 0;
if (y == 0)
return 1;
*********************************************************
Slip 16
i.Write a C program to find the area and perimeter of
rectangle. [5 marks]
Ans-
#include <stdio.h>
int main()
{
int l = 10, b = 10, area, peri;
area=l*b;
peri=2 * (l + b);
printf("Area of rectangle is : %d", area);
printf("\n Perimeter of rectangle is : %d", peri);
return 0;
}
*********************************************************
Slip 17
int main()
{
int num;
scanf("%d",&num);
if(num == 0)
printf("Neither positive nor negative, it is zero");
else if(num < 0)
printf("Negative");
else
printf("Positive");
return 0;
}
switch(cdigit)
{
case 0:
printf("Zero\n");
break;
case 1:
printf("one\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("invalid digit. \nPlease try again ....\n");
break;
}
}
*********************************************************
slip 18
#include <stdio.h>
#include <math.h>
int main()
{
float radius, height;
float surface_area, volume;
printf("Enter value for radius and height of a cylinder : \n");
scanf("%f%f", &radius, &height);
surface_area = 2 * (22 / 7) * radius * (radius + height);
volume = (22 / 7) * radius * radius * height;
printf("Surface area of cylinder is: %f", surface_area);
printf("\n Volume of cylinder is : %f", volume);
}
first one.\n");
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
}
while (a<= b)
{
sum += a;
a++;
}
printf("Result : %d\n", sum);
}
*********************************************************
Slip 19
#include<stdio.h>
int main()
{
int a,b,res;
char c;
}
return 0;
}
*********************************************************
Slip 20
i. Write a C program to accept radius of circle and calculate
area and circumference.[5 marks]
Ans-
#include<stdio.h>
int main()
{
int r;
float Pi = 3.14, area, ci;
area = Pi* r * r;
printf("\nArea of circle : %f ", area);
ci = 2 * Pi * r;
printf("\nCircumference : %f ", ci);
return 0;
}
-------------------------------------------------------------------------------------
----------
ii. Write a function in C which accepts a character and
integer n as
parameter and displays the next n characters.[10 marks]
#include <stdio.h>
int display(char);
int main()
{
char ch;
printf("Enter character:");
scanf("%c", &ch);
display(ch);
}
int display(char ch)
{
int n,i;
printf("how many next char:");
scanf("%d",&n);
printf("\nYou entered:\t%c\n", ch);
printf("Next character :");
for(i=0;i<n;i++)
{
ch=ch+1;
printf("\t%c", ch);
}
return 0;
}
*********************************************************
Slip 21
scanf("%d%d",&a,&b);
res=a/b;
printf("\n The quotient is %d",res);
break;
default: printf ("\n Invalid entry");
}
}
• Write a ‘C’ program to check if a character is an
alphabet, digit or a special symbol. If it is an alphabet,
check if it is uppercase or lowercase. [10 marks]
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z')
{
printf("'%c' is uppercase alphabet.", ch);
}
else if(ch >= 'a' && ch <= 'z')
{
printf("'%c' is lowercase alphabet.", ch);
}
else if(ch>=0 && ch<=9)
{
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
*********************************************************
Slip 22
1.Write a ‘C’ program to accept radius of circle and calculate
area and circumference. [5 marks]
2.Write a function in ‘C’, which accepts a character and
integer n as parameter and displays the next n characters.
#include <stdio.h>
int display(char);
int main()
{
char ch,c;
printf("Enter character:");
scanf("%c", &ch);
display(ch);
}
int display(char ch)
{
int n,i;
printf("how many next char:");
scanf("%d",&n);
printf("\nYou entered:\t%c\n", ch);
printf("Next character :");
for(i=0;i<n;i++)
{
ch=ch+1;
printf("\t%c", ch);
}
return 0;
}
*********************************************************
Slip 23
#include <stdio.h>
int main()
{
float arr[30];
int n, i;
printf("Enter the length of the array: ");
scanf("%d",&n);
for(i =0;i<n;i++)
{
printf("\n Enter the element of the array: ");
scanf("%f",&arr[i]);
}
Slip24
1.Write a ‘C’ program to calculate area and circumference or
a circle. [5 marks]
#include<stdio.h>
void main()
{
float radius, area, cir;
printf("Enter Radius of Circle\n");
scanf("%f",&radius);
//value of pi is 3.14
area=3.14*radius*radius;
printf("The area of Circle is %f",area);
cir=2*3.14*radius;
printf("\nThe Circumference of Circle is
%f",cir);
}
Slip25
return 0;
}
2. Write a ‘C’ program to accept a character and check if it is
alphabet or digit. If it is alphabet, check if it is a vowel or
consonant.
#include <stdio.h>
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z')) {
if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=
='I' || ch=='o' || ch=='O' || ch== 'u' || ch=='U')
printf("%c is a vowel.\n", ch);
else
printf("%c is a consonant.\n", ch);
}
else
printf("%c is neither a vowel nor a consonant.\n", ch);
return 0;
}
*********************************************************
Slip26
1.Write a ‘C’ program to interchange two numbers. [5 marks]
#include<stdio.h>
int main()
{
int x, y, temp;
return 0;
}
2. Write a ‘C’ program to accept an array of n integers and
display them in the reverse order.
int main()
{
//Initialize array
int arr[] = {1,2,3,4,5};
int i;
//Calculate length of array arr
*********************************************************
Slip27
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,i,n,temp; clrscr();
printf("Enter two numbers:\n");
scanf("%d%d",&x,&y);
printf("\n1: Equality\n 2: to check x is less than y or
not\n 3: Swap\n");
printf("Choose any option:\n");
scanf("%d",&i);
switch(i)
{
case 1:
if(x==y)
printf("x is equal to y");
else
printf("x is not equal to y");
break;
case 2:
if(x<y)
printf("x is less than y");
else
printf("x is not less than y");
break;
case 5:
printf("before swap x=%d & y=%d\n",x,y);
k=x;
x=y;
y=temp;
printf("After swap x=%d & y=%d",x,y);
break;
default:
printf("Please choose correct option");
}
getch();
}
•
*********************************************************
Slip28
• Write a ‘C’ program to accept initial velocity (u) ,
acceleration (a), and time (t). Print the final velocity (v).
( Use formula v = u + at) [5 marks]
#include<stdio.h>
void main()
{
int time;
float distance , initial_velocity,velocity, acceleration;
printf("Enter the time in seconds \n");
scanf("%d",&time);
printf("Enter the initial velocity \n");
scanf("%f", &initial_velocity);
printf("Enter the acceleration \n");
scanf("%f", &acceleration);
velocity = (initial_velocity + (acceleration * time));
distance = (initial_velocity + (acceleration * (time*time)))
printf(" Total velocity is %f",velocity);
printf("\n Total distance travelled is %f", distance);
}
• 2.Write a ‘C’ function isPrime, which accepts an integer
number as parameter and returns 1 if the number is
prime and 0 otherwise. Use this function to check if a
number is prime.
(Prime numbers are natural numbers that are
divisible by only 1 and the number itself. In other words,
prime numbers are positive integers greater than 1 with
exactly two factors, 1 and the number itself. Some of
the prime numbers include 2, 3, 5, 7, 11, 13, etc.
Always remember that 1 is neither prime nor
composite. )
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
*********************************************************
Slip29
1.Write a ‘C’ program to calculate the area and perimeter of
a rectangle. [5 marks]
Ans-
#include <stdio.h>
int main()
{
int l = 10, b = 10, area, peri;
area=l*b;
peri=2 * (l + b);
printf("Area of rectangle is : %d", area);
printf("\n Perimeter of rectangle is : %d", peri);
return 0;
}
#include<stdio.h>
int main()
{
int x,n,sum=0,i;
printf("enter value of x : ");
scanf("%d",&x);
printf("enter limit n : ");
scanf("%d",&n);
n=n*(2);
for(i=0;i<n;i++)
{
if(i%2!=0)
{
sum=sum + (i * x);
}
}
printf("sum of series : %d",sum);
}
*********************************************************
Slip30
• Write a ‘C’ program to calculate area and perimeter of a
rectangle. [5 marks]
Ans-
#include <stdio.h>
int main()
{
int l = 10, b = 10, area, peri;
area=l*b;
peri=2 * (l + b);
printf("Area of rectangle is : %d", area);
printf("\n Perimeter of rectangle is : %d", peri);
return 0;
}
scanf("%d%d",&a,&b);
res=a*b;
printf("\n The product is %d",res);
break;
case '/':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a/b;
printf("\n The quotient is %d",res);
break;
default: printf ("\n Invalid entry");
}
}
*********************************************************
Slip 31
• Write a ‘C’ program to accept a character and check if it
is a vowel or not
#include <stdio.h>
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z')) {
if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || c
h=='I' || ch=='o' || ch=='O' || ch== 'u' || ch=='U')
printf("%c is a vowel.\n", ch);
else
printf("%c is a consonant.\n", ch);
}
else
printf("%c is neither a vowel nor a consonant.\n", ch);
return 0;
}