0% found this document useful (0 votes)
40 views116 pages

Solution of Programs

This document contains a series of programming assignments written in C for a college course. Each program addresses different programming concepts such as basic input/output, mathematical operations, conditionals, and loops. The assignments include tasks like calculating the circumference of a circle, performing arithmetic operations, and checking for even/odd numbers.

Uploaded by

aslamsaira437
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)
40 views116 pages

Solution of Programs

This document contains a series of programming assignments written in C for a college course. Each program addresses different programming concepts such as basic input/output, mathematical operations, conditionals, and loops. The assignments include tasks like calculating the circumference of a circle, performing arithmetic operations, and checking for even/odd numbers.

Uploaded by

aslamsaira437
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/ 116

1

Reader Group of College, SARGODHA


CAMPUS
Department of BS IT - Term 1

Programming in C
Grand Assignment

Name: Faza-ul-Rehman
Registration Number: 200141
Instructor: Sir. Irfan

Friday, April 2, 2021


2

Simple. C program
Program no 1:
Write a program that display Hello world on screen.
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hello Word");
return 0;
}
Program no 2:
Write a program that inputs the radius of a circle from user and find
its circumference using formula 2*3.14*r*r. ( store the value of pi in
a constant by using DEFINE directive)
#include<stdio.h>
#include<conio.h>
#define PI 3.14
int main()
{
float r,c;
printf("enter radius of circle:\n");
scanf("%f",&r);
c=PI*r*r;
printf("%f is circumfarance",c);
return 0;

Friday, April 2, 2021


3

}
Program no 3:
Write a program that take two integer from user and perform all
mathematical operations.
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("enter two integer:\n");
scanf("%d%d",&a,&b);
printf("sum=%d\n",a+b);
printf("sub=%d\n",a-b);
printf("mul=%d\n",a*b);
printf("Divide=%d\n",a/b);
printf("Mode=%d\n",a%b);
return 0;
}
Program no 4:
Write a program that take a number from user and perform all
compound operations.
#include<stdio.h>
#include<conio.h>
int main()
{

Friday, April 2, 2021


4

int c;
printf("enter a integer:\n");
scanf("%d",&c);
printf("%d\n",c+=2);
printf("%d\n",c-=2);
printf(“%d\n",c*=2);
printf("%d\n",c/=2);
printf("%d\n",c%=2);
return 0;
}
Program no 5
Write a program that explain the working of postfix and prefix
increment operator (used as independent expression)
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,x,y;
a=b=x=y=0;
a++;
b=a;
++x;
y=x;
printf("a=%d\nb=%d\n",a,b);
printf("x=%d\ny=%d\n",x,y);

Friday, April 2, 2021


5

return 0;
}
Program no 6
Write a program that explain the working of postfix and prefix
increment operator (used as part of larger expression)
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,x,y;
a=b=x=y=0;
b=a++; //larger expration,bcz more than two operations
y=++x;
printf("a=%d\nb=%d\n",a,b);
printf("x=%d\ny=%d\n",x,y);
return 0;
}
Program no 7
Write a program that take two integer number from user and
perform division on them.( type casting).
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;

Friday, April 2, 2021


6

printf("enter two numbers:\n");


scanf("%d%d",&a,&b);
float divide;
divide= a / (float) b;
printf("%d is =%f",a,b,divide);
return 0;
}
Program no 8
Write a program that take two floating point number from user and
show the remainder ( use explicit type casting ).
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("enter two numbers:\n");
scanf("%d%d",&a,&b);
float divide;
divide= (float) a / (float) b;
printf("%f",divide);
return 0;
}
Program no 9

Friday, April 2, 2021


7

Write a program that calculate the simple interest. User enter


Amount , Rate of Interest and number of year. And program display
Amount of Interest .
Formula................. interest =( amount* rate * time) /100
#include<stdio.h>
#include<conio.h>
int main()
{
int intrest,a,r,t;
printf("Enter Amount\n:");
scanf("%d", &a);
printf("Enter rate of intrest\n:");
scanf("%d", &r);
printf("Enter time or year\n:");
scanf("%d", &t);
intrest=(a*r*t)/100;
printf("%d is simple intrest");

return 0;
}
Program no 10
Write a program that inputs a character from user and display its
ASCII code.
#include<stdio.h>
#include<conio.h>

Friday, April 2, 2021


8

int main()
{
char ch;
printf("Enter a Chracter");
scanf("%c",&ch);
printf("%d is ASCII code of chracter ",ch);
return 0;
}
Program no 11
Write a program that inputs two numbers from user , show on screen
, swap their values and display again on screen. ( using third variable
)
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter two numbers:");
scanf("%d %d",&a&,b);
c=a;
a=b;
b=c;
printf("Value after swapping is %d %d",a,b);
return 0;
}

Friday, April 2, 2021


9

Program no 12
Write a program that input two numbers from user , swap their
values ( without using third variable).
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("%d %d",a,b);
return 0;
}
Program no 13
Write a program that inputs the distance traveled and speed of a car.
Program calculate and display the time required to reach the
destination . ( time = distance /speed )
#include<stdio.h>
#include<conio.h>
int main()
{
int dis,sp;

Friday, April 2, 2021


10

printf("Enter travel Distance");


scanf("%d",&dis);
printf("Enter speed of a Car");
scanf("%d",&sp);
int time;
time= dis/sp;
printf("%d is the time",time);
return 0;
}
Program no 14
Write a program that inputs Base and Height of a triangle from user
. calculate and displays the area of triangle. ( area = 1⁄2 * base * height
)
#include<stdio.h>
#include<conio.h>
int main()
{
int base,height;
float area;
printf("Enter base and height of a triangle");
scanf("%d %d",base,height);
area=0.5*base*height;
printf("%d is area",area);
return 0;
}

Friday, April 2, 2021


11

Program no 15
Write a program that inputs time in seconds and convert it into
HH:MM:SS format.
#include<stdio.h>
#include<conio.h>
int main()
{
int sec,hr,min,s;
printf("Enter seconds");
scanf("%d",sec);
hr=sec/3600;
min=sec/60;
sec=sec%3600;
s=sec%60;
printf("Time in HH-MM-SS %d %d %d",hr,min,s);
return 0;
}
Program no 16
Write a program that input temperature from the user in Celsius and
converts it into
Fahrenheit using formula F= 9 / 5 * C + 32
#include<stdio.h>
#include<conio.h>
int main()
{

Friday, April 2, 2021


12

float cel,f;
printf("Enter temp in celcius");
scanf("%f",&cel);
f=9/5*cel+32;
printf("%f",f);
return 0;
}
Program no 17
Write a program that input the height of a person in Inches and
convert it in
centimeters ( c = I * 2.54)
#include<stdio.h>
#include<conio.h>
int main()
{
float h,c;
printf("Enter height in inches");
scanf("%f",&h);
c=h*2.54;
printf("%f is height in cm",c);
return 0;
}
Program no 18
Write a program that inputs a three-digit-number from user and
display in reverse

Friday, April 2, 2021


13

order ( input 345 , output 543 )


#include<stdio.h>
int main()
{
int n,num,temp;
printf("enter three_digit_number:\n");
scanf("%d",&n);
num=0;
while(n!=0)
{
temp=n%10;
num=num*10;
num=num+temp;
n=n/10;
}
printf("rev number is %d",num);
return 0;
}
Program no 19
Write a program that inputs a 5 digit number from user and display
in reverse order.
#include<stdio.h>
int main()
{
int n,num,temp;

Friday, April 2, 2021


14

printf("enter three_digit_number:\n");
scanf("%d",&n);
num=0;
while(n!=0)
{
temp=n%10;
num=num*10;
num=num+temp;
n=n/10;
}
printf("rev number is %d",num);
return 0;
}
Program no 20
Write a program that inputs number of hours from user and convert
it in Week: Days: Hours format.
#include<stdio.h>
int main()
{
int hours,days,weeks;
printf("Enter number of hours:\n");
scanf("%d",&hours);
weeks=hours/(24*7);
days=hours /24;
hours=hours-24*days;

Friday, April 2, 2021


15

printf("weeks=%d\ndays=%d\nhours=%d",weeks,days,hours);
return 0;
}
Simple IF Statement

1. Write a program that take a number from user and check whether
it is even or odd.
#include<stdio.h>
int main()
{
int a;
printf("enter a numbers:");
scanf("%d",&a);
if(a%2==0)
printf("even");
if(a%2!=0)
printf("odd");
return 0;
}
2. Write a program that take a number from user and check whether
it is positive, negative or zero.
#include<stdio.h>
int main()
{
int a;

Friday, April 2, 2021


16

printf("enter a numbers:");
scanf("%d",&a);
if(a>0)
printf("%d is positive",a);
if(a<0)
printf("%d is negative",a);
if(a==0)
printf("%d is zero",a);
return 0;
}
3. Write a program that take two numbers from user and display
maximum number on screen.
#include<stdio.h>
int main()
{
int y,z;
printf("enter two numbers:\n");
scanf("%d%d",&y,&z);
if(y>z)
printf("%d is max",y);
if(z>y)
printf("%d is max",z);
return 0;
}

Friday, April 2, 2021


17

4. Write a program that take three numbers from user and display
maximum number on screen.
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter three numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
printf("%d is max",a);
if(b>a&&b>c)
printf("%d is max",b);
if(c>a&&c>b)
printf("%d is max",c);
return 0;
}
5. Write a program that take three numbers from user and display
minimum
number on screen.
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter three numbers:\n");
scanf("%d%d%d",&a,&b,&c);

Friday, April 2, 2021


18

if(a<b&&a<c)
printf("%d is min",a);
if(b<a&&b<c)
printf("%d is min",b);
if(c<a&&c<b)
printf("%d is min",c);
return 0;
}
6. Write a program that take 5 numbers from user and display
Largest and Smallest integer .
#include<stdio.h>
int main()
{
int a,b,c,d,e;
int max,min;
printf("enter five numbers:\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
if(e>max)

Friday, April 2, 2021


19

max=e;
printf("%d is max\n",max);
min=a;
if(b<max)
min=b;
if(c<max)
min=c;
if(d<max)
min=d;
if(e<max)
min=e;
printf("%d is min",min);
return 0;
}

IF ELSE

7. Write a program that inputs a year from user and display leap year
or not.
#include<stdio.h>
int main()
{
int year;
printf("enter ayear:");
scanf("%d",&year);

Friday, April 2, 2021


20

if(year%4==0)
printf("year is leap");
else
printf("year is not leap");
return 0;
}
8. Write a program that input salary and grade of an employee. It
adds 50% bonus if grade is greater then 15 , it adds 25% bonus if
grade is 15 or less.
Display total salary.
#include<stdio.h>
int main()
{
int salary,grade,bonus;
int total_salary;
printf("enter a salary:\n");
scanf("%d",&salary);
printf("entar a grade:");
scanf("%d",&grade);
if(grade>15)
bonus=(salary*50)/100;
else
bonus=salary*25/100;
total_salary=salary+bonus;
printf("%d",total_salary);

Friday, April 2, 2021


21

return 0;
}
9. Write a program that inputs two integers. It determines and prints
if the first number is a multiple of second number.
#include<stdio.h>
int main()
{
int a,b;
printf("enter two numbers:\n");
scanf("%d%d",&a,&b);
if(a%b==0)
printf("%d number is multiple of %d",a,b);
else
printf("%d isnumber is not multiple of %d",a,b);
return 0;
}
10. Write a program that take name of two students from user. It
check and display whether both are same or not.
#include<stdio.h>
#include<string.h>
int main()
{
char a[10],b[10];
printf("enter two number:\n");
scanf("%s%s",&a,&b);

Friday, April 2, 2021


22

if(strcmp(a,b)==0)
printf("same name");
else
printf("not same name");
return 0;
}

IF ELSE IF

11.Write a program that inputs test marks of a student and displays


his grade
according to the following criteria.
Marks Grade
> = 90 A
80 – 89 B
70-79 C
60-69 D
< 60 F

#include<stdio.h>
int main()
{
int marks;
printf ("enter marks:");
scanf("%d",&marks);

Friday, April 2, 2021


23

if(marks>=90)
printf("grade is A");
else if(marks>=80)
printf("grade is B");
else if(marks>=70)
printf("grade is C");
else if(marks>=60)
printf("grade is D");
else
printf("grade is F");
return 0;
}
12. Write a program that calculate the electricity bill. The rates of
electricity per
unit are as follow.
Units < = 300 Rs 2 per unit
Units > 300 and <= 500 Rs 5 per unit
Units > 500 Rs 7 per unit
A line rent RS 150 is also added to the total bill and a surcharge of
5% extra if
the bill is exceeds RS . 2,000. Calculate total bill.
#include<stdio.h>
int main()
{
int unit,bill;

Friday, April 2, 2021


24

printf("enter unit consumed:\n");


scanf("%d",&unit);
if(unit>500)
{
bill=unit*7;
}
else if(unit>300)
{
bill=unit*5;
}
else
{
bill=unit*2;
}
bill=bill+150;
int extrabill=0;
int totalbill;
if(bill>2000)
{
extrabill=(bill*5)/100;
}
totalbill=bill+extrabill;
printf("%d",totalbill);

Friday, April 2, 2021


25

return 0;
}
13. Write a program that inputs radius and user’s choice . it calculates
area of circle if user enters 1 as choice. It calculates circumference if
the user enters 2
as choice.
#include<stdio.h>
#define PI 3.141
int main()
{
int choice;
float radius,area,circumfrence;
printf("enter a choice 1 area and 2 for circumferencr:\n");
scanf("%d",&choice);
printf("enter a radius:\n");
scanf("%f",&radius);
if(choice==1)
{
area=PI*radius*radius; //area=PI*r*r
printf("%f",area);
}
else if(choice==2)
{
circumfrence=2*PI*radius; //cir=2*PI*r
printf("%f",circumfrence);

Friday, April 2, 2021


26

}
else
printf("invalid input");
return 0;
}

NESTED IF ELSE

14. Write a program that take 3 integers from user. And display
smallest integer.
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter three numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a<b)
{
if(a<c)
printf("%d is smaller",a);
else
printf("%d is smaller",c);
}
else
{

Friday, April 2, 2021


27

if(b<c)
printf("%d is smaller",b);
else
printf("%d is smaller",c);
}
return 0;
}
15. Write a program that take three numbers fro user. And display
whether all are equal or not.
#include<stdio.h>
int main()
{
int x,y,z;
printf("enter three numbers:\n");
scanf("%d%d%d",&x,&y,&z);
if(x==y)
{
if(x==z)
printf("numbers are equall");
else
printf("number are different");
}
else
printf("number are different");
return 0;

Friday, April 2, 2021


28

COMPOUND CONDITION

16. Write a program that inputs three number from user and display
maximum number using logical operator.
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter three numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
{

Aqsa Ashraf BS IT-1 200123

printf("%d is max",a);

}
if(b>a&&b>c)
{

printf("%d is max",b);

Friday, April 2, 2021


29

}
else
{
printf("%d is max",c);
}
return 0;
}
17.Write a program that inputs a character from user and display
whether it is Vowel or not using logical operator.
#include<stdio.h>
int main()
{
char n;
printf("enter a chararacter:");
scanf("%c",&n);
if(n=='a'||n=='e'||n=='i'||n=='o'||n=='u')
printf("you enter vowel");
else
printf("you enter a consonent");
return 0;
}
18. Write a program that allows the user to enter any character
through the key board and determines whether it is a capital letter,
small case letter, a digit or
a special symbol.

Friday, April 2, 2021


30

#include<stdio.h>
int main()
{
char input;
printf("enter any character:\n");
scanf("%c",&input);
if(input>='A'&&input<='Z')
printf("you enter capital letter");
else if(input>='a' && input<='z')
printf("you enter small letter");
else if(input>='0' && input<='9')
printf("you enter a digit");
else
printf("you enter special symbol");
return 0;
}
19.Write a program that inputs a number from user and display even
or odd using not operator.
#include<stdio.h>
int main()
{
int num;
printf("enter a number:");
scanf("%d",&num);
if(!(num%2==0))

Friday, April 2, 2021


31

printf("you enter odd number");


else
printf("you enter even number");
return 0;
}
SWITCH

20. Write a program that inputs number of week’s day and displays
the name of the day. 1 Friday 2 Saturday 3 Sunday ............
#include<stdio.h>
int main()
{
int days;
printf("enter number of day weeks:\n");
scanf("%d",&days);
switch(days)
{
case 1:
printf("friday");
break;
case 2:
printf("saturday");
break;
case 3:
printf("sunday");

Friday, April 2, 2021


32

break;
case 4:
printf("monday");
break;
case 5:
printf("tuesday");
break;
case 6:
printf("wednesday");
break;
case 7:
printf("thursday");
break;
default:
printf("valid input");
}
return 0;
}
21. Write a program that inputs a character from user and display
whether it is vowel or consonant.
#include<stdio.h>
int main()
{
char c ;
printf("enter an alphabet:\n");

Friday, April 2, 2021


33

scanf("%c",&c);
switch(c)
{
case 'a':
printf("you enter a vowel");
break;
case 'e':
printf("you enter a vowel");
break;
case 'i':
printf("you enter a vowel");
break;
case 'o':
printf("you enter a vowel");
break;
case 'u':

printf("you enter a vowel");


break;
default:
printf("you enter consonent");
}
return 0;

Friday, April 2, 2021


34

}
22. Write a program that input 2 numbers for user and a symbol ( +,-
,/,*,%). It perform operation on numbers according to operator , and
show results.
#include<stdio.h>
int main()
{
int num1,num2 ;
char op;
printf("enter two numbers:\n");
scanf("%d%d",&num1,&num2);
printf("enter operator:\n");
scanf(" %c",&op);

switch(op)
{
case '+':
printf("%d+%d=%d",num1,num2,num1+num2);
break;
case '-':
printf("%d-%d=%d",num1,num2,num1-num2);
break;
case '*':
printf("%d*%d=%d",num1,num2,num1*num2);
break;

Friday, April 2, 2021


35

case '/':
printf("%d/%d=%d",num1,num2,num1/num2);
break;
case '%':
printf("%d%%%d=%d",num1,num2,num1%num2);
break;
default:
printf("invalid input") ;

CONDITIONAL OPERATOR

23. Write a program that inputs marks of a student and display “


Pass” if marks
are more then 40 and display :”Fail” otherwise .
#include<stdio.h>
int main()
{
int marks;
printf("enter marks of std:");
scanf("%d",&marks);
(marks>40)?printf("you are pass"):printf("you are fail");
return 0;
}

Friday, April 2, 2021


36

24.Write a program that take a number fro m user and display


whether it is divisible by three or not.
#include<stdio.h>
int main()
{
int num;
printf("enter a number:");
scanf("%d",&num);
(num%3==0)?printf("number is divisible "):printf("number is not
divisible");
return 0;
}
WHILE LOOP
1. Write a program that display counting from 1 to 10 .
#include<stdio.h>
int main()
{
int a;
a=1;
while(a<=10)
{
printf("%d\n",a);
a++;
}
return 0;

Friday, April 2, 2021


37

}
2. Write a program that display first 5 numbers and their sum.
#include<stdio.h>
int main()
{
int a,sum;
sum=0;
a=1;
while(a<=5)
{
printf("%d\n",a);
sum=sum+a;
a++;
}
printf("%d is total sum",sum);
return 0;
}
3. Write a program that display first 5 numbers and their squares.
#include<stdio.h>
int main()
{
int a;

a=1;

Friday, April 2, 2021


38

while(a<=5)
{
printf("%d\t%d\n",a,a*a);
a++;
}
return 0;
}
4. Write a program that take a number from user and display its table
#include<stdio.h>
int main()
{
int n,a;
printf("enter a number:\n");
scanf("%d",&a);
n=1;
while(n<=10)
{
printf("%d*%d=%d\n",a,n,a*n);
n++;
}
return 0;
}
5. Write a program that inputs a number from user and display the
sum of its digits.
#include<stdio.h>

Friday, April 2, 2021


39

int main()
{
int num,digit;
printf("enter three number digit:\n");
scanf("%d",&num);
int sum=0;
while(num>=1)
{
digit=num%10;
sum=sum+digit;
num=num/10;

}
printf("%d",sum);
return 0;
}

6. Write a program that input a number from user and display its
factorial.
#include<stdio.h>
int main()
{
int num,fact=1;
printf("enter a number:\n");
scanf("%d",&num);

Friday, April 2, 2021


40

while(num>=1)
{
fact=fact*num;
num--;
}
printf("%d",fact);
return 0;
}
7. Write a program that display the sum of following series 1+ 1⁄2+
1⁄4 + 1/6 +1/8+....+1/100.
#include<stdio.h>
int main()
{

int num=2;
float sum=1;
while(num<=100)
{
sum=sum+1.0/num;
num=num+2;
}
printf("%f",sum);
return 0;
}

Friday, April 2, 2021


41

8. Write a program that inputs a positive number. It then display the


sum of all odd numbers and sum of all even numbers from 1 to
number entered by user.
#include<stdio.h>
int main()
{
int n,esum=0,osum=0,count=1;
printf("enter a positive number:\n");
scanf("%d",&n);
while(count<=n)
{
if(count%2==0)
{
esum=esum+count;
}
else
{
osum=osum+count;
}
count++;
}

printf("%d is esum\n%d is osum",esum,osum);

return 0;

Friday, April 2, 2021


42

}
Program no 9:
Write a program that take a number from user and check whether
it is ARMSTRONG number or not. ( 370 , 371 are Armstrong
numbers)
#include<stdio.h>
int main()
{
int n,temp,sum=0,num;

printf("enter a number:\n");
scanf("%d",&n);
num=n;
while(num!=0)
{
temp=num%10;
sum=sum+(temp*temp*temp);
num=num/10;
}
if(n==sum)
printf("Armstrong");
else
printf("not arm strong");
return 0;
}

Friday, April 2, 2021


43

10. Write a program that take starting and ending point from user
and display all numbers
between them.
#include<stdio.h>
int main()
{
int s,e;
printf("enter starting number:");
scanf("%d",&s);
printf("enter ending number:");
scanf("%d",&e);
s=s+1;
while(s<e)
{
printf("%d\n",s);
s++;
}
return 0;
}
11. Write a program that inputs a number from user and display n
Fibonacci terms. ( if user
enter 5 , program display first five Fibonacci numbers)
#include<stdio.h>
int main()
{

Friday, April 2, 2021


44

int n,a=0,b=1,next,count=1;

printf("enter fibonacci numbers you want to print:\n");


scanf("%d",&n);
//fibonacci =sum of last two numbers
while(count<=n)
{
printf("%d\t",a);
next=a+b;
a=b;
b=next;
count++;

}
return 0;
}

DO WHILE LOOP
12. Write a program that displays back counting from 10 to 1 .
#include<stdio.h>
int main()
{

Friday, April 2, 2021


45

int count=10;
do
{
printf("%d\t",count);
count--;

}
while(count>=1) ;
return 0;
}
13. Write a program that take 2 numbers from user and display the
result of first number
raised power second.(first= 2, second= 3 , answer=8)
#include<stdio.h>
int main()
{

int num1,num2;
int power=1;

printf("enter two numbers:\n");


scanf("%d%d",&num1,&num2);
do
{
power=power*num1;

Friday, April 2, 2021


46

num2--;
}
while(num2!=0);
printf("%d",power);
return 0;

}
14. Write a program that inputs a number from user and check
whether it is Palindrome number or not. ( 121,626, 62526, 131,222).
#include<stdio.h>
int main()
{

int n,temp,rev=0,num;
printf("enter a number:\n");
scanf("%d",&n);
num=n;
do
{
temp=num%10;
rev=rev*10;
rev=rev+temp;
num=num/10;
}
while(num!=0);

Friday, April 2, 2021


47

if(rev==n)
printf("Palindrome");
else
printf("not Palindrome");
return 0;
}

FOR LOOP

15. Write a program that display the sum of following series. 1^2
+2^2 +3^2 + ......+10^2.
#include<stdio.h>
int main()
{

int count=1,sum=0,sqa;
for (count;count<=10;count++)
{
sqa=count*count;
sum=sum+sqa;
}

printf("%d is sum of aquare of series",sum);

return 0;

Friday, April 2, 2021


48

}
16. Write a program that take a number from user and check whether
it is prime or not.
#include<stdio.h>
int main()
{

int n,c;
printf("enter a number:\n");
scanf("%d",&n);
int flag=0;
for(c=2;c<n;c++)
if(n%c==0)
flag=1;
if(n==0||n==1)
printf("not prime");
else
{
if(flag==0)
printf("prime");
else
printf("not prime");
}
return 0;
}

Friday, April 2, 2021


49

17. Write a program that take 2 numbers from user and display their
GCD.

#include <stdio.h>
int main()
{
int n1, n2, c, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(c=1; c <= n1 && c <= n2; c++)
{
// Checks if i is factor of both integers
if(n1%c==0 && n2%c==0)
gcd = c;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}
NESTED LOOP
18.
1
12
12
123
1234

Friday, April 2, 2021


50

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

return 0;
}
19.
A
AB
ABC
ABCD
ABCDE
#include<stdio.h>
int main()
{
char i,j;

Friday, April 2, 2021


51

for(i='A';i<='E';i++)
{
for(j='A';j<=i;j++)
printf("%c",j);
printf("\n");
}
return 0;
}
20.
12345
1234
123
12
1
#include<stdio.h>
int main()
{
int i,j;
for(i=5;i>=1;i--)
{

for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");

Friday, April 2, 2021


52

}
return 0;
}
21.
1
123
12345
123
1
#include <stdio.h>

int main()
{
int row,space,column;
for(row=0;row<=3;row++)
{
for(space=1;space<=3-row;space++)
printf(" ");
for(column=1;column<=2*row-1;column++)
printf("%d",column);
printf("\n");
}

for(row=3-1;row>=1;row--)

Friday, April 2, 2021


53

{
for(space=1;space<=3-row;space++)
printf(" ");
for(column=1;column<=2*row-1;column++)
printf("%d",column);
printf("\n");
}
return 0;

}
22.
*
***
*****
***
*
#include <stdio.h>
int main()
{
int row,space,column;
for(row=0;row<=3;row++)
{
for(space=1;space<=3-row;space++)
printf(" ");

Friday, April 2, 2021


54

for(column=1;column<=2*row-1;column++)
printf("*");
printf("\n");
}
for(row=3-1;row>=1;row--)
{
for(space=1;space<=3-row;space++)
printf(" ");
for(column=1;column<=2*row-1;column++)

printf("*");
printf("\n");
}
return 0;
}

23.
@
@@
@@@
@@@@
@@@@@
#include<stdio.h>
int main()

Friday, April 2, 2021


55

{
int row,space,column;
for(row=1;row<=5;row++)
{
for(space=1;space<=5-row;space++)
printf(" ");
for(column=1;column<=row;column++)
printf("@");
printf("\n");
}
return 0;
}
24.
1
24
369
24
1
#include<stdio.h>
int main()
{
int i, j;
for(i=1;i<=3;i++)
{

Friday, April 2, 2021


56

for(j=1;j<=i;j++)
printf("%d",i*j);
printf("\n");
}
for(i=3-1;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("%d",i*j);
printf("\n");
}
return 0;
}
25.
@ @
@ @
@ @
@@
@
#include<stdio.h>
int main()
{
int sp = 9;
for ( int i=0;i<=5;i++) {
for ( int j=0;j<=i;j++) { printf(" "); }

Friday, April 2, 2021


57

printf("@");
for ( int k=sp; k>0; k--) { printf(" "); }
sp -= 2;
if ( i != 5) {

printf("@");
}
printf("
\n");

}
}
Arrays
Program no 1:
Write a program that take 5 numbers from user in array, calculate
their sum and average.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ary[5],i,sum=0;
float avg;
for(i=0;i<5;i++)
{
printf("enter number %d=",i+1);

Friday, April 2, 2021


58

scanf("%d",&ary[i]);
}
for(i=0;i<5;i++)
{
sum=sum+ary[i];
}
printf("sum=%d\n",sum);
avg=(float)sum/5;
printf("avg=%f",avg);
return 0;
}
Program no 2:
Write a program that inputs current day and month from user. It
then calculate and displays the total number of days in current year
till the date entered.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int month[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int d,m,i,total=0;
printf("enter current day:\n");
scanf("%d",&d);
printf("enter current month:\n");
scanf("%d",&m);

Friday, April 2, 2021


59

for(i=m;i<12;i++)
{
total=total+month[i];
}
total=total+(month[m-1]-d);
printf("total=%d",total);
return 0;
}
Program no 3:
Write a program that take 10 numbers from user and display
maximum number.
#include<stdio.h>
int main()
{
int ary[10];
int i,max;
for(i=0;i<10;i++)
{
printf("enter number %d=",i+1);
scanf("%d",&ary[i]);
}
for(i=0;i<10;i++)
{
max=ary[0];
if(ary[i]>max)

Friday, April 2, 2021


60

max=ary[i];
}
printf("max=%d",max);
return 0;
}
Program no 4:
Write a program that take 10 numbers from user and display
minimum
number.
#include<stdio.h>
int main()
{
int ary[10];
int i, min;
for(i=0;i<10;i++)
{
printf("enter number %d=",i+1);
scanf("%d",&ary[i]);
}
for(i=0;i<10;i++)
{
min =ary[0];
if(ary[i]< min)
min =ary[i];
}

Friday, April 2, 2021


61

printf("min =%d", min);


return 0;
}
Program no 5:
Write a program that initialize an array. Take a number from user
and searches the number in array.
#include<stdio.h>
int main()
{
int ary[10];
for(i=0;i<10;i++)
{
printf("enter number %d=",i+1);
scanf("%d",&ary[i]);
}
int i,search;i=-1;
printf("enter number to search:\n");
scanf("%d",&search);
for(i=0;i<10;i++)
{
if(ary[i]==search)

{
i=1;
break;

Friday, April 2, 2021


62

}
}
if(i==1)
printf("found");
else
printf("not found");
return 0;
}
Program no 6:
Write a program that initialize an array. Take a number from user
and searches the number in array.
#include<stdio.h>
int main()
{
int ary[10],c;
for(c=0;c<10;c++)
{
printf("enter number %d=",c+1);
scanf("%d",&ary[c]);
}
int search;
printf("enter number to search:\n");
scanf("%d",&search);
int s=0,e=9,mid,i=-1;
while(s<=e)

Friday, April 2, 2021


63

{
mid=(s+e)/2;
if(ary[mid]==search)
{
i=mid;
break;
}
else if(ary[mid]>search)
e=mid-1;
else
s=mid+1;
}
if(i==-1)
printf("not found");
else
printf("found");
return 0;
}
Program no 7
Write a program that input in array and sort using selection sort
(arrange it).
#include<stdio.h>
int main()
{
int ary[10];

Friday, April 2, 2021


64

int i,j,temp;
for(i=0;i<10;i++)
{
printf("enter number %d=",i+1);
scanf("%d",&ary[i]);
}
for(i=0;i<10;i++)
{
for(j=i;j<10;j++)
if(ary[i] > ary[j])
{
temp=ary[i];
ary[i]=ary[j];
ary[j]=temp;
}
}
printf("sorting data:\n");
for(i=0;i<10;i++)
printf("\n %d\t ",ary[i]);
return 0;
}
Program no 8:
Write a program that input in array and sort using bubble sort.
#include <stdio.h>

Friday, April 2, 2021


65

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
/* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}

Friday, April 2, 2021


66

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}
Program no 9
Write a program that read the temperature of a week. And display
the maximum, minimum, average temperature of the week.
#include<stdio.h>
#include<stdlib.h>
int main()
{
float ary[7],sum=0,avg;
int i,max,min;
for(i=0;i<7;i++)
{
printf("enter temperature reading of the day %d=",i+1);
scanf("%f",&ary[i]);
}
max=ary[0];
min=ary[0];

Friday, April 2, 2021


67

for(i=0;i<7;i++)
{
if(ary[i]>max)
{
max=ary[i];
}
if(ary[i]<min)
{
min=ary[i];
}

}
printf("\nMaximim temprature=%d\n",max);
printf("\nMinimum temprature=%d\n",min);
for(i=0;i<7;i++)
sum=sum+ary[i];
avg=sum/7.0;
printf("\navg temprature=%.2f",avg);
return 0;
}

Program no 10

Write a program that take 10 numbers from user and display their
Mean .( Average ).

Friday, April 2, 2021


68

#include<stdio.h>
int main()
{
int ary[10];
int i,sum=0;
float avg;
for(i=0;i<10;i++)
{
printf("enter number %d=",i+1);
scanf("%d",&ary[i]);
}
for(i=0;i<10;i++)
{
sum=sum+ary[i];
}
avg = (float)sum/10;
printf("mean = %f",avg);
return 0;
}

Program no 11
Write a program that take 10 numbers from user and display their
Median .( middle value from sorted array) .
#include<stdio.h>
int main()

Friday, April 2, 2021


69

{
int ary[10];
int i,j,temp;
float median;
for(i=0;i<10;i++)
{
printf("enter number %d=",i+1);
scanf("%d",&ary[i]);
}
for(i=0;i<10;i++)
{
for(j=i;j<10;j++)
{
if(ary[i] > ary[j])
{
temp=ary[i];
ary[i]=ary[j];
ary[j]=temp;
}
}
}
printf("\n sorted values are:");
for(i=0;i<10;i++)
printf("\n%d\t",ary[i]);

Friday, April 2, 2021


70

// if number of elements are even


if(10%2 == 0)
median = ((ary[(10-1)/2] + ary[10/2])/2.0);
// if number of elements are odd
else
median = ary[10/2];
printf("\n median=%f",median);
return 0;
}

Program no 12
Write a program that take 10 numbers from user and display their
Mode .
( most repeated value)
#include<stdio.h>
int main ()
{
int arr[10];
int a,b,count,max_count=0;
for(a=0;a<9;a++)
{
printf("Enter the %d Number :",a);
scanf("%d",&arr[a]);
}
for(a=0;a<9;a++)

Friday, April 2, 2021


71

{
count=1;
for(b=a+1;b<9;b++)
{
if(arr[a]==arr[b])
count++;
}
if(count>max_count)
max_count=count;
}
printf("Maximum Repeated value is:");
for(a=0;a<9;a++)
{
count=1;
for(b=a+1;b<9;b++)
{
if(arr[a]==arr[b])
count++;
}
if(count==max_count)
printf("\n%d\t%d",arr[a],max_count);
}
return 0;
}

Friday, April 2, 2021


72

2D Arrays
Program no 13
Write a program that declare a 4 x 4 array. Take input from user .
find maximum , minimum from array.
#include<stdio.h>
int main()
{
int ary[4][4];
int i,j,max,min;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("enter number");

37

February 3, 2021

scanf("%d",&ary[i][j]);
}
}
max=ary[0][0];
min=ary[0][0];
for(i=0;i<4;i++)

Friday, April 2, 2021


73

{
for(j=i;j<4;j++)
{
if(ary[i][j]>max)
max=ary[i][j];
if(ary[i][j]<min)
min=ary[i][j];

}
}
printf("\nMaximum=%d\nMinimum=%d",max,min);
return 0;
}
Program no 14
Write a program that take input in two matrices of size 3 by 4.
Perform subtraction and addition on them.
#include<stdio.h>
int main()
{
int a[3][4],b[3][4],c[3][4];
int i,j;
printf("enter first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)

Friday, April 2, 2021


74

scanf("%d",&a[i][j]);
}
printf("enter second matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
scanf("%d",&b[i][j]);
}
printf("Sum is\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++){
c[i][j]=a[i][j] + b[i][j];
printf("%d",c[i][j]);
}
printf("\n");
}
printf("Sub is\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++){
c[i][j]=a[i][j] - b[i][j];
printf("%d",c[i][j]);
}

Friday, April 2, 2021


75

printf("\n");
}

return 0;
}
Program no 15
Write a program that take input in two matrices of size 3 by 4.
Perform Multiplication on them.
#include<stdio.h>
int main()
{
int a[3][4],b[3][4],c[3][4];
int i,j;
printf("enter first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
printf("enter second matrix:\n");
for(i=0;i<3;i++)

{
for(j=0;j<4;j++)
scanf("%d",&b[i][j]);

Friday, April 2, 2021


76

}
printf("multiply is\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++){
c[i][j]=a[i][j] * b[i][j];
printf("%d",c[i][j]);
}
printf("\n");
}
return 0;
}
Structure
Program no 1
Write a program that declare a structure to store roll
no,marks,average,grade.Take input of 5 students. And disply topper
student on screen.
#include <stdio.h>
#include <stdlib.h>;
#include <conio.h>
struct student
{
int roll, marks;
char grade;
float avg;

Friday, April 2, 2021


77

};
int main ()
{
student s[5];
int i;
for (i=0;i<5;i++)
{
printf("Enter roll number:");
scanf("%d",&s[i].roll);
printf("Enter marks:");
scanf("%d",&s[i].marks);
printf("Enter grade:\n");
s[i].grade=getche();
printf("\nEnter avg:");
scanf("%f",&s[i].avg);
}
int max;
max=0;
for (i=1;i<5;i++)
{
if (s[i].marks > s[max].marks)
{
max=i;
}

Friday, April 2, 2021


78

}
printf("topper student:\n");
printf("\nRoll number:%d\n",s[max].roll);
printf("\nMarks:%d\n",s[max].marks);
printf("\nGrade:%c\n",s[max].grade);
printf("\naverage:%f\n",s[max].avg);
return 0;
}
Program no 2
Write a program that declare a structure to store employer number,
name,hours worked,and hourly rate ,and gross pay. The program
then inputs the employer number,name,hours worker and hourly
rate from user and disply gross pay with all other information.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct employee
{
int number, grosspay, hours, hourlyrate;
char name[50];
}e1;
int main ()
{
printf("Enter name:\n");
gets(e1.name);

Friday, April 2, 2021


79

printf("Enter number:");
scanf("%d",&e1.number);
printf("\nEnter grosspay:");
scanf("%d",&e1.grosspay);
printf("\nEnter hours worked:\n");
scanf("%d",&e1.hours);
printf("\nEnter hourly rate:");
scanf("%d",&e1.hourlyrate);
printf("\nEmployee name:%c\n",e1.name);
printf("\nEmployee Number:%d\n",e1.number);
printf("\nEmployee gross pay:%d\n",e1.grosspay);
printf("\nEmployee hours worked:%d\n",e1.hours);
printf("\nEmployee hourly rate:%d\n",e1.hourlyrate);
return 0;
}
Program no 3
Write a program that declare a structure to store roll no,5 student
marks, and average.Take input in marks and roll no. And disply
average on screen.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct student
{
int roll;

Friday, April 2, 2021


80

int marks[5];
float avg;
};
int main ()
{
student s;
printf("Enter roll number:\n");
scanf("%d",&s.roll);
int i,sum;
for (i=0;i<5;i++)
{
printf("\nEnter subject mark %d:\n",i+1);
scanf("%d",&s.marks[i]);
sum =sum + s.marks[i];
}
s.avg=sum/5;
printf("Average is:%f",s.avg);
return 0;
}
Program no 4
Write a program that declare a structure to store id ,pages ,price of a
book.program input data of 5 books and display the most costly book
on screen.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

Friday, April 2, 2021


81

struct book
{
int id, pages, price;
};
int main ()
{
struct book b[5];
int i;
for (i=0;i<5;i++)
{
printf("Enter id:");
scanf("%d",&b[i].id);
printf("Enter price:");
scanf("%d",&b[i].price);
printf("Enter pages:\n");
scanf("%d",&b[i].pages);
}
int max;
max=0;
for (i=1;i<5;i++)
{
if (b[i].price > b[max].price)
{
max=i;
}
}
printf("\n%d is the id of book\n",b[max].id);
printf("\n%d is the price of book\n",b[max].price);
printf("\n%d are the pages of book\n",b[max].pages);
return 0;

Friday, April 2, 2021


82

Program no 5
Write a program that declare a structure player to store name
,score,wickets,catches,matches,take input of 5 players from user and
display the best bolwer name,best batsman name,and best fielder
name on screen.
#include<stdio.h>
struct cricketer
{
int runs,wickets,catches,matches;
char name[25];
}player[100],t;

int main()
{
int i,j,n;
printf("Enter the no of cricket players\n");
scanf("%d",&n);
printf("Enter player info as name , matches , catches , wickets taken , runs
scored\n");
for(i=0;i<n;i++)
{
scanf("%s %d
%d",&player[i].name,&player[i].wickets,&player[i].runs,&player[i].catches,
&player[i].matches);
}
//for best bowler
for(i=0;i<n;i++)

Friday, April 2, 2021


83

{
for(j=0;j<n-1;j++)
{
if(player[j].catches>player[j+1].catches)
{
if(player[j].wickets>player[j+1].wickets){
player[j].name;
}

}
}
}
printf("BEST BOWLER IS");
printf("&s",player[j].name);
//for best BATSMAN
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(player[j].matches>player[j+1].matches)
{
if(player[j].runs>player[j+1].runs){
player[j].name;
}

}
}

Friday, April 2, 2021


84

}
printf("BEST BATSMAN IS");
printf("&s",player[j].name);
return 0;
}
Functions
Program no 1
Write a program that displays a message “Programming makes like
easy!” using function.
#include <stdio.h>
void msg(void);
int main()
{
msg();

return 0;
}
void msg()
{
printf("programing makes like easy:");
}
Program no 2
Write a program that displays first 5 number on screen using function.
#include<stdio.h>
void no(void);
int main()
{
no();
return 0;

Friday, April 2, 2021


85

}
void no()
{
int n;
for(n=1;n<=5;n++)
{
printf("%d\n",n);
}
}

Program no 3
Write a program that displays back counting from 10 to 1
using function.
#include <stdio.h>
void no();
int main()
{
no();
return 0;
}
void no()
{
int n;
for(n=10;n>=1;n--)
{
printf("%d\n",n);

Friday, April 2, 2021


86

}
}
PASSING PARAMETERS TO FUNCTIONS

Program no 4
4. Write a program that input a number in main function, passes to function. Function check
whether it is even
or odd.
#include <stdio.h>
void no(int);
int main()
{
int n;
printf("enter a number:\n");
scanf("%d",&n);
no(n);
return 0;
}
void no(int m)
{
if(m%2==0)
printf("even");
if(m%2!=0)
printf("odd");
}
Program no 5
Write a program that input 2 numbers from user , passes them to a function. Function display
maximum
number on screen.
#include <stdio.h>

Friday, April 2, 2021


87

void max(int,int);
int main()
{
int a,b;
printf("enter two numbers:\n");
scanf("%d%d",&a,&b);
max(a,b);
return 0;
}
void max(int m,int n)
{
if(m>n)
printf("%d is max",m);
else
printf("%d is max",n);
}
Program no 6
Write a program that input a number from user, passes it to a function. Function display its table.
#include <stdio.h>
void table(int);
int main()
{
int n;
printf("enter a number:\n");
scanf("%d",&n);
table(n);
return 0;
}
void table(int m)
{

Friday, April 2, 2021


88

int c;
for(c=1;c<=10;c++)
{
printf("%d * %d = %d\n",m,c,m*c);
}
}
Program no 7
Write a program that inputs a number from user, passes to a function. Function display its
factorial.
#include<stdio.h>
void fact(int);
int main()
{
int n;
printf("enter a number\n:");
scanf("%d",&n);
fact(n);
return 0;
}
void fact(int m)
{
int f=1;
while(m>0)
{
f=f*m;
m--;
}
printf("%d",f);
}
Program no 8

Friday, April 2, 2021


89

Write a program that inputs 2 number and 1 operator, passes them to a function. Function perform
the operations according to operator. ( calculator working)
#include <stdio.h>
void rev(int);
int main()
{
int n;
printf("enter a number");
scanf("%d",&n);
rev(n);
return 0;
}
void rev(m)
{
int r;
int temp;
while(m!=0)
{
temp=m%10;
r=(r*10)+temp;
m=m/10;
}
printf("%d",r);
}
Program no 9
Write a program that inputs a number from user and a character, passes them to a function.
Function display the square of that symbol.
#include<stdio.h>
#include<conio.h>
void pat(int,char);
int main()

Friday, April 2, 2021


90

{
int a;
char sym;
printf("Enter a symbol\n");
scanf("%c",&sym);
printf("Enter a number to print a symbol in matrix");
scanf("%d",&a);
pat(a,sym);
return 0;
}
void pat (int a,char sym)
{
printf("\n");
int i,j;
for(i=1;i<=a;i++)
{
for(j=1;j<=a;j++)
{
printf("%c",sym);k
}
printf("\n");
}
}
Program no 10:
Write a program that inputs a number from user, passes to a function.
Function display whether it is prime
or not.
#include<stdio.h>
void prime(int);
int main()
{

Friday, April 2, 2021


91

int n;
printf("Enter a number");
scanf("%d",&n);
prime(n);
return 0;
}
void prime(int n)
{
int flag=0,c;
for(c=2;c<n;c++)
{
if(n%c==0)
flag=1;
break;
}
if(n==0||n==1)
{
printf("Not prime");
}
else
{
if(flag==0)
printf("Prime");
else
printf("Not prime");
}
}
Program no 11:
Write a program that inputs a number from user, passes to a function.
Function check whether it is Fibonacci

Friday, April 2, 2021


92

or not.
#include<stdio.h>
void fab(int);
int main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
fab(n);
return 0;
}
void fab(int n)
{
int t1,t2,nt;
t1=0;
t2=1;
if(n==t1||n==t2)
printf("Fibonccai");
nt=t1+t2;
while(nt<=n)
{
if(nt==n)
printf("Fibonccai");
t1=t2;
t2=nt;
nt=t1+t2;
}
}
Program no 12:
Write a program that input a number from user, passes to a function.
Function display the number in reverse order. ( 1234 , output= 4321)

Friday, April 2, 2021


93

#include<stdio.h>
#include<conio.h>
void reverse(int);
int main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
reverse(n);
return 0;
}
void reverse(int n)
{
int rev=0,temp;
while(n!=0)
{
temp=n%10;
rev=rev*10;
rev=rev+temp;
n=n/10;
}
printf("%d",rev);
}
PASS BY REFERENCE
Program no 13:
Write a program that inputs two integer from user, passes
them to a function. Function swap the values.
#include<stdio.h>
#include<conio.h>
void swap (int&a,int&b);

Friday, April 2, 2021


94

int main()
{
int a,b;
printf("Enter value of a\n");
scanf("%d",&a);
printf("Enter value of b\n");
scanf("%d",&b);
swap(a,b);
return 0;
}
void swap (int &a,int &b)
{
int c;
c=a;
a=b;
b=c;
printf("Value of a after swaping are %d\n",a);
printf("Value of b after swaping are %d",b);
}
RETURN VALUE BY FUNCTION
Program no 14:
Write a program that inputs base and height of a triangle in
main function. Passes them to a function. Then function
finds the area of triangle and return it to main function.
Main displayed on screen .
#include<stdio.h>
#include<conio.h>
float area ( float ,float);

Friday, April 2, 2021


95

int main()
{
float height,base,c;
printf("Enter height");
scanf("%f",&height);
printf("Enter base of a tringle");
scanf("%f",&base);
c=area(height,base);
return 0;
}
float area(float height ,float base)
{
float a;
a=0.5*base*height;
printf("%f",a);
return a;
}
Program no15:
Write a program that inputs two number from user, passes
them to a function. Function calculate GCD and return to
main function. Main function display the gcd of numbers.
#include<stdio.h>
int gcd (int,int);
int main()
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);

Friday, April 2, 2021


96

c=gcd(a,b);
printf("%d is gcd",c);
return 0;
}
int gcd (int a,int b)
{
int m,gcd;
if(a>b)
m=a;
else
m=b;
int i;
for(i=1;i<=m;i++)
{
if(a%i==0&&b%i==0)
{
gcd=i;
}
}
return gcd;
}
ARRAY AS PARAMETERS OF FUNCTION
Program no 16
Write a program that take 5 numbers from user, store them
in array, pass array to a function. Function display the
maximum number on screen.
#include<stdio.h>

Friday, April 2, 2021


97

#include<conio.h>
int max (int ary[5]);
int main()
{
int ary[5];
int i;
for(i=0;i<5;i++)
{
printf("enter number %d:",i+1);
scanf("%d",&ary[i]);
}
max(ary);

return 0;
}
int max(int ary[5])
{
int i;
int max=ary[0];
for(i=0;i<5;i++)
{

Friday, April 2, 2021


98

if(ary[i]>max)
max=ary[i];
}
printf("max=%d",max);
}
Program no 18
Write a program that take input 10 numbers in array ,
passes it to function. Function sort the array.
#include<stdio.h>
#include<conio.h>
int sort (int ary[10]);
int main()
{
int ary[10],i;
for(i=0;i<10;i++)
{
printf("enter number:");
scanf("%d",&ary[i]);
}
sort(ary);
return 0;

Friday, April 2, 2021


99

}
int sort (int ary [10])
{
int i,j,swap;
for(i=0;i<10;i++)
{
for(j=i;j<10;j++)
{
if(ary[i]>ary[j])
{
swap=ary[i];
ary[i]=ary[j];
ary[j]=swap;
}
}
}
printf("sorting data:\n");
for(i=0;i<10;i++)
printf("\n%d\t",ary[i]);
}

Friday, April 2, 2021


100

Recurison
Program 21:
#include<stdio.h>
#include<conio.h>
int fact (int);
int main()
{
int n,f;
printf("enter a number for factroial:\n");
scanf("%d",&n);
f=fact (n);
printf("factorial=%d",f);
return 0;
}
int fact (int n)
{
if(n>0)
{
return n * fact ( n-1 );
}
else
{
return 1;
}
};

Program 22:
#include<stdio.h>
#include<conio.h>

Friday, April 2, 2021


101

int pow (int,int);


int main()
{
int a,b,c;
printf("enter a number:\n");
scanf("%d",&a);
printf("enter a number:\n");
scanf("%d",&b);
c=pow(a,b);
printf("%d",c);
return 0;
}
int pow ( int n,int m)
{
if(m>0)
{
return n * pow ( n , m-1);
}
else
{
return 1;
}
}

Program 23:
#include<stdio.h>
#include<conio.h>
int backcount (int);
int main()
{

Friday, April 2, 2021


102

int n,c;
printf("enter a number:\n");
scanf("%d",&n);
c=backcount(n);
printf("%d",c);
return 0;
}
int backcount (int n)
{
if(n>1)
{
printf("%d\n",n);
backcount ( n-1);
}

Program 24:
#include<stdio.h>
#include<conio.h>
int sum (int);
int main()
{
int n,c;
printf("enter a number:\n");
scanf("%d",&n);
c=sum(n);
printf("sum=%d",c);
return 0;
}

Friday, April 2, 2021


103

int sum (int n)


{
if(n>0)
{
printf("%d\n",n);
return n + sum ( n-1);
}
else
return 0;

Program 25:
#include<stdio.h>
#include<conio.h>
int fab (int);
int main()
{
int n,c;
printf("enter a number of trems you want to find:\n");
scanf("%d",&n);
c=fab (n);
printf("%d is the term",c);
return 0;
}
int fab (int n)
{
if(n==0)
return 0;
else if(n==1)

Friday, April 2, 2021


104

return 1;
else
{
return(fab(n-1)+fab(n-2));
}
return 0;

File handling
Program 1
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
FILE *f;
f = fopen("merifile.txt","w");
if (f == NULL)
{
printf("error");
}
else
{
int n;
for(n=1;n<=10;n++)

Friday, April 2, 2021


105

{
fprintf(f,"%d",n);
}
printf("data entered");
}
fclose(f);
getch();
return 0;
}
Program 2
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
FILE*p;
p = fopen("cities.txt","w");
if (p == NULL)
{
printf("error");
}
else
{

Friday, April 2, 2021


106

char cities[80];
printf("Enter cities:\n");
char c;
for (c=0;c<80;c++)
{
scanf("%c",&cities[c]);
putc(cities[c],p);
}
}
fclose(p);
getch();
return 0;
}
Program 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// a struct to read and write


struct person
{
int wickets;
char name[20];

Friday, April 2, 2021


107

int matches;
int scores;
};
int main ()
{
FILE *f;
f = fopen ("person.text", "w");
if (f == NULL)
{
printf("\nError opend file\n");
exit (1);
}
struct person input1 = {25,"babar",17,265};
fwrite (&input1, sizeof(struct person),25, f);
if(fwrite != 0)
printf("contents to file written successfully !\n");
else
printf("error writing file !\n");
fclose (f);
return 0;
}
Program no 4
#include <stdlib.h>

Friday, April 2, 2021


108

#include <stdio.h>

int countWords(FILE *f)


{
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF)
{
if (ch == '\n')
count++;
}
return count;
}
int main(void)
{

int wordCount = 0;
FILE *rFile = fopen("wrds.txt", "r");
wordCount += countWords(rFile);
printf("%d", wordCount);
return 0;
}
Program 5

Friday, April 2, 2021


109

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
FILE*f;
f = fopen("merifile.txt","r");
if (f == NULL)
{
printf("error");
}
else
{
char a;
int count =0;
a = fgetc(f);
while(a != EOF)
{
count++;
printf("%c",a);
a = fgetc(f);
}
printf("total characters in file is %d",count);

Friday, April 2, 2021


110

}
fclose(f);
getch();
return 0;
}
Program 6
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
FILE*f,*q;
f = fopen("merifile.txt","r");
q = fopen("merifile01.txt","w");
if (f == NULL || q == NULL)
{
printf("error");
exit(1);
}
else
{
char a;
a = fgetc(f);

Friday, April 2, 2021


111

while(a != EOF)
{
fprintf(q,"%c",a);
a = fgetc(f);
}
printf("data transfered");
}
fclose(f);
fclose(q);
getch();
return 0;
}
Program 7
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fPtr;
char ch;
fPtr = fopen("data.txt", "r");
if(fPtr == NULL)
{
printf("Unable to open file.\n");

Friday, April 2, 2021


112

exit(EXIT_FAILURE);
}
printf("File opened successfully\n\n");
do
{
ch = fgetc(fPtr);
putchar(ch);
} while(ch != EOF);
fclose(fPtr);
return 0;
}
Program 8
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
FILE*f;
f = fopen("random.txt","w");
if (f == NULL)
{
printf("error");
}

Friday, April 2, 2021


113

else
{
int c,n;
for (c = 1; c <= 100; c++)
{
n = rand() % 100 + 1;
fprintf(f,"%d\n",n);
}
}
fclose(f);
getch();
return 0;
}
Program 9
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
FILE*p,*q,*r;
int n;
p = fopen("random.txt","r");
q = fopen("even.txt","w");

Friday, April 2, 2021


114

r = fopen("odd.txt","w");
n = getc(p);
while (n != EOF)
{
if (n % 2 == 0)
putc(n,q);
else if(n % 2 != 0)
putc(n,r);
n=getc(p);
}
fclose(p);
fclose(q);
fclose(r);
getch();
return 0;
Program 10
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
FILE*p;
p = fopen("Table.txt","w");

Friday, April 2, 2021


115

if (p == NULL)
{
printf("error");
}
else
{
int n;
printf("Enter a number");
scanf("%d",&n);
int c;
for (c=1;c<=10;c++)
{
fprintf(p,"%d x %d = %d",n,c,n*c);
}
fclose(p);
}
getch();
return 0;
}

Friday, April 2, 2021


116

Friday, April 2, 2021

You might also like