0% found this document useful (0 votes)
22 views

c Practical Slips

Uploaded by

abhirajpande141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

c Practical Slips

Uploaded by

abhirajpande141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

lOMoARcPSD|46669343

C practical slips

Bachelors of Arts (Purnea University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by abhiraj pande ([email protected])
lOMoARcPSD|46669343

C practical slips (programs)

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-

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

#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

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

1. Write a C program to accept two integers from the user


and interchange them. (Hint: swap numbers)
Display the interchanged numbers.[5 marks]
Ans-
#include<stdio.h>
int main()
{
Int a, b, temp;
printf("Enter two number a and b :");
scanf("%d %d",&a, &b);
printf("Before swapping a = %d and b= %d ", a,b);
temp=a;
a=b;
b=temp;
printf("After swapping a = %d and b= %d ",a,b);
return 0;
}

2. Write C program to accept a single digit and display it in


words. For
example, Input = 9 Output = Nine [10 marks]
Ans-
#include <stdio.h>
void main()
{
int number;
printf("Enter any single Digit in between 0-9 : ");
scanf("%d",&number);
switch(number)
{
case 0:
printf("Zero\n");
break;
case 1:
printf("one\n");
break;

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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

1. Write a C program to accept three dimensions length (l),


breadth(b)
and height(h) of a cuboid and print surface area
(surface area = 2(lb+lh+bh) [5 marks]
Ans-
#include <stdio.h>
int main()
{
int SA, l, h, b;
clrscr();
SA= 2*l*b+l * h+ b*h;
printf(" Surface Area of cuboid is : %d", SA);
getch();

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

return 0;
}

2. Write a C program to accept an array of n float values and


display
them in the reverse order. [10 marks]
int main()
{
//Initialize array
float arr[] = {1.5, 2.9, 3.4, 4.1, 5.0};
int i;
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%f", arr[i]);
}
printf("\n");
printf("Array in reverse order: \n");
//Loop through the array in reverse order
for ( i = length-1; i >= 0; i--) {
printf("%f ", arr[i]);
}
return 0;
}

**********************************************************
Slip 5

• Write a C program to check whether the given year is


leap year or not. [5 marks]
Ans-
#include <stdio.h>
#include <conio.h>
int main()
{

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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;
}

2. Write a C program to display all numbers between two


given
numbers.[10 marks]
Ans-
#include <stdio.h>
#include <conio.h>
int main()
{
int start , end, n;
clrscr();
printf("Enter any two numbers: ”);
scanf(“%d %d”, &start, &end);
for(n=start; n<=end; n++)
{
printf("%d”, n);
}
getch();
return 0;
}

*********************************************************
Slip 6

• Write a C program to find maximum of two numbers. [5


marks]
Ans-

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

#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;
}

2. Write a recursive function in C to calculate factorial of a


number. Use
this function in main. [10 marks]
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}

*********************************************************
Slip 7

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

1. Write a C program to calculate area and circumference of


a circle. [5 marks]
Ans-
#include<stdio.h>
int main()
{
int r;
float Pi = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &r);
area = Pi* r * r;
printf("\nArea of circle : %f ", area);
ci = 2 * Pi * r;
printf("\nCircumference : %f ", ci);
return 0;
}
2. Write a C program to accept a character and check if it is
alphabet,
digit or 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 8

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

1.Write a C program to accept an integer and check if it is


divisible by 3 and 5.[5 marks]
Ans-
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
clrscr();
printf("Enter any number: ”);
if(num%3==0 && num%5==0)
printf("%d number is divisible by 3 and 5”, num);
else
printf("%d number is NOT divisible by 3 and 5”, num);
getch();
return 0;
}

• Write a function in C to calculate sum of digits of an


integer. Use this function in main[10 marks]
#include <stdio.h>
int sumofdigits(int n)
{
if (n == 0)
return 0;
else
return n % 10 + sumofdigits(n / 10);
}
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Sum of digits of the number is %d",
sumofdigits(n));
return 0;
}

**********************************************************
Slip 9
• Write a C program to interchange two numbers and

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

display the interchanged numbers.[5 marks]


Ans-
#include <stdio.h>
int main()
{
int a=10, b=20, temp;
printf("Before swapping numbers are a= %d and b=%d
", a,b);
temp=a;
a=b;
b=temp;
printf("After swapping numbers are a= %d and b=%d ",
a,b);
getch();
return 0;
}

• Write a function in C to reverse an integer. Use this in


main. (ex. 567 output 765 ) [10 marks]
#include<stdio.h>
int main()
{
int n,j=0,p;
printf("enter the no : ");
scanf("%d",&n);
printf("reverse number = ");
while(n!=0)
{
p=n%10;
n=n/10;
j++;
printf("%d",p);
}
}

**********************************************************
Slip 10

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

1. Write a C program to check whether a given number is


even or odd. [5 marks]
Ans-
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if number is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

2. Write a C program to accept n integers in an array and


display the array in reverse order.
[10 marks]
int main()
{
//Initialize array
int arr[] = {1,2,3,4,5};
int i;
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d", arr[i]);
}
printf("\n");

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

printf("Array in reverse order: \n");


//Loop through the array in reverse order
for ( i = length-1; i >= 0; i--) {
printf("%d", arr[i]);
}
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();
}

ii.Write a C program to accept an integer and display its sum


of digits. [10 marks]
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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;

printf("Input the age of the candidate : ");


scanf("%d",&age);
if (age<18)
{
printf("You are not eligible to caste your vote.\n");
}
else
printf(" You are eligible for casting your vote.\n");
}
-------------------------------------------------------------------------------------
-------------
ii.Write a C program to check if a number is perfect (number
= sum of
its factors)[10 marks]
(A perfect number is a positive integer that is equal to the
sum of its factors except for the number itself.)

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

#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

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()
{

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

int age;

printf("Input the age of the candidate : ");


scanf("%d",&age);
if (age<18)
{
printf("You are not eligible to caste your vote.\n");
}
else
printf(" You are eligible for casting your vote.\n");
}

-------------------------------------------------------------------------------------
-------------
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)

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

{
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;
}

ii. Write C program to accept a single digit number and


display it in
words. For example, Input = 9 Output = Nine[10 marks]

#include <stdio.h>
void main()
{
int cdigit;

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

printf("Input Digit(0-9) : ");


scanf("%d",&cdigit);
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:

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("invalid digit. \nPlease try again ....\n");
break; }
}
*********************************************************

Slip 15

• Write a C program to perform all arithmetic


operations on two integers.[5 marks]

• Write a C program to calculate the factorial of a


number using function. [10 marks]
#include <stdio.h>

int main(){

int x = 7;

printf("The factorial of the number is %d", fact(x));

return 0;

// Recursive function to find factorial

int fact(int y){

if (y == 0)

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

return 1;

return y * fact(y - 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;
}

ii.Write a C program to display n lines of the following


pattern.[10 marks]
1
23
456
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

*********************************************************
Slip 17

i. Write a C program to accept a number and check number


is
positive or negative.[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;
}

ii. Write a C program to accept a single digit number from


the user
and display it in words. Input = 9, output = Nine.
[10 marks]
#include <stdio.h>
void main()
{
int cdigit;

printf("Input Digit(0-9) : ");


scanf("%d",&cdigit);

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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:

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

printf("Nine\n");
break;
default:
printf("invalid digit. \nPlease try again ....\n");
break;
}
}
*********************************************************
slip 18

i. Write a C program to Accept dimensions of a cylinder and


print the
surface area and volume. ( surface area = 2Ï€ r2+ 2Ï€ r h,
volume = π r2 h)[5 marks]

#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);
}

ii. Write a Cprogram to Accept two integers x and y and


calculate the
sum of all integers between x and y.[10 marks]
#include <stdio.h>
void main(void)
{
int a = 1;
int b = 0;
int sum = 0;
while (a > b)
{
printf("The second number should be bigger than the

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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

i. Write a C program to accept two integers and perform all


arithmetic operations.[5 marks]

#include<stdio.h>
int main()
{
int a,b,res;
char c;

printf("\n Enter any one operator +, -, *, / :");


scanf("%c",&c);
switch(c)
{
case '+':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a+b;
printf("\n The sum is %d",res);
break;
case '-':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a-b;
printf("\n The difference is %d",res);
break;
case '*':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a*b;

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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");
}
}

ii. 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 digit.", ch);
}
else
{
printf("'%c' is special charactor.", ch);

}
return 0;
}

*********************************************************
Slip 20
i. Write a C program to accept radius of circle and calculate
area and circumference.[5 marks]

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

Ans-
#include<stdio.h>
int main()
{
int r;
float Pi = 3.14, area, ci;

printf("\nEnter radius of circle: ");


scanf("%d", &r);

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:");

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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

1.Write a ‘C’ program to accept two integers and perform all


arithmetic operations. [5 marks]
#include<stdio.h>
int main()
{
int a,b,res;
char c;

printf("\n Enter any one operator +, -, *, / :");


scanf("%c",&c);
switch(c)
{
case '+':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a+b;
printf("\n The sum is %d",res);
break;
case '-':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a-b;
printf("\n The difference is %d",res);
break;
case '*':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a*b;
printf("\n The product is %d",res);
break;
case '/':
printf("\n Enter two numbers: ");

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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:");

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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

1.Write a C program to check whether number is positive or


not [5 marks]
2.Write a C program to accept n float values in an array and
display them in the reverse order.

#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]);
}

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

printf("\nThe original array: ");


for(i=0;i<n;i++)
{
printf("%0.1f\n",arr[i]);
}

printf("\n The reversed array is: ");


for(i=n-1;i>=0;i--)
{
printf("%0.1f \n",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);

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

cir=2*3.14*radius;
printf("\nThe Circumference of Circle is
%f",cir);
}

2.Write a ‘C’ function to calculate the sum of digits of a


number. Use this function in main to accept a number and
print sum of its digits.
*********************************************************

Slip25

1.Write a ‘C’ program to interchange two numbers. [5 marks]


#include<stdio.h>
int main()
{
int x, y, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

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");

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

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

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

int length = sizeof(arr)/sizeof(arr[0]);


printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Array in reverse order: \n");
//Loop through the array in reverse order
for ( i = length-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}

*********************************************************

Slip27

• Write a ‘C’ program to accept the character from the


keyboard and display its previous and next character with
ASCII values. . [5 marks]
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character:");
scanf("%c", &ch);

/* Using the format specifiers we can get the ASCII code


* of a character. When we use %d format specifier for a
* char variable then it displays the ASCII value of a char
*/
printf("ASCII value of character %c = %d", ch, ch);
printf("ASCII value of next character %c = %d", ch+1, ch+1);
printf("ASCII value of previous character %c = %d", ch-1, ch-1);

return 0;
}

• Write a ‘C’ program to accept two numbers in variable x


and y from user and perform the following operation.
Options Actions 1. Equality Check if x is equal to y. 2. Less
Than Check if x is less than y. 3. Swap Interchange x and
y.

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

#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()

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

{
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)

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

printf("%d is a prime number.", n);


else
printf("%d is not a prime number.", n);
return 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;
}

2.Write a ‘C’ program to accept real number x and integer n


and calculate the sum of first n terms of the series x+
3x+5x+7x+…

#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);
}

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

*********************************************************

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;
}

• Write a ‘C’ program, which accepts two integers and an


operator as a character (+ - * /), performs the
corresponding operation and displays the result.
#include<stdio.h>
int main()
{
int a,b,res;
char c;

printf("\n Enter any one operator +, -, *, / :");


scanf("%c",&c);
switch(c)
{
case '+':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a+b;
printf("\n The sum is %d",res);
break;
case '-':
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b);
res=a-b;
printf("\n The difference is %d",res);
break;
case '*':
printf("\n Enter two numbers: ");

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

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;
}

• Write a ‘C’ program , which accept annual basic salary


of an employee and calculates and displays the Income
tax as per the following rule.
Basic salary <150000 tax=0%

Downloaded by abhiraj pande ([email protected])


lOMoARcPSD|46669343

150000 to 300000 tax=20%


>300000 tax=30%

Write a ‘C’ program, which accepts annual basic salary of an


employee and calculates and displays the Income tax as per
the
following rules.
Basic: < 1,50,000 Tax = 0
1,50,000 to 3,00,000 Tax = 20%
> 3,00,000 Tax = 30%
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float salary,tax;
printf("enter the emp basic salary :");
scanf("%f",&salary);
if(salary<=150000)
{
tax=0;
printf("tax = %f",tax);
}
else if(salary>150000 && salary<=300000)
{
tax=(salary*0.2);
printf("tax = %f",tax);
}
else if(salary>300000)
{
tax=(salary*0.3);
printf("tax = %f",tax);
}
}

Downloaded by abhiraj pande ([email protected])

You might also like