0% found this document useful (0 votes)
94 views115 pages

Assignment 1: Bangalore Institute of Technology

The document contains 11 programming problems with solutions in C language. The problems cover basic concepts like calculating area of a circle, printing ASCII value of a character, data type sizes, currency conversion, and bill calculation. Each problem has the code to solve it along with sample input/output. The code includes basics like input/output, arithmetic operators, conditional statements, loops and functions.

Uploaded by

Shantanu Prashar
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)
94 views115 pages

Assignment 1: Bangalore Institute of Technology

The document contains 11 programming problems with solutions in C language. The problems cover basic concepts like calculating area of a circle, printing ASCII value of a character, data type sizes, currency conversion, and bill calculation. Each problem has the code to solve it along with sample input/output. The code includes basics like input/output, arithmetic operators, conditional statements, loops and functions.

Uploaded by

Shantanu Prashar
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/ 115

Roll No.

BANGALORE INSTITUTE OF TECHNOLOGY


K.R. Road, V.V. Pura, Bengaluru-560004
Ph:080-26613237,26615865, Fax: 080-22426796

ASSIGNMENT 1

Name :

Mobile No. :

E-mail :

Semester/Section : ........................................Academic Year …………….

Branch : …………………………………………………………………………

Subject with code : …………………………………………………………….

Signature of the Teacher Signature of the Student


1.Evaluating expressions using the precedence chart

1. x = 3 * 4 + 5 * 6
= 12 + 5 * 6
= 12 + 30
= 42
2. x = 3 * (4 + 5) * 6
= 3*9*6
= 27 * 6
= 162
3. x = 3 * 4 % 5 / 2
= 12 % 5 / 2
= 2/2
=1
4. x = 3 * (4 % 5) / 2
= 3*412
= 12 / 2
= 6
5. x = 3 * 4% (5 / 2)
=3*4%2
=12%2
=0
6. x = 3 * ((4 % 5) / 2)
= 3 * (4 / 2)
=3*2
=6
Take the following variable declarations,
int a = 0, b = 1, c = -1;
float x=2.5, y=0.0;
If we write,
a=b=c=7;
Since the assignment operator works from
right-to-left, c = 7.
Then since b = c, therefore b= 7. Now
a = b, so a = 7.
7.a += b -= c *= 10
This is expanded as
a = a + (b = b - (c = c * 10))

CP Assignment Page 2
= a + (b = 1 - (-10)) = a + (b = 11)
= 0 + 11
= 11
8. --a-*(5+b)/2-c++*b
= --a * 6 / 2 - c++ * b
= --a * 6 / 2 - -1 * b
(Value of c has been incremented but its altered
value will not be visible for the evaluation
of this expression)
= -1 * 6 / 2 - -1 * 1
(Value of a has been incremented and its altered
value will be used for the evaluation of this
expression)
= -1 * 3 - -1 * 1
=-3
=-3
=-2
9. a * b * c
= (a * b) * c
(because associativity o f * is from
left-to-right)
=0
10. a && b
=0
11. a ‹ b && c ‹ b
=1
12. b+c || !a
= ( b + c) || (!a)
= 0 || 1
= 1
13. x * 5 && 5 | ( b / c)
= ((x * 5) & 5) || (b / c)
= (12.5 & 5) || (1/-1)
= 1
14. a <= 10 && x >
= 1 && b
= ((a =‹ 10) & (× => 1)) && b
= (1 && 1) && 1
=1

CP Assignment Page 3
15. | x || | c || b + c
=((!x)||(!c))| (b+c)
= ( 0 || 0 ) || 0
= 0
16.x * y ‹ a+b | c
=((x *y) <(a +b))|| c
= (0 ‹ 1) || -1
= 1
17.(x > y) + !a | | c++
=((x >y) + (!a)) || (c++)
=(1+1 ) || 0
= 1
2. Write a program to calculate the area of a circle.

#include ‹stdio.h>
#include ‹conio.h>
Int main()
{
float radius;
double area, circumference;
clrscr();
printf("In Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circumference = 2*3.14 *radius;
printf("AREA = %.2le", area);
printf ( " \n CIRCUMFERENCE = %.2e",circumference);
return 0;
}

Output:

Enter the radius of the circle: 7


AREA = 153.86
CIRCUMFERENCE = 4.40+01

3. Write a program to print the ASCII value of a character.

CP Assignment Page 4
#include ‹stdio.h>
#include <conio.h>
int main()
{
char ch:
clrscr();
printf("In Enter any character: ");
scanf("%c", &ch);
printf("In The ASCII value of % is: %d", ch, ch);
return 0;
}
Output:
Enter any character: A
The ASCII value of A is: 65
Enter any character: A
The ASCII value of A is: 65

4. Write a program to read a character in uppercase and then print it in lower case.

#include <stdio.h>
#include ‹conio.h>
int main)
{
char ch; clrscr():
printf("In Enter any character in uppercase: ");
scanf("%c", &ch);
printf("In The character in lower case is: % " ,
ch+32);
return 0;
}
Output:

Enter any character: A


The character in lower case is: a

5. Write a program to print the digit at ones place of a number.

#include <stdio.h>
#include <conic.h>

CP Assignment Page 5
int main ()
{
int num, digit_at_ones_place;
clrscr();
printf("In Enter any number: ");
scanf ("%d", &num);
digit_at_ones_place = num %10;
printf("In The digit at ones place of % is %", num,
digit_at_ones_place);
return 0;
}
Output:

Enter any number: 123


The digit at ones place of 123 is 3

6. Write a program to swap two numbers using a temporary variable.

#include ‹stdio.h>
#include ‹conio.h>
int main()
{
int num 1, num 2, temp;
clrscr();
printf("In Enter the first number: ");
scanf("%d" ,&num 1);
printf("in Enter the second number: ");
scanf("%d" ,&num 2);
temp = num 1;
num1 = num 2;
num2 = temp;
printf("\nThe first number is %d", num 1);
printf("\n The second number is %d",num 2);
return 0;
}
Output:

Enter the first number:3


Enter the second number: 5

CP Assignment Page 6
The first number is 5
The second number is 3

7. Write a program to swap two numbers without using a temporary variable.

#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;
clrscr();
printf("'In Enter the first number: ");
scanf("%d", &num1);
printf("'n Enter the second number: ");
scanf("%d" ,&num2);
num1 = num1 + num2;
num2 = num1 - num2; num1 = num1 - num2;
printf("\n The first number is %d", num1);
printf("\n The second number is%d",num2);
return 0;
}

Output:

Enter the first number: 3


Enter the second number: 5
The first number is 5
The second number is 3

8. Write a program to convert degrees Fahrenheit into degrees celsius.

#include ‹stdio.h>
#include ‹conio.h>
int main()

CP Assignment Page 7
{
float fahrenheit, celsius;
printf("In Enter the temperature in fahrenheit: ");
scanf("%f", & fahrenheit);
celsius = (0.56) * (fahrenheit - 32);
printf("\n Temperature in degrees celsius= %f",
celsius);
return 0;
}

Output:

Enter the temperature in fahrenheit: 32


Temperature in degree celsius = o

9. Write a program that displays the size of every data type.

#include ‹stdio.h>
#include ‹conio.h>
int main()
{
clrscr();
printf("\n The size of short integer is:
%d",sizeof(shortint));
printf("\n The size of unsigned integer is: %d",
sizeof(unsigned int));
printf("\n The size of signed integer is:
%d", sizeof(signed int));
printf("\n The size of integer is: %d",
sizeof(int));
printf("\n The size of long integer is:
%d", sizeof(long int));
printf("\n The size of character is: %d",
sizeof(char));
printf("\n The size of unsigned character is: %",
sizeof(unsigned char));
printf("\n The size of signed character is: %d", sizeof
(signed char));
printf("\n The size of floating point number

CP Assignment Page 8
is: %d", sizeof(float));
printf("\n The size of double number is:
%d", sizeof (double));
return 0;
}

Output:
The size of short integer is: 2
The size of unsigned integer is: 2
The size of signed integer is: 2
The size of integer is: 2
The size of long integer is: 2

The size of character is: 1


The size of unsigned character is: 1
The size of signed character is: 1

The size of floating point number is: 4


The size of double number is: 8

10. Write a program to calculate the total amount of money in the piggy bank,given the coins
of Rs 10, Rs 5, Rs2, and Rs 1.

#include ‹stdio.h>
#include ‹conio.h>
int main()
{
int num_of_10_coins, num_of_5_coins,
num_of_2_coins, num_of_1_coins;
float totalamt = 0.0;
clrscr();
printf("\n Enter the number of Rs10 coins
in the piggy bank: ");
scanf("%d", &num_of_10_coins);
printf("\n Enter the number of Rs5 coins
in the piggy bank: " ) ;
scanf("%d", &num_of_5_coins);
printf("\n Enter the number of Rs2 coins
in the piggy bank: ");
scanf("%d", &num_of_2 _coins);

CP Assignment Page 9
printf("\n Enter the number of Rel coins
in the piggy bank: " ) ;
scanf("%d", &num_of_1_coins);
total_amt = num_of_10_coins * 10 + num_of. 5_coins * 5
+ num_of_2_coins * 2 + num_of. 1_coins ;
printf("\n Total amount in the piggybank = %f",
total_amt);
getch();
return 0;
}

Output:

Enter the number of Rs10 coins in the piggy bank: 10


Enter the number of Rs5 coins in the
piggybank: 23
Enter the number of Rs2 coins in the
piggybank: 43
Enter the number of Rel coins in the
piggybank: 6
Total amount in the piggybank = 307

11.Write a program to calculate the bill amount for an item given its quantity sold, value,
discount, and tax.

#include ‹stdio.h>
#include ‹conio.h>
int main()
{
float total_amt, amt, sub_total, discount_amt, tax_amt,
qty, val, discount, tax;
printf("\n Enter the quantity of item sold: ");
scanf("%f", &qty);
printf("In Enter the value of item: ");
scanf("%f", &val);
printf("\n Enter the discount percentage: ");
scanf("%f", &discount);
printf("\n Enter the tax: ");
scanf("%f", &tax);

CP Assignment Page 10
amt = qty * val;
discount_amt = (amt * discount)/100.0; sub_total = amt
- discount_amt; tax_amt = (sub_total * tax) /100.0;
total_amt = sub_total + tax_amt;
printf("\n\n\n ************* BILL *************");
printf("\n Quantity Sold: %f", qty);
printf("\n Price per item: %f", val);
printf("\n—-------------------------");
printf("\n Amount: %f", amt);
printf("\n Discount: - %f", discount_ant);
printf("\n Discounted Total: %f", sub_total);
printf("\n Tax: + %f", tax amt);
printf("\n—--------------------------");
printf("In Total Amount %f", total_amt);
return 0;
}

Output:

Enter the quantity of item sold: 20


Enter the value of item: 300
Enter the discount percentage: 10
Enter the tax: 12
************* BILL *************
quantity Sold : 20
price per item : 300
—-------------------------------
Amount : 6000
Discount : - 600
Discounted Total : 5400
Tax : + 648
—-------------------------------
Total Amount 6048

12. Write a program to convert a floating point number into the corresponding integer.

#include<stdio.h>

CP Assignment Page 11
#include<conio.h>
int main()
{
float f_num;
int i_num;
clrscr();
printf("\n Enter any floating number:");
scanf("%f", &f_num);
i_num = (int)f_num;
printf("\n The integer variant of %f is = %d",
f_num, i_num);
return 0;
}
Output:

13. Write a program to convert an integer into the corresponding floating point number.

#include<stdio.h>
#include<conio.h>
int main()
{
float f_num;
int i_num;
clrscr();
printf("\n Enter any integer:");
scanf("%d", &i_num);
f_num = (float)i_num;
printf("\n The floating point variant of %d is =
%f", i_num, f_num);
return 0;
}

CP Assignment Page 12
Output:

14. Write a program to calculate a student’s result based on two examinations, one sports
event, and three activities conducted. The weightage of activities = 30%,sports = 20%, and
examination =50%.

#include<stdio.h>
#include<conio.h>
#define ACTIVITIES_WEIGHTAGE 30
#define SPORTS_WEIGHTAGE 20
#define EXAMS_WEIGHTAGE 50
#define EXAMS_TOTAL 200
#define ACTIVITIES_TOTAL 60
#define SPORTS_TOTAL 50
int main()
{
int exam_score1, activities_score1, sports_score;
int exam_score2, activities_score2,
activities_score3;
float exam_total, activities_total;
float total_percent, exam_percent,
sports_percent, activities_percent;
clrscr();
printf("\n Enter the score obtained in two
examinations (out of 100): ");
scanf("%d %d", &exam_score1, &exam_score2);
printf("\n Enter the score obtained in sports event
(out of 50):");
scanf("%d", &sports_score);
printf("\n Enter the score obtained in three
activities (out of 20): ");

CP Assignment Page 13
scanf("%d %d %d", &activities_score1,
&activities_score2, &activities_score3);

exam_total = exam_score1 +exam_score2;


activities_total = activities_score1 +
activities_score2 + activities_score3;

exam_percent = (float)exam_total * EXAMS_WEIGHTAGE


/ EXAMS_TOTAL;
sports_percent = (float)sports_score *
SPORTS_WEIGHTAGE / SPORTS_TOTAL;
activities_percent = (float)activities_total *
ACTIVITIES_WEIGHTAGE / ACTIVITIES_TOTAL;

total_percent = exam_percent + sports_percent +


activities_percent;

printf("\n\n
********************RESULT********************");
printf("\n Total percent in examination:
%f",exam_percent);
printf("\n Total percent in activities:
%f",activities_percent);
printf("\n Total percent in sports:
%f",sports_percent);
printf("\n
--------------------------------------------------");
printf("\n Total percentage :%f", total_percent);
return 0;
}

Output:

CP Assignment Page 14
Find the output of the following codes.

15. #include<stdio.h>
int main()
{
int x=3,y=5,z=7;
int a,b;
a = x * 2 + y / 5 - z * y;
b= ++x * (y - 3) / 2 - z++ * y;
printf("\n a = %d", a);
printf("\n b = %d", b);
return 0;
}

int x=3,y=5,z=7;
int a,b;
a = x * 2 + y / 5 - z * y;
b= ++x * (y - 3) / 2 - z++ * y;
printf("\n a = %d", a);
printf("\n b = %d", b);
return 0;
}

Output:

CP Assignment Page 15
16. #include<stdio.h>
int main()
{
int a, b=3;
char c = 'A ';
a = b + c;
printf("\n a = %d", a);
return 0;
}

Output:

17. #include<stdio.h>
int main()
{
int a;
printf("\n %d", 1/3 + 1/3);
printf("\n %f", 1.0/3.0 + 1.0/3.0);
a = 15/10.0 + 3/2;
printf("\n %d", a);
return 0;
}

Output:

18. #include<stdio.h>

CP Assignment Page 16
int main()
{
int a = 4;
printf("\n %d", 10 + a++);
printf("\n %d", 10 + ++a);
return 0;
}

Output:

19. #include<stdio.h>
int main()
{
int a = 4, b = 5, c = 6;
a = b == c;
printf("\n a = %d", a);
return 0;
}

Output:

20. #include <stdio.h>


#include <conio.h>
int main()
{
int a=1, b=2, c=3, d=4, e=5, res;
clrscr();
res = a + b /c - d * e;

CP Assignment Page 17
printf("\n Result = %d", res);
res = (a + b) /c - d * e;
printf("\n Result = %d", res);
res = a + ( b / (c -d)) * e;
printf("\n Result = %d", res);
return 0;
}

Output:

21. #include<stdio.h>
int main()
{
int a = 4, b = 5;
printf("\n %d", (a>b)? a:b);
return 0;
}

Output:

22. #include<stdio.h>
int main()
{
int a = 4, b = 12, c = -3, res;
res = a > b && a < c;
printf("\n %d", res);
res = a ==c || a < b;
printf("\n %d", res);

CP Assignment Page 18
res = b >10 || b && c < 0 || a > 0;
printf("\n %d", res);
res = (a/2.0 == 0.0 && b/2.0 != 0.0) || c <
0.0;
printf("\n %d", res);
return 0;
}

Output:

23. #include <stdio.h>


int main()
{
int a = 20, b = 5, result;
float c = 20.0, d= 5.0;
printf("\n 10 + a / 4 * b = %d", 10 + a / 4 *
b);
printf("\n c / d * b + a - b = %d", c / d * b +
a - b);
return 0;
}

Output:

24. #include<stdio.h>
int main()

CP Assignment Page 19
{
int a, b;
printf("\n a =%d \t b = %d \t a + b = %d", a,
b, a+b);
return 0;
}

Output:

25. #include<stdio.h>
int main ()
{
printf("\n %d", 'F');
return 0;
}

Output:

26. #include<stdio.h>
int main ()
{
int n = 2;
n = !n;
printf("\n n = %d", n);
return 0;
}

Output:

CP Assignment Page 20
27. #include<stdio.h>
int main ()
{
int a = 100, b =3;
float c;
c = a/b;
printf("\n c =%f", c);
return 0;
}

Output:

28. #include<stdio.h>
int main ()
{
int n = -2;
printf("\n n =%d", -n);
return 0;
}

Output:

29. #include<stdio.h>
int main ()
{
int a = 2, b =3, c, d;
c = a++;
d = ++b;

CP Assignment Page 21
printf("\n c = %d d = %d",c,d);
return 0;
}

Output:

30. #include <stdio.h>


int main()
{
int _=30;
printf("\n _=%d",_);
return 0;
}
Output:
_=30

31. #include <stdio.h>


int main()
{
int a=2,b=3;
a++;
++b;
printf("\n a=%d b=%d",a,b);
return 0;
}
Output:
a=3 b=4

32. #include <stdio.h>


int main()
{

CP Assignment Page 22
int a=2,b=3,c;
c = (a-b);
printf("\n %d",++c);
return 0;
}
Output:
0

33. #include <stdio.h>


int main()
{
int a=2,b=3;
printf("\n %d",++a-b);
return 0;
}
Output:
0

34. #include <stdio.h>


int main()
{
int a=2,b=3;
printf("\n a*b=%d",a*b);
printf("\n a/b=%d",a/b);
printf("\n a mod b=%d",a%b);
printf("\n a&&b=%d",a &&b);
return 0;
}
Output:
a*b=6
a/b=0
a mod b=2
a&&b=1

CP Assignment Page 23
35. #include <stdio.h>
int main()
{
int a=2;
a = a+3*a++;
printf("\n a=%d",a);
return 0;
}
Output:
a=9

36. #include <stdio.h>


int main()
{
int result;
result = 3+5-1*17%-13;
printf("\n %d",result);
result = 3*2+(15/4%7);
printf("\n %d",result);
result = 18/9/3*2*3*5%10/4;
printf("\n %d",result);
return 0;
}
Output:
4
9
0

37. #include <stdio.h>


int main()
{
int n=2;

CP Assignment Page 24
printf("\n %d %d %d",n++,n,++n);
return 0;
}
Output:
3 4 4

38. #include <stdio.h>


int main()
{
int a=2,b=3,c=4;
a=b==c;
printf("\n a=%d",a);
return 0;
}
Output:
a=0

39. #include <stdio.h>


int main()
{
int num=070;
printf("\n num=%d",num);
printf("\n num=%o",num);
printf("\n num=%x",num);
return 0;
}
Output:
num=56
num=70
num=38

40. #include <stdio.h>


int main()

CP Assignment Page 25
{
printf("\n %40.27s Welcome to C Programming");
printf("\n %40.20s Welcome to C Programming");
printf("\n %40.14s Welcome to C Programming");
printf("\n %-40.27s Welcome to C Programming");
printf("\n %-40.20s Welcome to C Programming");
printf("\n %-40.14s Welcome to C Programming");
return 0;
}
Output:
+╣ Welcome to C Programming
+╣ Welcome to C Programming
+╣ Welcome to C Programming
+╣ Welcome to C Programming
+╣ Welcome to C Programming
+╣ Welcome to C Programming

41. #include <stdio.h>


int main()
{
int a=-21,b=3;
printf("\n %d",a/b+10);
b=-b;
printf("\n %d",a/b+10);
return 0;
}
Output:
3
17

42. #include <stdio.h>


int main()
{
int a;
float b;

CP Assignment Page 26
printf("Enter a four digit number :\n");
scanf("%d",&a);
printf("Enter a floating point number :\n");
scanf("%f",&b);
printf("\n The numbers are :%d and %f",a,b);
return 0;
}
Output:
Enter a four digit number :
1234
Enter a floating point number :
23.32
The numbers are :1234 and 23.320000

43. #include <stdio.h>


int main()
{
char a,b,c;
printf("\n Enter three characters :");
scanf("%c %c %c",&a,&b,&c);
a++,b++,c++;
printf("\n a=%c b=%c and c=%c",a,b,c);
return 0;
}
Output:
Enter three characters :
r
a
s
a=s b=b and c=t

44. #include <stdio.h>


int main()
{
int x=10,y=20,res;

CP Assignment Page 27
res = y++ + x++;
res += ++y + ++x;
printf("\n x=%d y=%d RESULT=%d",x,y,res);
return 0;
}
Output:
x=12 y=22 RESULT=64

45. #include <stdio.h>


int main()
{
int x=10,y=20,res;
res = x+++y;
printf("\n x=%d y=%d RESULT=%d",x,y,res);
return 0;
}
Output:
x=11 y=20 RESULT=30

46. Write a Program to determine whether a person is eligible to vote or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int age;
printf("\n Enter the age: ");
scanf("%d",&age);
if(age>=18)
printf("\n You are eligible to vote");
getch();
return 0;
}
Output:

Enter the age: 28


You are eligible to vote

CP Assignment Page 28
47. Write a Program to determine the Character entered by the user.

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
int main()
{
char ch;
printf("\n Press any key: ");
scanf("%c",&ch);
if(isalpha(ch)>0)
printf("\n The user has entered a character");
if(isdigit(ch)>0)
printf("\n The user has entered a digit");
if(isprint(ch)>0)
printf("\n The user has entered a printable
character");
if(ispunct(ch)>0)
printf("\n The user has entered a punctuation
mark");
if(isspace(ch)>0)
printf("\n The user has entered a white space
character");
getch();
return 0;
}
Output:

Press any key: 3


The user has entered a digit

48. Write a Program to find whether the given number is even or odd.

#include<stdio.h>
#include<conio.h>
int main()
{
int num;

CP Assignment Page 29
clrscr();
printf("\n Enter any number: ");
scanf("%d",&num);
if(num%2==0)
printf("\n %d is an even number",num);
else
printf("\n %d is an odd number",num);
return 0;
}
Output:

Enter any number: 11


11 is an odd number

49. Write a Program to enter any character. If the entered character is in lower case then
convert it into upper case and if it is a lower case character then convert it into upper case.
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c",&ch);
if(ch>='A' && ch<='Z')
printf("\n The entered character is in upper case.
In lower case it is: %c", (ch+32));
else
printf("\n The entered character is in lower case.
In upper case it is: %c", (ch-32));
return 0;
}
Output:

Enter any character: a


The entered character is in lower case. In upper case
it is: A

50. Write a program to enter a character and then determine whether it is vowel or not.

CP Assignment Page 30
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
clrscr();
printf("\n Enter any character: ");
scanf("%c",&ch);
if(ch='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u'
||ch=='A' ||ch=='E' ||ch=='I' ||ch=='O' ||ch=='U' )
printf("\n %c is a vowel", ch);
else
printf("\n %c is not a vowel");
getch();
return 0;
}
Output:

Enter any character: v


v is not a vowel

51. Write a program to find whether a given year is a leap year or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int year;
clrscr();
printf("\n Enter any year: ");
scanf("%d",&year);
if(((year%4==0) && ((year&100 !=0)) ||
(year%400==0))
printf("\n Leap year");
else
printf("\n Not a Leap year");
return 0;
}
Output:

CP Assignment Page 31
Enter any year: 1996
Leap year

52. Write a program to demonstrate the use of nested if structure.

#include<stdio.h>
int main()
{
int x,y;
printf("\n Enter the numbers: ");
scanf("%d %d", &x, &y);
if(x==y)
printf("\n The two numbers are equal");
else if(x > y)
printf("\n %d is greater than %d",x,y);
else
printf("\n %d is smaller than %d",x,y);
return 0;
}
Output:

Enter the numbers: 12 23


12 is smaller than 23

53. Write a program to test whether a number entered is positive, negative or equal to zero.

#include<stdio.h>
int main()
{
int num;
printf("\n Enter any number: ");
scanf("%d", &num);
if(num==0)
printf("\n The number is equal to zero");
else if(num>0)
printf("\n The number is positive");
else
printf("\n The number is negative");
return 0;

CP Assignment Page 32
}
Output:

Enter any number: 0


The number is equal to zero

54. A company decides to give bonus to all its employees on Diwali. A 5% bonus on salary is
given to the male workers and 10% bonus on salary is given to the female workers. Write a
program to enter the salary and sex of the employee. If the salary of the employee is less than
Rs 10,000 then the employee gets an extra 2% bonus on salary. Calculate the bonus that has
to be given to the employee and display the salary that the employee will get.

#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
float sal, bonus, amt_to_be_paid;
printf("\n Enter the sex of the employee (m or
f):");
scanf("%c",&ch);
printf("\n Enter the salary of the employee: ");
scanf("%f",&sal);
if(ch=='m')
bonus = 0.05*sal;
else
bonus = 0.10*sal;
if(sal<10000)
bonus += 0.20*sal;
amt_to_be_paid = sal+bonus;
printf("\n Salary=%f",sal);
printf("\n Bonus=%f",bonus);
printf("\n*********************************");
printf("\n Amount to be paid: %f",amt_to_be_paid);
getch();
return 0;
}
Output:

Enter the sex of the employee (m or f): f

CP Assignment Page 33
Enter the salary of the employee: 12000
Salary = 12000
Bonus = 1200
************************************
Amount to be paid: 13200

55. Write a program to display the examination result.


#include<stdio.h>
int main()
{
int marks;
printf("\n Enter the marks obtained: ");
scanf("%d",&marks);
if(marks>=75)
printf("\n DISTINCTION");
else if(marks>=60 && marks<75)
printf("\n FIRST DIVISION");
else if(marks>=50 && marks<60)
printf("\n SECOND DIVISION");
else if(marks>=40 && marks<50)
printf("\n THIRD DIVISION");
else
printf("\n FAIL");
return 0;
}
Output:

Enter the marks obtained: 55


SECOND DIVISION

56. Write a program to calculate tax, given the following conditions:


i. If income is less than 1,50,000 then no tax
ii. If taxable income is in range 1,50,001-3,00,000 then charge 10% tax
iii. If taxable income is in range 3,00,001-5,00.000 then charge 20% tax
iv. If taxable income is above 5,00,001 then charge 30% tax

#include<stdio.h>
#include<conio.h>
#define MIN1 150001
#define MAX1 300000

CP Assignment Page 34
#define RATE1 0.10
#define MIN2 300001
#define MAX2 500000
#define RATE2 0.20
#define MIN3 500001
#define RATE3 0.30
int main()
{
double income,taxable_income,tax;
clrscr();
printf("\n Enter the income: ");
scanf("lf",&income);
taxable_income = income-150000;
if(taxable_income<=0)
printf("\n NO TAX");
else if(taxable_income>= MIN1 &&
taxable_income<MAX1)
tax = (taxable_income - MIN1)*RATE1;
else if(taxable_income>=MIN2 &&
taxable_income<MAX2)
tax = (taxable_income - MIN2)*RATE2;
else
tax = (taxable_income - MIN3)*RATE3;
printf("\n TAX = %lf",tax);
getch();
return 0;
}
Output:

Enter the income: 900000


TAX = 74999.70

57. Write a program to find the greatest of three numbers

#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2,num3,big=0;

CP Assignment Page 35
clrscr();
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n Enter the second number: ");
scanf("%d",&num2);
printf("\n Enter the third number: ");
scanf("%d",&num3);
if(num1>num2)
{
if(num1>num3)
printf("\n %d is greater than %d and
%d",num1,num2,num3);
else
printf("\n %d is greater than %d and
%d",num3,num1,num2);
}
else if(num2>num3)
printf("\n %d is greater than %d and
%d",num2,num1,num3);
else
printf("\n %d is greater than %d and
%d",num3,num1,num2);
return 0;
}
Output:

Enter the first number: 12


Enter the second number: 23
Enter the third number: 9
23 is greater than 12 and 9

58. Write a program to input three numbers and then find largest of them using && operator.

#include<stdio.h>
#include<conio.h>
int main()
{
int num1, num2, num3;
clrscr();

CP Assignment Page 36
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n Enter the second number: ");
scanf("%d",&num2);
printf("\n Enter the third number: ");
scanf("%d",&num3);
if(num1>num2 && num1>num3)
printf("\n %d is the laregst number",num1);
if(num2>num1 && num2>num3)
printf("\n %d is the largest number",num2);
else
printf("\n %d is the largest number",num3);
getch();
return 0;
}
Output:

Enter the first number: 12


Enter the second number: 23
Enter the third number: 9
23 is the largest number

59. Write a program to enter the marks of a student in four subjects. Then calculate the total,
aggregate, and display the grades obtained by the student.

#include<stdio.h>
#include<conio.h>
int main()
{
int marks1, marks2. marks3, marks4,total=0;
float avg=0.0;
clrscr();
printf("\n Enter the marks in Mathematics: ");
scanf("%d",&marks1);
printf("\n Enter the marks in Science: ");
scanf("%d",&num2);
printf("\n Enter the marks in Social Science: ");
scanf("%d",&marks3);
printf("\n Enter the marks in Computer Science: ");
scanf("%d",&marks4);

CP Assignment Page 37
total= marks1 + marks2 + marks3 + marks4;
avg=(float)total/4;
printf("\n TOTAL= %d", total);
printf("\n AGGREGATE= %.2f", avg);
if(avg>=75)
printf("\n DISTINCTION");
else if(avg>=60 && avg<75)
printf("\n FIRST DIVISION");
else if(avg>=50 && avg<60)
printf("\n SECOND DIVISION");
else if(avg>=40 && avg<50)
printf("\n THIRD DIVISION");
else
printf("\n FAIL");
return 0;
}
Output:

Enter the marks in Mathematics: 90


Enter the marks in Science: 91
Enter the marks in Social Science: 92
Enter the marks in Computer Science: 93
TOTAL= 366
AGGREGATE= 91.00
DISTINCTION

60. Write a program to calculate the roots of a quadratic equation.

#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int a,b,c;
float D,deno,root1,root2;
clrscr();
printf("\n Enter the values of a,b and c: ");
scanf("%d %d %d",&a,&b,&c);
D = (b*b)-(4*a*c);

CP Assignment Page 38
deno = 2*a;
if(D>0)
{
printf("\n REAL ROOTS");
root1 = (-b+sqrt(D))/deno;
root2 = (-b-sqrt(D))/deno;
printf("\n ROOT1 = %f\t ROOT2 =
%f",root1,root2);
}
else if(D==0)
{
printf("\n EQUAL ROOTS");
root1 = -b/deno;
printf("\n ROOT1=%f\t ROOT2=%f",root1,root2);
}
else
printf("\n IMAGINARY ROOTS");
getch();
}
Output:

Enter the values of a,b and c: 3 4 5


IMAGINARY ROOTS

61. Write a program to demonstrate the use of switch statement without a break.

#include<stdio.h>
int main()
{
int option=1;
switch(option)
{
case 1: printf("\n In case 1");
case 2: printf("\n In case 2");
default: printf("\n In case default");
}
return 0;
}
Output:

CP Assignment Page 39
In case 1
In case 2
In case default

62. Write a program to determine whether an entered character is a vowel or not.

#include <stdio.h>
int main()
{
char ch;
printf(“\n Enter any character:”);
scanf(“%c”,&ch);
switch(ch)
{
case ‘A’:
case ‘a’:
printf(“\n % c is vowel”,ch);
break ;
case ‘E’:
case ‘e’:
printf(“\n %c is a vowel”,ch);
break ;
case ‘I’:
case ‘i’:
printf(“\n %c is a vowel”,ch);
break ;
case ‘O’
case ‘o’
printf(“\n %c is vowel”,ch);
break ;
case ‘U’ :
case ‘u’ :
printf(“\n %c is vowel”,ch);
break ;
default: printf(“\n %c is not a vowel”,ch);
}
return 0;

CP Assignment Page 40
}

Output :

Enter any character : E


E is vowel

63. Write a program to enter a number from 1-17 and display the corresponding day of the
week using switch case statements.

#include <stdio.h>
#include <conio.h>
int main()
{
int day;
clrscr();
printf(“\n Enter any number from 1 to 7: ”);
scanf(“%d”,&day);

switch(day)
{
case 1: printf(“\n SUNDAY”);
break;
case 2: printf(“\n MONDAY”);
break;
case 3: printf(“\n TUESDAY”);
break;
case 4: printf(“\n WEDNESDAY” );
break;
case 5 : printf(“\n THURSDAY” );
break;
case 6 : printf(“\n FRIDAY” );
break;
case 7 : printf(“\n SATURDAY” );
break;
default: printf(“\n wrong number”);
}

CP Assignment Page 41
return 0;
}

Output:

Enter any number from 1 to 7: 5


THURSDAY

64. Write a program that accepts a number from 1 to 10.print whether the number is even or
odd using a switch case construct.

#include <stdio.h>
void main()
{
int num,rem;
printf(“\n Enter any number (1 to 10):”);
scanf(“%”,&num);
rem=num%2;
switch(rem)
{
case 0;
printf(“\n EVEN”);
break;
case 1:
printf(“\n ODD”);
break;
}
}

OUTPUT:

Enter any number from 1 to 10:5


ODD

65.Write a program to calculate the sum of 10 numbers.

CP Assignment Page 42
#include <stdio.h>
int main()
{
int i=1,sum=0;
while(i<10);
{
sum=sum+i;
i=i+1; // condition updated
}
printf(“\n SUM= %d”,sum);
return 0;
}

OUTPUT:

SUM=55

66. Write a program to print 20 horizontal asterisks(*)

#include <stdio.h>
int main()
{
int i=2;
while(i<=20)
{
printf(“*”);
i++;
}
return 0;
}

OUTPUT:

********************
67. Write a program to calculate the sum of numbers from m to n.

#include <stdio.h>
int main()

CP Assignment Page 43
{
int n,m,sum =0;
clrscr();
printf(“\n Enter the value of m:” );
scanf(“%d”,&m);

printf(“\n Enter the value of n:”);


scanf(“%d”,&n);

while(m<=n)
{
sum =sum+m;
m=m+1;
}
printf(“\n SUM=%d”,sum);
return 0;
}

OUTPUT:

Enter the value of m:7


Enter the value of n:11
SUM=45

68. Write a program to display the largest 5 numbers using ternary operator.

#include<stdio.h>
#include<conio.h>
int main()
{
int i=1, large = -32768,num;
clrscr();

while(i<=5);
{
printf(“\n enter the number :”);
scanf(:%d”,&num);
large =num>large>?num:large;

CP Assignment Page 44
i++;
}
printf(“\n the largest of five numbers entered is :
%d”,large);
return 0;
}

OUTPUT:

Enter the number :29


Enter the number :15
Enter the number :17
Enter the number :19
Enter the number :25
The largest of five numbers entered is : 29

69. Write a program to read the numbers until -1 is encountered.also count the
negative,positive and zeros entered by the user.

#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int negatives=0, positives=0,zeros=0;
clrscr();

printf(“\n Enter -1 to exit.”);


printf(“\n\n Enter any number:”);
scanf(“%d”,&num);

while(num != -1)
{
if(num>0)
positives ++;
else if(num<0)
negatives ++;
else
zeroes ++;

CP Assignment Page 45
printf(“\n\n Enter any number :”);
scanf(“%d”,&num);
}
printf(“\n Count of positive number entered
=%d”,positives);
printf(“\n Count of negative number
entered=%d”,negatives);
printf(“\n Count of zeros entered=%d”,zeros);
getch();
return 0;
}

OUTPUT:

Enter -1 to exit
Enter any number:-12
Enter any number:-108
Enter any number:-24
Enter any number:-99
Enter any number:--23
Enter any number:-101
Enter any number:--1
Count of positive numbers entered =3
Count of negative numbers entered=3
Count of zeros entered=0

70. Write a program to calculate the average of numbers entered by the user.

#include <stdio.h>
int main()
{
int num,sum=0,count=0;
float avg;
printf(“\n Enter any number : Enter -1 to STOP:”);
scanf(“%d”,&num);
while(num != -1)
{

CP Assignment Page 46
count++;
sum= sum+num;
printf(“\n enter any number.Enter -1 to
STOP:”);
scanf(“%d”,&num);
}
avg=(float)sum/(count);
printf(“\n SUM=%d”,sum);
printf(“\n AVERAGE=%.2f”,avg);
return 0;
}

OUTPUT:

Enter -1 to exit
Enter any number . Enter -1 to STOP:23
Enter any number . Enter -1 to STOP:13
Enter any number . Enter -1 to STOP:3
Enter any number . Enter -1 to STOP:53
Enter any number . Enter -1 to STOP:4
Enter any number . Enter -1 to STOP:63
Enter any number . Enter -1 to STOP:-23
Enter any number . Enter -1 to STOP:-6
Enter any number . Enter -1 to STOP:-1
SUM=130
AVERAGE=16.25

71. Write a program to calculate the average of first n numbers.

#include <stdio.h>
int main()
{
int n,i=1,sum=0;
float avg =0,0;
printf(“\n Enter the value of n:”);
scanf(“%d”,&n);
do
{

CP Assignment Page 47
sum=sum+1;
i=i+1;
}while(i<=n);
avg=(float) sum/n;
printf(“\n The sum of first %d numbers = %d,n,sum);
printf(“\n The average of first %d numbers
=%2f”,n,avg);
return 0;
}

OUTPUT:

Enter the value of n:18


The sum of first 18 numbers=171
The average of first 18 numbers=9.00

72. Write a program using a do-while loop to display the square and cube of the first n natural
number.

#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
clrscr();

printf(“\n Enter the value of n:”);


scanf(“%d”,&n);
printf(“\n- - - - - - - - - - - - - - - - - - - - -
- - - - - - - -”);
i=1;
do
{
printf(“\n / \t %d \t / \t %d\t |\t%1d \t /”,i,i++
}while(i<=n);
printf(“\n - - - - - - - - - - - - - - - - - - - -
- - - - - - - - -”);
return 0;

CP Assignment Page 48
}

OUTPUT:

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
| 1 | 1 | 1 |
| 2 | 4 | 8 |
| 3 | 9 | 27 |
| 4 | 16 | 64 |
| 5 | 25 | 125|
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

73. Write a program to list all the leap years from 1900 to 1920.

# include<stdio.h>
# include<conio.h>
int main()
{
int m=1900,n=2100;
clrscr();

Do
{
if(((m%4 ==0) && (m%100!=0)))||(m%400==0))
printf(“\n %d is a leap year”,m);
m=m+1;
}while(m<=n);
return 0;
}

OUTPUT:

1904 is a leap year


1908 is a leap year
1912 is a leap year

CP Assignment Page 49
1916 is a leap year
1920 is a leap year

74. Write a program to read a character until a * is encountered.also count the number of
upper case,lower case and number entered.

#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
int lowers=0,uppers=0,numbers=0;
clrscr();

printf(“\n Enter any character:”);


scanf(“%c,&ch);

do
{
if(ch>=’A’ && ch<=’Z’
uppers ++;
if(ch >=’a’ && ch<=’z’)
lower ++;
if(ch >=’0’ && ch=’9’)
numbers ++;

fflush(stdin);
/* The function is used to clear the standard input
file. */
printf(“\n Enter another character. Enter * to
exit.”);
scanf(“%c”,&ch);
} while(ch !=’*’);

printf(“\n Total count of lowercase characters


entered = %d”,lowers);
printf(“\n Total count of uppercase characters
entered=%d”,upper);

CP Assignment Page 50
printf(“\n Total count of numbers entered
=%d”,numbers);
return 0;
}

OUTPUT:
Enter any character:0
Enter another character . Enter *to exit. X
Enter another character . Enter *to exit. F
Enter another character . Enter *to exit. o
Enter another character . Enter *to exit. R
Enter another character . Enter *to exit. d
Enter another character . Enter *to exit. *

Total count of lowercase characters entered: =3


Total count of uppercase characters entered: =3
Total count of numbers entered = 0

75. Write a program to read the numbers until -1 is encountered.Also calculate the sum and
mean of all positive numbers entered and the sum and mean of all negative numbers entered
separately.

#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int sum_negatives=0,sum_positives=0;
int positives =0,negatives =0;
float mean_positives = 0.0;
clrscr();

printf(“\n Enter -1 to exit”);


printf(“\n\n Enter any number:”);
scanf(“%d”,&num);

do
{

CP Assignment Page 51
if (num>0)
{
sum_ positives +=num;
positives ++;
}
else if(num<0)
{
sum_negatives +=num;
negatives++;
}

printf(“\n\n Enter any number: “);


scanf(%d,&num);
}while(num !=-1);

mean_ positive = (float)sum_positives/positives;


mean _negative=(float)sum_negatives/negatives;

printf(“\n sum of all positive numbers entered =


%d”,sum_positives);
printf(“\n sum of all positive numbers entered =
%2.f”,mean_positives);

printf(“\n sum of all negative numbers entered =


%d”,sum_positives);
printf(“\n mean of all negative numbers entered =
%2.f”,sum_positives);
return 0;
}

OUTPUT:

Enter -1 to exit
Enter any number:9
Enter any number:8
Enter any number:7
Enter any number:-6
Enter any number:-5
Enter any number:-4

CP Assignment Page 52
Enter any number:-1
Sum of all positive numbers entered =24
Mean of all positives numbers entered=8.00
Sum of all negatives numbers entered= -15
Mean of all negative numbers entered =-5.00

76. Write a program to print the following pattern.

Pass 1-1 2 3 4 5
Pass 2-1 2 3 4 5
Pass 3-1 2 3 4 5
Pass 4-1 2 3 4 5
Pass 5-1 2 3 4 5

#include <stdio.h>
int main()
{
int i,j
for(i=1;i<=5;i++)
{
printf(“\n Pass %d-”,i);
for(j=1;j<=5;j++)
printf(“%d”,j);
}
return 0;
}
OUTPUT:

Pass 1-1 2 3 4 5
Pass 2-1 2 3 4 5
Pass 3-1 2 3 4 5
Pass 4-1 2 3 4 5
Pass 5-1 2 3 4 5

77. Write a program to print the following pattern

CP Assignment Page 53
**********
**********
**********
**********
**********

#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;1++)
{
printf("\n");
for (j=1;j<=10;j++)
Printf("*");
}
return 0;
}
OUTPUT:

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

78. Write a program to print the following pattern

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

CP Assignment Page 54
#include <stdio.h>
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
printf("\n");
for (j=1;j<=i;j++)
printf("*");
}
return 0;
}
OUTPUT:

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

79.Write a program to print the following pattern

1
12
123
1234
12345

#include<stdio.h>
int main()
{
int i,j;
for((i=1;i<=5;i++)

CP Assignment Page 55
{
printf(“\n”);
for(j=1;j<=i;j++)
printf(“%d”,j);
}
return o;
}
OUTPUT:

1
12
123
1234
12345

80.Write a program to print the following pattern.


1
22
333
4444
55555

#include<stdio.h>
int main()
{
int i,j;
for(i=1;j<=5;i++)
{
printf(“\n”);
for(j=1;j<=i;j++)
printf(“%d”,i);
}
return 0;
}

CP Assignment Page 56
OUTPUT:

1
22
333
4444
55555

81. Write a program to print the following pattern.


0
12
345
6789

#include<stdio.h>
int main ()
{
int i,j,count=0;
for(i=1;i<=4;i++)
{
printf(“\n”);
for(j=1;j<=i;j++)
printf(“%d”,count++);
}
return 0;
}
OUTPUT:

0
12
345
6789

82. Write a program to print the following pattern.


A

CP Assignment Page 57
AB
ABC
ABCD
ABCDE
ABCDEF

#include <stdio.h>
int main()
{
char i, j;
for(i=65;i<=70;i++)
{
printf("\n")
for (j=65;j<=i;j++)
printf("%c", j)
}
return 0;
}
OUTPUT:

A
AB
ABC
ABCD
ABCDE
ABCDEF

83. Write a program to print the following pattern.

1
12
123
1234
12345

CP Assignment Page 58
#include <stdio.h>
#define N 5
int main ()
{
int i, j, k
for(i=1;i<=N;i++)
{
for(k=N;k>=i;k--)
printf(" ");
for(j=1;j<i;j++);
printf("\n");
}
return 0;
}
OUTPUT:

1
12
123
1234
12345

84. Write a program to print the following pattern.


1
121
12321
1234321
123454321

#include <stdio.h>
#define N 5
int main()
{
int i, j, k, 1;

CP Assignment Page 59
for(i=1;i<=N; i++)
{
for (k=N; k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d", j);
for (1-j-2;10;1--)
printf("%d", 1); printf("\n");
}
return 0;
}
OUTPUT:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

85. Write a program to print the following pattern.


1
22
333
4444
55555

#include <stdio.h>
#define N 5
int main()
{
int i, j, k, count=5, c;
for(i=1;i<=N; i++)
{
for (k=1;k<=count; k++)

CP Assignment Page 60
printf(" ");
for(j=1;j<=i;j++)
printf("%.2d", i);
printf("\n");
c--;
}
return 0;
}
Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

86. Write a program to print the multiplication table of n, where n is entered by the user.
#include <stdio.h>
int main()
{
int n, i;
printf("\n Enter any number: ");
scanf("%d", &n);

printf("\n Multiplication table of %d", n);


printf(“\n *************************”);
for(i=0;i<=20;i++)
printf("\n %d X %d = %d", n, i, (n* i));
return 0;
}
Output

Enter any number: 2


Multiplication table of 2

CP Assignment Page 61
2 X 0 = 0
2 X 1 = 2
2 X 20 = 40

87. Write a program using for loop to print all the numbers from m to n, thereby classifying
them as even or odd

#include <stdio.h>
#include <conio.h>
int main()
{
int i, m, n;
clrscr();
printf("\n Enter the value of m: ");
scanf("%d", &m);
printf("\n Enter the value n: ");
scanf("%d", &n);
for (i=m;i<=n;i++)
{
if(i%2==0)
printf("\n %d is even", i);
else
printf("\n %d is odd", i);
}
return 0;
}
Output

Enter the value of m: 5


Enter the value of n: 7
5 is odd
6 is even
7 is odd

CP Assignment Page 62
88. Write a program using for loop to calculate the average of first n natural
numbers.

#include <stdio.h>
#include <conio.h>
int main()
{
int n, i, sum =0;
float avg = 0.0;
clrscr();
printf("\n Enter the value of n: ");
scanf(“%d”,&n);
for (i=1;i<=n;i++)
sum sumi;
avg= (float) sum/n;
printf("\n The sum of first n natural numbers
= %d", sum);
printf("\n The average of first n natural
numbers %.2f", avg);
return 0;
}

Output

Enter the value of n: 10


The sum of first n natural numbers = 55
The average of first n natural numbers = 5.50

89. Write a program using for loop to calculate factorial of a number.

#include <stdio.h>
#include <conio.h>

CP Assignment Page 63
int main()
{
int fact= 1, num;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
if (num == 0)
fact = 1;
else
{
for(int i=1; i<=num;i++) fact fact i;
}
printf("\n Factorial of %d is: %d ", num,
fact);
return 0;
}
Output

Enter the number: 5


Factorial of 5 is: 120

90. Write a program to classify a given number as prime or composite.

#include <stdio.h>
#include <conio.h>
int main()
{
int flag = 0, i, num;
clrscr();
printf("\n Enter any number: ");
scanf("%d", &num);
for(i=2; i<num/2;i++)
{
if(num%i== 0)

CP Assignment Page 64
{
flag =1;
break;
}
}
if(flag == 1)
printf("\n %d is a composite number", num);
else
printf("\n %d is a prime number", num);
return 0;
}
Output

Enter the number: 5


5 is a prime number

91. Write a program using do-while loop to read the numbers until 1 is encountered. Also
count the number of prime numbers and composite numbers entered by the user

#include <stdio.h>
#include <conio.h>
int main()
{
int num, i;
int primes-0, composites=0, flag=0;
clrscr();
printf("\n Enter -1 to exit");
printf("\n\n Enter any number: ");
scanf("%d", &num);
Do
{
for(i=2;i<=num/2;i++)
{
if(num%i==0)

CP Assignment Page 65
{
flag=1;
break;
}
}
if(flag==0)
primes++
else
Composites++;

flag=0;
printf("\n\n Enter any number: ");
scanf("%d",&num);
} while(num != -1);
printf("\n Count of prime numbers entered = %d",
primes);
printf("\n Count of composite numbers entered = %d",
composites);
return 0;
}
Output

Enter -1 to exit
Enter any number: 5
Enter any number: 10
Enter any number: 7
Enter any number: -1
Count of prime numbers entered = 2
Count of composite numbers entered = 1

92. Write a program to calculate pow(x,n)

#include <stdio.h>
#include <conio.h>

CP Assignment Page 66
#include <math.h>
int main()
{
int i, num, n;
long int result =1;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
printf("\n Till which power to calculate: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
result= result* num;
printf("\n pow(%d, %d) = %ld", num, n,result);
return 0;
}
Output

Enter the number: 2


Till which power to calculate: 5
pow(2,5) = 32

93. Write a program to print the reverse of a number.

#include <stdio.h>
#include <conio.h>
int main()
{
int num, temp;
clrscr();
printf("\n Enter the number: ");
scanf("%d", &num);
printf("\n The reversed number is: ");
while(num != 0)
{

CP Assignment Page 67
temp = num%10;
printf("%d", temp);
num = num/10;
}
return 0;
}
Output

Enter the number: 123


The reversed number is: 321

94. Write a program to enter a number and then calculate the sum of its digits.

#include <stdio.h>
#include <conio.h>
int main()
{
int num, temp, sumofdigits = 0;
clrscr();
printf(“\n Enter the number: ”);
scanf(“%d”,&num);
while(num != 0)
{
temp = num%10;
sumofdigits += temp;
num = num/10;
}
printf(“\n The sum of digits = %d”, sumofdigits);
return 0;
}

Output:
Enter the number : 123
The sum of digits = 6

CP Assignment Page 68
95. Write a program to enter a decimal number. Calculate and display the binary equivalent of
this number.
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num, remainder, binary_num=0, i=0;
clrscr();
printf(“\n Enter the decimal number: ”);
scanf(“%d”, &decimal_num);
while(decimal_num != 0)
{
remainder = decimal_num%2;
binary_num += remainder*pow(10,i);
decimal_num = decimal_num/2;
i++;
}
printf(“\n The binary equivalent = %d”,
binary_num);
return 0;
}

Output:

Enter the decimal number: 7


The binary equivalent = 111

96. Write a program to enter a decimal number. Calculate and display the octal equivalent of
this number.

CP Assignment Page 69
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num, remainder, octal_num=0, i=0;
clrscr();
printf(“\n Enter the decimal number: ”);
scanf(“%d”, &decimal_num);
while(decimal_num != 0)
{
remainder = decimal_num%8;
octal_num += remainder*pow(10,i);
decimal_num = decimal_num/8;
i++;
}
printf(“\n The octal equivalent = %d”, binary_num);
return 0;
}

Output:

Enter the decimal number: 18


The octal equivalent = 22

97. Write a program to enter a binary number. Calculate and display the decimal equivalent of
this number.

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{

CP Assignment Page 70
int decimal_num=0, remainder, binary_num, i=0;
clrscr();
printf(“\n Enter the binary number: ”);
scanf(“%d”, &binary_num);
while(binary_num != 0)
{
remainder = binary_num%10;
decimal_num += remainder*pow(2,i);
binary_num = binary_num/10;
i++;
}
printf(“\n The decimal equivalent = %d”,
binary_num);
return 0;
}

Output:

Enter the binary number: 111


The decimal equivalent = 7

98. Write a program to enter an octal number. Calculate and display the decimal equivalent of
this number.

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num = 0, remainder, octal_num, i=0;
clrscr();
printf(“\n Enter the octal number: ”);
scanf(“%d”, &octal_num);

CP Assignment Page 71
while(octal_num != 0)
{
remainder = octal_num%10;
decimal_num += remainder*pow(8,i);
octal_num = octal_num/10;
i++;
}
printf(“\n The decimal equivalent = %d”,
binary_num);
return 0;
}

Output:

Enter the octal number: 22


The decimal equivalent = 18

99. Write a program to enter an hexadecimal number. Calculate and display the decimal
equivalent of this number.

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decimal_num = 0, remainder, hex_num, i=0;
clrscr();
printf(“\n Enter the hexadecimal number: ”);
scanf(“%d”, &hex_num);
while(hex_num != 0)
{
remainder = hex_num%10;
decimal_num += remainder*pow(16,i);

CP Assignment Page 72
hex_num = hex_num/10;
i++;
}
printf(“\n The decimal equivalent = %d”,
binary_num);
return 0;
}

Output:

Enter the hexadecimal number: 39


The decimal equivalent = 57

100. Write a program to calculate GCD of two number.

#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, temp;
int dividend, divisor, remainder;
clrscr();
printf(“\n Enter the first number: ”);
scanf(“%d”, &num1);
printf(“\n Enter the second number: ”);
scanf(“%d”, &num2);
if(num1>num2)
{
dividend = num1;
divisor = num2;
}
else

CP Assignment Page 73
{
dividend = num2;
divisor = num1;
}
while(divisor)
{
remainder = dividend%divisor;
dividend = divisor;
divisor = remainder;
}
printf(“\n GCD of %d and %d is = %d”, num1, num2,
dividend);
return 0;
}

Output:

Enter the first number: 64


Enter the second number: 14
GCD of 64 and 14 is = 2

101. Write a program to sum the series 1+1/2+1/3+...+1/n

#include <stdio.h>
#include <conio.h>
int main()
{
int n, i;
float sum=0.0, a;
clrscr();
printf(“\n Enter the value of n: ”);
scanf(“%d”, &n);

CP Assignment Page 74
for(i=1;i<=n;i++)
{
a=(float)1/i;
sum = sum + a;
}
printf(“\n The sum of the series 1/1 + 1/2 +...+
1/%d = %0.2f”, n, sum);
return 0;
}

Output:

Enter the value of n: 5


The sum of the series 1/1 + 11/2 +...+ 1/5 = 2.28

102. Write a program to sum the series 1/1^2 + 1/2^2 +...+ 1/n^2

#include <stdio.h>
#include <math.h>
#include <conio.h>
{
int n, i;
float sum=0.0, a;
clrscr();
printf(“\n Enter the value of n: ”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
a = (float)1/pow(i,2);
sum = sum + a;
}

CP Assignment Page 75
printf(“\n The sum of series 1/1^2 + 1/2^2 +...+
1/%d^2 = %.2f”, n, sum);
return 0;
}

Output:

Enter the value of n: 5


The sum of series 1/1^2 + 1/2^2 +...+1/5^2 = 1.46

103. Write a program to sum the series 1/2 + 2/3 +...+ n/(n+1)

#include <stdio.h>
#include <conio.h>
int main()
{
int n, i;
float sum=0.0, a;
clrscr();
printf(“\n Enter the value of n: ”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
a = (float)i/(i+1);
sum = sum + a;
}
printf(“\n The sum of series 1/2 + 2/3 +...+
%d/%d^2 = %.f”, n, sum);
return 0;
}

Output:

CP Assignment Page 76
Enter the value of n: 5
The sum of series 1/2 + 2/3 +...+5/6 = 2.681+E

104. Write a program to sum the series 1/1 + 2^2/2 + 3^3/3 +...

#include <stdio.h>
#include <math.h>
#include <conio.h>
int main()
{
int n, NUM, i;
float sum=0.0;
clrscr();
printf(“\n Enter the value of n: ”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
NUM = pow(i,i);
sum += (float)NUM/i;
}
printf(“\n 1/1 + 4/2 + 27/3 +... = %.2f”, sum);
return 0;
}

Output:

Enter the value of n: 5


1/1 + 4/2 + 27/3 +... = 701.00

105. Write a program to calculate sum of cubes of first n numbers.

CP Assignment Page 77
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int i, n;
int term, sum = 0;
clrscr();
printf(“\n Enter the value of n : ”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
term = pow(i,3);
sum += term;
}
printf(“\n 1^3 + 2^3 + 3^3 +... = %d”, sum);
return 0;
}

Output:

Enter the value of n: 5


1^3 + 2^3 + 3^3 +... = 225

106. Write a program to calculate sum of squares of first n even numbers.

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()

CP Assignment Page 78
{
int i, n;
int term, sum = 0;
clrscr();
printf(“\n Enter the value of n : ”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
if(i%2 == 0)
{
term = pow(i,2);
sum += term;
}
}
printf(“\n 2^2 + 4^2 + 6^2 +... = %d”, sum);
return 0;
}

Output:

Enter the value of n: 5


2^2 + 4^2 + 6^2 +... = 20

107. Write a program to find whether the given number is an Armstrong number or not.

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int num, sum=0, r, n;

CP Assignment Page 79
clrscr();
printf(“\n Enter the number: ”);
scanf(“%d”, &num);
n = num;
while(n>0)
{
r=n%10;
sum += pow(r,3);
n=n/10;
}
if(num==sum)
printf(“\n %d is an Armstrong number”, num);
else
printf(“\n %d is not an Armstrong number”, num);
return 0;
}

Output:

Enter the number: 432


432 is not an Armstrong number

108. Write a program using for loop to calculate the value of an investment, given the initial
value of investment and the annual interest. Calculate the value of investment over a period
of time.

#include <stdio.h>
int main()
{
double initVal, futureVal, ROI;
int yrs, i;
printf(“\n Enter the investment value: ”);
scanf(“%lf”, &initVal);

CP Assignment Page 80
printf(“\n Enter the rate of interest: ”);
scanf(“%lf”, &ROI);
printf(“\n Enter the number of years for which
investment has to be done: ”);
scanf(“%d”, &yrs);
futureVal = initVal;
printf(“\n YEAR \t\t VALUE”);
printf(“\n __________________________”);
for(i=1;i<=yrs;i++)
{
futureVal = futureVal * (1 + ROI/100.0);
printf(“\n %d \t\t %lf”, i, futureVal);
}
return 0;
}

Output:

Enter the investment value: 20000


Enter the rate of interest: 12
Enter the number of years for which investment has to
be done: 5
YEAR VALUE
__________________________
1 22400.00
2 25088.00
3 28098.56
4 31470.38
5 35246.83

109. Write a program to generate calender of a month given the start day of the week and the
number of days in that month.

CP Assignment Page 81
#include <stdio.h>
int main()
{
int i, j, startDay, num_of_days;
printf(“\nEnter the start day of the week(1 to
7):”);
scanf(“%d”, &startDay);
printf(“\n Enter the number of days in that
month:”);
scanf(“%d”, &num_of_days);
printf(“Sun \t Mon \t Tues \t Wed \t Thurs \t Fri
\t Sat \n”);
printf(“\n
_______________________________________________________
__ \n”);
for(i=0;i<startDay-1;i++)
printf(“ ”);
for(j=1;j<=num_of_days;j++)
{
if(i>6)
{
printf(“\n”);
i=1;
}
else
i++;
printf(“%2d \t”, j);
}
return 0;
}

Output:

CP Assignment Page 82
Enter the starting day of the week(1 to 7):5
Enter the number of days in that month:31
Sun Mon Tue Wed Thurs Fri
Sat
_______________________________________________________
__
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24

25 26 27 28 29 30 31

110. Write a program to calculate the square root of a number.

#include <stdio.h>
#include <math.h>
int main()
{
int num;
do
{
printf("\nEnter any number. Enter 999 to
stop:");
scanf("%d", &num);
if(num==999)
break;
if(num<0)
{
printf("\n Square root of negative number
is not defined");
continue;
}
printf("\n The square root of %d is %f", num,
sqrt(num));
}

CP Assignment Page 83
while(1);
return 0;
}

Output:

Enter any number. Enter 999 to stop:25


The square root of 25 is 5.0000000
Enter any number. Enter 999 to stop:999

111.Find the output of the following.


#include <stdio.h>
int main()
{
    int a = 2, b = 3, c = 4;
    if( c!= 100)
    a = 10;
    else
    b = 10;
    if(a + b > 10)
    c = 12;
    a = 20;
    b = ++c;
    printf(" \n a = %d \t b = %d \t c = %d" , a, b, c);
    return 0;
}

OUTPUT:

a = 20 b = 13 c = 13

112.Find the output of the following.

#include <stdio.h>
int main()
{
    int a = 2, b = 3, c = 4;
    if(b==2)
    a = 10;
    else

CP Assignment Page 84
    c = 10;
    printf(" \n a = %d \t b = %d \t c = %d" , a, b, c);
    return 0;
}

OUTPUT:

a = 2 b = 3 c = 10

113.Find the output of the following.


#include <stdio.h>
int main()
{
    int a = 2, b = 3, c = 4;
    if(a&&b)
    c=10;
    else
    c=20;
    printf(" \n a = %d \t b = %d \t c = %d" , a, b, c);
    return 0;
}
OUTPUT:
a = 2 b = 3 c = 10
114.Find the output of the following.
#include <stdio.h>
int main()
{
    int a = 2, b = 3, c = 4;
    if(a || b || c)
    c=10;
    else
    c=20;
    printf(U" \n a = %d \t b = %d \t c = %d" , a, b,
c);
    return 0;
}

OUTPUT:
a = 2 b = 3 c = 10
115.Find the output of the following.
#include <stdio.h>

CP Assignment Page 85
int main()
{
    int a = 2, b = 3, c = 4;
    if(a)
    if(b)
    c=10;
    else
    c=20;
    printf(" \n a = %d \t b = %d \t c = %d" , a, b, c);
    return 0;
}

OUTPUT:
a = 2 b = 3 c = 10

116.Find the output of the following.


#include <stdio.h>
int main()
{
    int a = 2, b = 3, c = 4;
    if(a == 0 || b >= c && c > 0)
    if(a && b)
    c=10;
    else
    c=20;
    printf(" \n a = %d \t b = %d \t c = %d" , a, b, c);
    return 0;
}

OUTPUT:
a = 2 b = 3 c = 4
117.Find the output of the following.
#include <stdio.h>
int main()
{
    int a = 2, b = 3, c = 4;
    if( a= b)
    c++;
    printf(" \n a = %d \t b = %d \t c = %d" , a, b, c);
    return 0;
}

OUTPUT:

CP Assignment Page 86
a = 3 b = 3 c = 5
118.Find the output of the following.
#include <stdio.h>
int main()
{
    int a = 2, b = 3, c = 4;
    if(a = b < c)
    {
        c++;
        a--;
    }
    ++b;
    printf(" \n a = %d \t b = %d \t c = %d", a, b, c);
    return 0;
}

OUTPUT:
a = 0 b = 4 c = 5
119.Find the output of the following.
#include <stdio.h>
int main(){
    char ch;
    printf("Enter ");
    scanf("%c",&ch);
    switch(ch)
    {
            case 'a':
            case 'A':
            printf("\n A");
            case 'b':
            case 'B':
            printf("\n B");
            default:
            printf("\n DEFAULT");
    }
}
OUTPUT:

Enter A

CP Assignment Page 87
B
DEFAULT
120.Find the output of the following.

#include <stdio.h>
int main(){
    char ch;
    printf("Enter ");
    scanf("%c",&ch);
    switch(ch)
    {
            case 'a':
            case 'A':
            printf("\n A");
            case 'b':
            case 'B':
            printf("\n B");
            break;
            default:
            printf("\n DEFAULT");
    }
}

OUTPUT:

Enter B

B
121.Find the output of the following.
#include <stdio.h>
int main(){
    char ch;
    printf("Enter ");
    scanf("%c",&ch);
    switch(ch)
    {
            case 'a':
            printf("\n a");
            break;
            case 'A':
            printf("\n A");
            break;
            case 'b':
            printf("\n b");

CP Assignment Page 88
            break;
            case 'B':
            printf("\n B");
            break;
            default:
            printf("\n DEFAULT");
    }
}

OUTPUT:

Enter a

122.Find the output of the following.

#include<stdio.h>

void main()

int num = 10;

printf("\n %d , a>100");

OUTPUT:

4200768 , a>100

123.Find the output of the following.

#include<stdio.h>

void main()

CP Assignment Page 89
printf("HELLO");

if (-1)

printf("WORLD");

OUTPUT:

HELLOWORLD

124.Find the output of the following.

void main()

int a = 3;

if(a < 10)

printf("\n LESS");

if(a < 20)

printf("\n LESS");

if(a < 30)

printf("\n LESS");

OUTPUT:

LESS

LESS

LESS

CP Assignment Page 90
125.Find the output of the following.

#include<stdio.h>

void main()

int a=10, b=20, c=30, d=40;

if(c < d)

if (c < b)

printf("\n c");

else if(a < c)

printf("\n a");

if(a > b)

printf("\n b");

else

printf("\n d");

OUTPUT:

126.Find the output of the following.

#include<stdio.h>

void main()

CP Assignment Page 91
{

char ch = 'Y';

switch(ch)

default:

printf("\n YES OR NO");

case 'Y':

printf("YES");

break;

case 'N':

printf("NO");

break;

OUTPUT:
YES

127.Find the output of the following.

#include<stdio.h>

int main()

int num=10;

for(num++num++num++)

CP Assignment Page 92
printf("%d", num);

return 0;

OUTPUT:

128.Find the output of the following.

#include<stdio.h>

int main()

int num=10;

for(;--num;)

printf("%d",num);

return 0;

OUTPUT:

987654321

129.Find the output of the following.

#include<stdio.h>

int main()

int num=10;

for(; !num;num++)

printf("%d",num);

CP Assignment Page 93
return 0;

OUTPUT:

130.Find the output of the following.

#include<stdio.h>

int main()

float PI = 3.14, area;

int r = 7;

while(r>=0)

area = PI*r*r;

printf("\n Area = %f",area);

return 0;

OUTPUT:

Area = 153.860016

Area = 153.860016

Area = 153.860016

Area = 153.860016

Area = 153.860016

Area = 153.860016

CP Assignment Page 94
131.Find the output of the following.

#include<stdio.h>

void main()

int i=0,n=10;

while(i==0)

if(n<10)

break;

n--;

printf("\n i=%d and n=%d",i,n);

OUTPUT:

i=0 and n=9

132.Find the output of the following.

#include<stdio.h>

void main()

int i=0;

do{

CP Assignment Page 95
printf("\n %d",i);

i++;

}while(i<=0);

printf("\n STOP");

OUTPUT:

STOP

133.Find the output of the following.

#include<stdio.h>

void main()

int i,j;

for(i=0;i<=10;i++)

printf("\n");

for(j=0;j<=10;j++)

printf("");

printf("\n %d",j);

CP Assignment Page 96
OUTPUT:

11

11

11

11

11

11

11

11

11

11

11

134.

#include<stdio.h>

void main() {

int num=10;
printf("\n%d",num>10);

}
Output:

135.

#include<stdio.h>

CP Assignment Page 97
void main() {

printf("HELLO");
if (!1)
{
printf("WORLD");
}

}
Output:

136.

#include<stdio.h>

void main() {

int x=-1;
unsigned int y=1;
if(x,y)
printf("\n SMALL");
else
printf("\n LARGE");

}
Output:

CP Assignment Page 98
137.

#include<stdio.h>

void main() {

char ch=-63;
int num=-36;
unsigned int unum=-18;
if(ch>num)
{
printf("A");
if(ch>unum)
printf("B");
else
printf("C");

}
else
{
printf("D");
if(num<unum)
printf("E");
else
printf("F");
}

}
Output:

CP Assignment Page 99
138.

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

int num=10;
for(;++num;num-=2)
printf("%d",num);
return 0;
}
Output:

139.

#include<stdio.h>

int main() {

int num=10;
for(;;)
printf("HI!!");
return 0;
}
Output:

CP Assignment Page 100


140.

#include<stdio.h>

int main() {

int num=10;
for(num=100;num<=100;num++)
printf("%d",num);
return 0;
}
Output:

141.

#include<stdio.h>

int main() {

while(1)
printf("Hi");
return 0;
}
Output:
Hi till infinity

142.

#include<stdio.h>

int main() {

int i=0;

CP Assignment Page 101


char c='O';
while (i<10)
{
printf("%c",c+i);
i++;
}

return 0;
}
Output:

143.

#include<stdio.h>

void main() {

int i=0;
do
{
if(i>10)
continue;
i++;
} while (i<20);
printf("\n i=%d",i);
}
Output:

144.

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

CP Assignment Page 102


int i=1;
for(;i<=1;i++)
{
printf("\n %d",i);
printf("\n STOP");
}
}
Output:

145.

#include<stdio.h>

void main() {

int i,j;
for(i=10;i>=0;i--)
{
printf("\n");
for(j=i;j>=0;j--)
printf("%d",j);
}
}
Output:

CP Assignment Page 103


146. Write a program to add two integers using functions

#include<stdio.h>
int sum(int a, int b);
int main()
{
int num1, num2, total=0;
printf("\n Enter the first number:");
scanf("%d",&num1);
printf("\n Enter the second number:");
scanf("%d",&num2);
total=sum(num1,num2);
printf("\n total=%d",total);
return 0;
}
int sum(int a, int b)
{
int result;
result=a+b;
return result;
}

Output:

Enter the first number:20

CP Assignment Page 104


Enter the second number:30

total=50

147. Write a program to return value in functions.

#include<stdio.h>
#include<conio.h>
int check_relation(int a, int b);
int main()
{
int a=3, b=5, res;
res=check_relation(a, b);
if(res==0)
printf("\n EQUAL");
if(res==1)
printf("\n a is greater than b");
if(res==-1)
printf("\n a is less than b");
getch();
return 0;
}

int check_relation(int a, int b)


{
if(a==b)
return 0;
else if(a>b)
return 1;
else
return -1;
}

Output:

a is less than b

CP Assignment Page 105


148. Write a program to check the call by value function

#include<stdio.h>
void add(int n);
int main()
{
int num=2;
printf("\n The value of num before the function
calling = %d",num);
add(num);
printf("\n The value of num after the function
calling = %d",num);
return 0;
}

void add(int n)
{
n=n+10;
printf("\n The value of num in the called function
= %d",n);
}

Output:
The value of num before the function calling = 2
The value of num in the called function = 12
The value of num after the function calling = 2

149. Write a program to return value in the call by value function.

#include<stdio.h>
int add(int n);
int main()
{
int num=2;
printf("\n The value of num before the function
calling = %d",num);
num=add(num);

CP Assignment Page 106


printf("\n \n The value of num after the function
calling = %d",num);
return 0;
}

int add(int n)
{
n=n+10;
printf("\n\n The value of num in the called
function = %d",n);
return n;
}

Output:
The value of num before the function calling = 2

The value of num in the called function = 12

The value of num after the function calling = 12

150. Write a program for Call by Reference function.

#include<stdio.h>
void add(int *n);
int main()
{
int num=2;
printf("\n The value of num before the function calling
= %d",num);
add(&num);
printf("\n \n The value of num after the function
calling = %d",num);
return 0;
}

void add(int *n)

CP Assignment Page 107


{
*n=*n+10;
printf("\n\n The value of num in the called
function = %d",*n);
}

Output:
The value of num before the function calling = 2
The value of num in the called function = 12
The value of num after the function calling = 12

151. Write a function to swap the values of two variables.

#include<stdio.h>
void swap_call_by_val(int, int);
void swap_call_by_ref(int *, int *);
int main()
{
int a=1, b=2, c=3,d=4;
printf("\n In main(), a = %d and b = %d",a, b);
swap_call_by_val(a, b);
printf("\n In main(), a = %d and b = %d",a, b);
printf("\n\n In main(), d = %d and b = %d",c, d);
swap_call_by_ref(&c, &d);
printf("\n In main(), c = %d and d = %d",c,d);
return 0;
}

void swap_call_by_val(int a, int b)


{
int temp;
temp = a;
a = b;
b = temp;
printf("\n In function (Call By Value Method) a =
%d and b = %d",a,b);
}

CP Assignment Page 108


void swap_call_by_ref(int *c, int *d)
{
int temp;
temp = *c;
*c = *d;
*d = temp;
printf("\n In function (Call By Reference Method) c
= %d and d = %d", *c, *d);
}

Output:

In main(), d = 3 and b = 4
In function (Call By Reference Method) c = 4 and d = 3
In main(), c = 4 and d = 3

152.Write a program to find biggest of three integers using a function.

#include <stdio.h>
int greater(int a, int b, int c);
int main()
{
int num1, num2, num3, large;
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n Enter the second number: ");
scanf("%d",&num2);
printf("\n Enter the third number: ");
scanf("%d",&num3);

large=greater(num1, num2, num3);


printf("\n Largest number = %d",large);
return 0;
}

int greater(int a, int b, int c)


{
if(a>b && a>c)
return a;

CP Assignment Page 109


if(b>a && b>c)
return b;
else
return c;
}

Output:
Enter the first number: 45

Enter the second number: 23

Enter the third number: 34

Largest number = 45

153.Write a program to calculate area of circle using function.

#include <stdio.h>
float cal_area(float r);
int main()
{
float area, radius;
printf("\n Enter the radius of the circle: ");
scanf("%f",&radius);
area=cal_area(radius);
printf("\n Area of the circle with radius %f =
%f",radius,area);
return 0;
}

float cal_area(float radius)


{
return(3.14*radius*radius);
}

Output:
Enter the radius of the circle: 25

CP Assignment Page 110


Area of the circle with radius 25.000000 = 1962.500000

154. Write a program to convert time to minutes.

#include <stdio.h>
int convert_time_in_mins(int hrs,int minutes);
int main()
{
int hrs, minutes, total_mins;
printf("\n Enter hours and minutes: ");
scanf("%d %d",&hrs, &minutes);
total_mins=convert_time_in_mins(hrs, minutes);
printf("\n Total minutes = %d",total_mins);
return 0;
}

int convert_time_in_mins(int hrs,int mins)


{
mins=hrs*60+mins;
return (mins);
}

Output:
Enter hours and minutes: 4 20
Total minutes = 260

155. Write a program to calculate P(n/r).

#include <stdio.h>
int Fact(int);
int main()
{
int n,r;
float result;
printf("\n Enter the value of n: ");
scanf("%d",&n);
printf("\n Enter the value of r: ");

CP Assignment Page 111


scanf("%d",&r);
result=(float) Fact(n)/Fact(r);
printf("\n P(n/r): p(%d)/(%d) = %.2f",n,r,result);
return 0;
}

int Fact(int num)


{
int f=1, i;
for(i=num;i>=1;i--)
f = f*i;
return f;
}

Output:
Enter the value of n: 4

Enter the value of r: 2

P(n/r): p(4)/(2) = 12.00

156. Write a program to calculate C(n/r).

#include <stdio.h>
int Fact(int);
int main()
{
int n,r;
float result;
printf("\n Enter the value of n: ");
scanf("%d",&n);
printf("\n Enter the value of r: ");
scanf("%d",&r);
result=(float) Fact(n)/(Fact(r)*Fact(n-r));
printf("\n P(n/r): p(%d)/(%d) = %.2f",n,r,result);
return 0;
}

CP Assignment Page 112


int Fact(int num)
{
int f=1, i;
for(i=num;i>=1;i--)
f = f*i;
return f;
}

Output:
Enter the value of n: 4

Enter the value of r: 2

P(n/r): p(4)/(2) = 6.00

157. Write a program to sum the series- 1/1!+1/2!+1/3!+....+1/n!.

#include <stdio.h>
int Fact(int);
int main()
{
int n,f,i;
float result=0.0;
printf("\n Enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=Fact(i);
result += 1/(float)f;
}
printf("\n The sum of the series
1/1!+1/2!+1/3!+.... = %.2f",result);
return 0;
}

int Fact(int num)

CP Assignment Page 113


{
int f=1, i;
for(i=num;i>=1;i--)
f = f*i;
return f;
}

Output:
Enter the value of n: 5
The sum of the series 1/1!+1/2!+1/3!+.... = 1.72

158. Write a program to sum the series- 1/1!+4/2!+27/3!+.....

#include <stdio.h>
#include<math.h>
int Fact(int);
int main()
{
int n,i,num,deno;
float sum=0.0;
printf("\n Enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
num=pow(i,i);

deno=Fact(i);

sum += (float)num/deno;
}
printf("\n The sum of the series
1/1!+4/2!+27/3!+.... = %.2f",sum);
return 0;
}

int Fact(int n)
{
int f=1, i;
for(i=n;i>=1;i--)

CP Assignment Page 114


f = f*i;
return f;
}

Output:
Enter the value of n: 5
The sum of the series 1/1!+4/2!+27/3!+.... = 44.21

CP Assignment Page 115

You might also like