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

Meaning of Format Specification /N /R /T /B and Uses

The document contains 16 code examples demonstrating various C programming concepts like: 1. Use of escape sequences like \n, \t, \b, \r in printf statements 2. Converting between Celsius and Fahrenheit temperatures 3. Calculating simple interest 4. Swapping two variables values 5. Calculating gross salary from basic salary 6. Computing areas and perimeters of shapes 7. Logical and bitwise operators like &&, ||, !, ^, ~ 8. Increment and decrement operators like ++, -- 9. Comparing and printing values using if-else conditions 10. Identifying even, odd and leap years 11. Computing roots of quadratic equations 12

Uploaded by

Supriyo Srimani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Meaning of Format Specification /N /R /T /B and Uses

The document contains 16 code examples demonstrating various C programming concepts like: 1. Use of escape sequences like \n, \t, \b, \r in printf statements 2. Converting between Celsius and Fahrenheit temperatures 3. Calculating simple interest 4. Swapping two variables values 5. Calculating gross salary from basic salary 6. Computing areas and perimeters of shapes 7. Logical and bitwise operators like &&, ||, !, ^, ~ 8. Increment and decrement operators like ++, -- 9. Comparing and printing values using if-else conditions 10. Identifying even, odd and leap years 11. Computing roots of quadratic equations 12

Uploaded by

Supriyo Srimani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

1.

Meaning of format specification \n \r \t \b and uses

#include<stdio.h>
#include<conio.h>
void main()
{
printf(“This is a Name List”);
/* \n to move the cursor to the next line */
/* \t to move the cursor to the next tab (each tab consists of 5 characters) */
/* \b to move the cursor to the backspace */
/* \r to replace the character */
printf(“\n Debobrata Dutta”);
printf(“\n Sukhendu\tDey”);
printf(“\n Kalyan\bAdhikary”);
printf(“\n Complete\rJava”);
getch();
}

Output
This is a Name List
Debobrata Dutta // gone to next line
Sukhendu Dey // moved the cursor 5 spaces advance after u
KalyaAdhikary // moved one space back side
Javalete // first four letters comp of complete have been replaced by ‘java’, from the
beginning

2. To compute Fahrenheit from centigrade using the C/5 = (F-32)/9

#include<stdio.h>
#include<conio.h>
void main()
{
float F,C;
printf("enter the value of centigrade temperature:");
scanf("%f",&C);
F=(9/5.0)*C+32;
printf("The Fahrenheit temperature %f",F);
getch();
}

Output
enter the value of centigrade tempeture:40
The Fahrenheit temperature 104.000000

3. Calculate the simple interest (SI) = (Principle Amount*Year*Rate of Interest)/100

#include<stdio.h>
-1-
#include<conio.h>
float SI;
void main()
{
float N,R,P;
clrscr();
printf("enter the value of years,interest,amount:");
scanf("%f%f%f",&N,&R,&P);
SI=(P*R*N)/100.0;
printf("The Simple Interest %f",SI);
getch();
}

Output
enter the value of years, interest, amount:
2
5
7000
The Simple Interest 700.000000

4. Swapping of Two Numbers using third Variable

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of A and B");
scanf("%d%d",&a,&b);
printf("\nBefore Swapping A=%d B=%d",a,b);
c=a;
a=b;
b=c;
printf("\nAfter Swapping A=%d B=%d",a,b);
getch();
}

Output
Enter the value of A and B
3
4
Before Swapping A=3 B=4
After Swapping A=4 B=3

5. Miraj’s Basic salary is input through the keyboard. His DA is 55% of Basic salary, and
HRA is 15% of Basic salary. Write a program to calculate his Gross salary.
-2-
#include<stdio.h>
#include<conio.h>
void main()
{
float bs,da,hra,gross;
clrscr();
printf("enter the value of Basic salary");
scanf("%f",&bs);
da=0.55*bs; /*Here use of multiplication operator to calculate DA */
hra=0.15*bs; /*Here use of multiplication operator to calculate HRA */
gross=bs+da+hra; /*Here use of addition operator to calculate Gross Salary*/
printf("The Gross Salary is= %f",gross);
getch();
}

Output
enter the value of Basic salary10000
The Gross Salary is= 17000.000000

6. Area and Perimeter of a Triangle, Square, Circle and Rectangle


Area of Triangle=1/2*base*height
Area of Square = side*side Perimeter of the Square= 4*side
2
Area of Circle = π*radius Perimeter of the circle= 2* π*radius /* π=3.14*/
Area of Rectangle= base*height Perimeter of the Square= 2*(base + height)

#include<stdio,h>
#include<conio.h>
void main()
{
int tbase, theight, side, radius, rbase, rheight;
float tarea, sarea, speri, carea, cperi, rarea, rperi;
printf(“\nEnter the Base and Height of Triangle”);
scanf(“%d%d”,&tbase,&theight);
printf(“\nEnter the side of Square”);
scanf(“%d”,&side);
printf(“\nEnter the radius of the Circle”);
scanf(“%d”,&radius);
printf(“\nEnter the Base and Height of Rectangle”);
scanf(“%d%d”,&rbase,&rheight);
tarea=0.5*tbase*theight;
sarea=side*side;
speri=4*side;
carea=3.14*radius*radius;
cperi=2*3.14*radius;
rarea=rbase*rheight;
rperi=2*(rbase+rheight);
printf(“\nArea of Triangle=%f “,tarea);
-3-
printf(“\nArea of Square=%f Perimeter of Square=%f”,sarea,speri);
printf(“\nArea of Circle=%f Perimeter of Circle=%f”,carea,cperi);
printf(“\nArea of Rectangle=%f Perimeter of Rectangle=%f”,rarea,rperi);
getch();
}

Output
Enter the Base and Height of Triangle
2
3
Enter the side of Square
2
Enter the radius of the Circle
2
Enter the Base and Height of Rectangle
2
3
Area of Triangle=3.000000
Area of Square=4.000000 Perimeter of Square=8.000000
Area of Circle=12.560000 Perimeter of Circle=12.560000
Area of Rectangle=6.000000 Perimeter of Rectangle=10.000000

7. Use of AND (&&), OR (||) and NOT (!) Operations

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,AND,OR,NOT;
clrscr();
printf("enter the value of A and B in between 0 and 1");
scanf("%d%d",&a,&b);
AND=a&&b; /* Here use of Logical AND operator */
OR=a||b; /* Here use of Logical OR operator */
NOT=!b; /* Here use of Logical NOT operator */
printf("\nThe AND Operation = %d",AND);
printf("\nThe OR Operation = %d",OR);
printf("\nThe NOT Operation for B variable = %d",NOT);
getch();
}

Output
enter the value of A and B in between 0 and 1
1
0
The AND Operation = 0
The OR Operation = 1
-4-
The NOT Operation for B variable = 1

8. Write a C program using increment and decrement operators

#include<stdio.h>
#include<conio.h>
void main()
{
int x=5;
int a,b,c,d,e,f,g;
clrscr();
printf(“\n p=%d”,x++);
a=x++; /* this is post operation */
x=5;
b=x+1;
x=5;
c=x--; /* this is post operation */
printf(“\ns=%d”,x--);
x=5;
d=x-1;
x=5;
e=++x; /* this is pre operation */
x=5;
f=--x; /* this is pre operation */
g=(x++)+(--x);
printf(“g=%d”,g);
printf("\n A=%d\tB=%d\tC=%d\tD=%d\tE=%d\tF=%d",a,b,c,d,e,f);
getch();.
}

Output
p=5 s=4 g=6
A=6 B=6 C=5 D=4 E=6 F=4

9. Use of XOR(^) and One’s complement(~)

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,EOR,ONES_COMP;
clrscr();
printf("\n Enter the value of a ,b and c");
scanf("%d%d%d",&a,&b,&c);
EOR=a^b;
ONES_COMP=~c;
printf("\nA=%d\nB=%d\nC=%d\nEOR=%d\nONES_COMP=%d",a,b,c,EOR,ONES_COMP);
-5-
getch();
}

Output
Enter the value of a and b
0
1
2
A=0
B=1
C=2
EOR=1
ONES_COMP=1

10. Use of Dot and Arrow operators

#include<stdio.h>
#include<conio.h>
void main()
{
struct stud
{
char name[20];
int roll;
int age;
}st;
struct stud *pst;
clrscr();
printf("\n Enter the student’s name--");
scanf("%s",st.name); /* %s use for string*/
printf("\n Enter roll--");
scanf("%d",&st.roll);
printf("\n Enter the age--");
scanf("%d",&st.age);
pst=&st;
printf("\n Name---%s\tRoll---%d\tAge---%d",pst->name,pst->roll,pst->age);
getch();
}

Output
Enter the student’s name--JOY
Enter roll--101

11. To find the Bigger one between two numbers

#include<stdio.h>
#include<conio.h>
-6-
void main()
{
int a,b,c;
clrscr();
printf("Enter the values of A and B");
scanf("%d%d",&a,&b);
printf("\nTwo Number A=%d B=%d",a,b);
if(a>b)
printf("\nNumber=%d is Bigger ",a );
else
printf("\nNumber=%d is Bigger ",b );
getch();
}

Output
Enter the value of A and B
5
4

Two Number A=5 B=4


Number=5 is Bigger
Enter the value of A and B
3
4

Two Number A=3 B=4


Number=4 is Bigger

12. Identification of Even or Odd Number

#include<stdio.h>
#include<conio.h>
void main()
{
int a,c;
clrscr();
printf("Enter The Number");
scanf("%d",&a);
c=a%2;
if(c==0)
printf("The Numbe %d is Even",a);
else
printf("The Numbe %d is Odd",a);
getch();
}

Output
-7-
Enter The Number
5
The Number 5 is Odd
Enter The Number
6
The Number 6 is Even

13. Find out a given year is leap year or not

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter The Year");
scanf("%d",&a);
if((a%4==0&& a%100!=0)||a%400==0)
printf("The Year=%d is Leap Year",a);
else
printf("The Year=%d is not Leap Year",a);
getch();
}

Output
Enter The Year
2004
The Year=2004 is Leap Year
Enter The Year
3000
The Year=3000 is not Leap Year

14. Calculate Roots of a Quadratic Equation


//If ax2+bx+c=0 is a Quadratic Equation then roots of this equation calculated by
the formula (-b±√(b2 -4ac)/)2a
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,e;
float root1,root2;
clrscr();
printf("Enter The Vales of A ,B and C");
scanf("%f%f%f",&a,&b,&c);
e=b*b-4*a*c;
if(e>0)
-8-
{
d=sqrt(e); /* sqrt() is a Standard Library function Calculate Square root*/

root1=(-b+d)/2*a;
root2=(-b-d)/2*a;
printf("\nThe Root1=%f",root1);
printf("\nThe Root2=%f",root2);
}
else
printf("Roots are imaginary");
getch();
}

Output
Enter The Vales of A ,B and C
5
4
3
Roots are imaginary
Enter The Vale of A ,B and C
1
-1
-6

The Root1=3.000000
The Root2=-2.000000

15. Change the case of a character

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("Enter a Character--------");
scanf("%c",&c);
if(c>=65&&c<=90)
{
c=c+32;
printf("\nThe Output is---------%c",c);
}
else
{
c=c-32;
printf("\nThe Output is---------%c",c);
}
-9-
getch();
}

Output
Enter a Character--------a
The Output is---------A
Enter a Character--------d
The Output is---------D

16. Check the status of character (i.e. the character is vowel or not)

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("Enter a Character--------");
scanf("%c",&c);
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
printf("\nThe Character %c is Vowel",c);
else
printf("\nThe Character %c is not Vowel",c);
getch();
}

Output
Enter a Character--------A
The Character A is Vowel
Enter a Character--------a
The Character a is Vowel

Enter a Character--------B
The Character B is not Vowel

17. Calculate the Biggest Number out of three given numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b, c;
clrscr();
printf("Enter the value of A ,B and C--------");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&b>c)
printf("\nThe Number %d is Biggest ",a);
- 10 -
else
if(b>a&&a>c)
printf("\nThe Number %d is Biggest ",b);
else
printf("\nThe Number %d is Biggest ",c);
getch();
}

Output
Enter the value of A ,B and C--------
5
3
2
The Number 5 is Biggest

18. Write a C program to read marks from keyboard and your program should display
equivalent grade according to the following table
Marks Grade
80-100 Distinction
60-79 First Class
35-59 Second Class
0-34 Fail

#include<stdio.h>
#include<conio.h>
void main()
{
float math,phy, chem,avg;
clrscr();
printf("Enter the value of A ,B and C--------");
scanf("%f%f%f",&math,&phy,&chem);
avg=(math+phy+chem)/3;
if(avg>=80&&avg<=100)
printf("\nThe Marks %f is Distinction Grade",avg);
else
if(avg>=60&&avg<=79)
printf("\nThe Marks %f is First Class Grade",avg);
else
if(avg>=35&&avg<=59)
printf("\nThe Marks %f is Second Class Grade",avg);
else
printf("\nThe Marks %f is Fail",avg);
getch();
}

Output
Enter the value of Math ,Physics and Chemistry--------
- 11 -
60
80
90
The Marks 76.666664 is First Class Grade
Enter the value of Math ,Physics and Chemistry--------
30
30
20
The Marks 26.666666 is Fail

19. If cost price and selling price of an item is input through the keyboard, write a program
to determine seller has made profit or loss. Also calculate how much profit or loss.

#include<stdio.h>
#include<conio.h>
void main()
{
float cp,sp,profit,loss;
clrscr();
printf("Enter the value Cost price and Selling price--------");
scanf("%f%f%f",&cp,&sp);
profit=sp-cp; /*Profit=Cost price-Selling price */
loss=cp-sp; /* Loss=Selling price-Cost price */
if(profit>0)
printf("\nThe Seller has nade a Profit=%f",profit);
else
if(loss>0)
printf("\nThe Seller has nade a Loss=%f",loss);
else
if(profit==0)
printf("\nThe Seller has made a no Profit or no Loss");
getch();
}

Output
Enter the value Cost price and Selling price--------
100
75
The Seller has made a Loss=25.000000
Enter the value Cost price and Selling price--------
75
95
The Seller has made a Profit=20.000000
Enter the value Cost price and Selling price--------
90
90
The Seller has made a no Profit or no Loss
- 12 -
20. According to the Gregorian calendar, it was Monday on the date 01/01/1900.If any year
is input through the keyboard write a program to find out what is the day on 1st January of
this year.

#include<stdio.h>
#include<conio.h>
void main()
{
int lepday,begday,year;
long int normalday,totalday;
clrscr();
printf("Enter the Year--------");
scanf("%d",&year);
normalday=(year-1)*365L;
lepday=(year-1)/4-(year-1)/100+(year-1)/400;
totalday=normalday+lepday;
begday=totalday%7;
printf("\n First day of the year");
if(begday==0)
printf("\nMonday");
else
if(begday==1)
printf("\nTuesday");
else
if(begday==2)
printf("\nWednesday");
else
if(begday==3)
printf("\nThrusday");
else
if(begday==4)
printf("\Friday");
else
if(begday==5)
printf("\nSaturday");
else
if(begday==6)
printf("\nSunday");
getch();
}

Output
Enter the Year--------2006
First day of the year
Sunday
Enter the Year--------2008
First day of the year
- 13 -
Tuesday
Enter the Year--------2012
First day of the year
Sunday

21. Write a C program that reads a number from 1 to 7 and accordingly display MONDAY
to SUNDAY.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number between 1 to 7--------");
scanf("%d",&n);
switch(n)
{
case 1: printf("MONDAY");
break;
case 2: printf("TUESDAY");
break;
case 3: printf("WEDNESDAY");
break;
case 4: printf("THURSDAY");
break;
case 5: printf("FRIDAY");
break;
case 6: printf("SATURDAY");
break;
case 7: printf("SUNDAY");
break;
}
getch();
}

Output
Enter the number between 1 to 7--------5
FRIDAY

22. To simulate a menu program using switch case.

#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
- 14 -
float a,b,result;
clrscr();
printf("Enter the value of A and B--------");
scanf("%f%f",&a,&b);
printf("*************MENU**************");
printf(" 1.ADDITION");
printf(" 2.SUBTRACTION");
printf(" 3.MULTIPLICATION");
printf(" 4.DIVISION");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: result=a+b;
printf("The Result is-------%d",result);
break;
case 2: result=a-b;
printf("The Result is-------%d",result);
break;
case 3: result=a*b;
printf("The Result is-------%d",result);
break;
case 4: result=a/b;
printf("The Result is-------%d",result);
break;
default: printf("Wrong choice");
}
getch();
}

Output
Enter the value of A and B--------
5
6

23. To read any five numbers and print their sum.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,result=0,i;
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter the value of %d no. Number--------",i);
scanf("%d",&a);
- 15 -
result=result+a;
}
printf("The sum of these five number is----->%d",result);
getch();
}

Output
Enter the value of 1 no. Number--------9
Enter the value of 2 no. Number--------8
Enter the value of 3 no. Number--------7
Enter the value of 4 no. Number--------6
Enter the value of 5 no. Number--------5
The sum of these five number is----->35

24. To find out all the numbers divisible by 5 or 7 in between 1 to 20.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=20;i++)
{
if(i%5==0)
printf("\nThe Number ---->%d is divisible by 5",i);
if(i%7==0)
printf("\nThe Number ---->%d is divisible by 7",i);
}
getch();
}

Output
The Number ---->5 is divisible by 5
The Number ---->7 is divisible by 7
The Number ---->10 is divisible by 5
The Number ---->14 is divisible by 7
The Number ---->15 is divisible by 5
The Number ---->20 is divisible by 5

25. To find sum of digits of a given number.

#include<stdio.h>
#include<conio.h>
{
int n,i,b,sum=0;
clrscr();
- 16 -
printf("Enter the Number\n");
scanf("%d",&n);
for(i=n;i>=1;i=i/10)
{
b=i%10;
sum=sum+b;
}
printf("The sum of digits --->%d",sum);
getch();
}

Output
Enter the Number
456
The sum of digits --->15

26. Check whether a number is Perfect or not.


/* 6 is perfect number because if all the factors of 6 are added then result is =6(i.e.
1+2+3=6).*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,b,per=0;
clrscr();
printf("Enter the Number---->");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
per=per+i;
}
if(per==n)
printf("The given number--->%d is Perfect", per);
else
printf("The given number--->%d is not Perfect", n);
getch();
}

Output
Enter the Number---->6
The given number--->6 is Perfect
Enter the Number---->5
The given number--->5 is not Perfect

27. Check whether a number is Armstrong or not.


- 17 -
/*A number is Armstrong if sum of the cubes of the digits is same as the original number
(i.e. 153=13+53+33)*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,arm=0,i,b;
clrscr();
printf("Enter the Number-------->");
scanf("%d",&a);
for(i=a;i>=1;i=i/10)
{
b=a%10;
arm=arm+(b*b*b);
}
if(arm==a)
printf("The number ---->%d is Armstrong",a);
else
printf("The number ---->%d is not Armstrong",a);
getch();
}

Output
Enter the Number-------->153
The number ---->153 is Armstrong
Enter the Number-------->152
The number ---->152 is not Armstrong

28. Check whether a number is Palindrome or not.


/*A number is palindrome if the number is same of its reverse number
121)*/
(i.e. 121-

#include<stdio.h>
#include<conio.h>
void main()
{
int a,pal=0,i,b;
clrscr();
printf("Enter the Number-------->");
scanf("%d",&a);
for(i=a;i>=1;i=i/10)
{
b=i%10;
pal=pal*10+b;
}
if(pal==a)
- 18 -
printf("The number ---->%d is Palindrome", a);
else
printf("The number ---->%d is not Palindrome", a);
getch();
}

Output
Enter the Number-------->121
The number ---->121 is Palindrome
Enter the Number-------->122
The number ---->122 is not Palindrome

29. Check whether a number is prime or not.


/* A number is prime if the number is divisible by 1 and this number only
(i.e. 5 is prime number)*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,i;
clrscr();
printf("Enter the Number-------->");
scanf("%d",&a);
for(i=2;i<a;i++)
{
if(a%i==0)
break;
}
if(i==a)
printf("The number ---->%d is Prime",a);
else
printf("The number ---->%d is not Prime",a);
getch();
}

Output
Enter the Number-------->5
The number ---->5 is Prime
Enter the Number-------->9
The number ---->9 is not Prime

30. Calculate Factorial of a given number.

#include<stdio.h>
#include<conio.h>
void main()
- 19 -
{
int a,i,fact=1;
clrscr();
printf("Enter the Number-------->");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
fact=fact*i;
}
printf("The Factorial of %d is ---->%d",a,fact);
getch();
}

Output
Enter the Number-------->5
The Factorial of 5 is ---->120

31. Print Fibonacci series up to a given range

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,i,fib=1,n;
clrscr();
printf("Enter the Range------->");
scanf("%d",&n);
printf("\n");
printf("%d\t%d",a,b);
for(i=1;i<=n-2;i++)
{
fib=a+b;
a=b;
b=fib;
printf("\t%d",fib);
}
getch();
}

Output
Enter the Range------->9

0 1 1 2 3 5 8 13 21

32. Compute the summation for the following series


2+4+6+8…………………………+n

- 20 -
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i;
clrscr();
printf("Enter the last term------->");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
if(i%2==0)
{
printf("%d\t",i);
sum=sum+i;
}
}
printf("\n\nSum of the series-----> %d",sum);
getch();
}

Output
Enter the last term------->10

2 4 6 8 10
Sum of the series-----> 30

33. To compute the summation for the following series


1!+ 2!+3!+4!………………………+n!

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,fact=1,i;
clrscr();
printf("Enter the number term less than 8------->");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("\nFactorial of--->%d is---->%d",i,fact);
sum=sum+fact;
}
printf("\n\nSum of the series-----> %d",sum);
getch();
- 21 -
}

Output
Enter the number term less than 8------->7
Factorial of--->1 is---->1
Factorial of--->2 is---->2
Factorial of--->3 is---->6
Factorial of--->4 is---->24
Factorial of--->5 is---->120
Factorial of--->6 is---->720
Factorial of--->7 is---->5040

Sum of the series-----> 5913

34. To compute the summation for the following series


1+x+x2/2!+x3/3!..........................+xn/n!

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,sum=0,fact=1,i,x,p;
clrscr();
printf("Enter the number term less than 8------->");
scanf("%d",&n);
printf("\n Enter the value of X----->");
scanf("%d",&x);
printf("\n");
for(i=1;i<=n;i++)
{
p=pow(x,i);/*This is a standrad library function calculates a value raised to a power*/
fact=fact*i;
printf("\nFactorial of--->%d is---->%d p/fact---->%d",i,fact,p/fact);
sum=sum+p/fact;
}
printf("\n\nSum of the series-----> %d",sum);
getch();
}

Output
Enter the number term less than 8------->7
Enter the value of X----->3
Factorial of--->1 is---->1 p/fact---->3
Factorial of--->2 is---->2 p/fact---->4
Factorial of--->3 is---->6 p/fact---->4
Factorial of--->4 is---->24 p/fact---->3
- 22 -
Factorial of--->5 is---->120 p/fact---->2
Factorial of--->6 is---->720 p/fact---->1
Factorial of--->7 is---->5040 p/fact---->0

Sum of the series-----> 17

35. To find the mean and Standard deviation of n data


Mean= ∑xi/n Standard Deviation=√∑(xi)2/n-(∑xi/n)2

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
float mean,sd,sum1=0,sum2=0,x,p;
clrscr();
printf("Enter the number term ------->");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
printf("\n Enter the value of X%d---->",i);
scanf("%f",&x);
p=pow(x,2);/*This is a standrad library function calculates a value raised to a power*/
sum1=sum1+x;
sum2=sum2+p;
}
mean=sum1/n;
sd=sqrt(sum2/n-(mean*mean));
printf("\n\nMean----->%f SD-------->%f",mean,sd);
getch();
}

Output
Enter the number term ------->5
Enter the value of X1---->6
Enter the value of X2---->3
Enter the value of X3---->4
Enter the value of X4---->2
Enter the value of X5---->9
Mean----->4.800000 SD-------->2.481934

36. To print the following pattern:


*
**
***
- 23 -
****

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5

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

37. To print the following pattern:


1
2 3
4 5 6

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int r,i,j,k,t=1;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
- 24 -
{
for(j=1;j<=i;j++)
{
printf("%3d",t);
t++;
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->3
1
2 3
4 5 6

38. To print the following pattern:


1
0 1
0 1 0
1 0 1 0
1 0 1 0 1

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k,t=1;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
{
if(t%2==0)
printf("0");
else
printf("1");
t++;
}
printf("\n");
}
getch();
}
- 25 -
Output
Enter the number row ------->5

1
0 1
0 1 0
1 0 1 0
1 0 1 0 1

39. To print the following pattern:


****
***
**
*

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=0;j<=i-1;j++)
{
printf(" ");
}
for(k=0;k<=r-i;k++)
{
printf("*");
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5
*****
****
***
**
*

- 26 -
40. To print the following pattern:
*
**
***
****
*****

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=r-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5

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

41. To print the following pattern:


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

- 27 -
#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=r-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf(" *");
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5

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

42. To print the following pattern


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k,t=1;
clrscr();
printf("Enter the number row ------->");
- 28 -
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=r-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%4d",t);
t++;
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

43. To print the following Pascal Triangle:


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k,t=1;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=0;i<=r;i++)
{
for(j=0;j<=r-i;j++)
- 29 -
{
printf(" ");
}
for(k=0;k<=i;k++)
{
if(i==0||k==0)
t=1;
else
t=t*(i-k+1)/k;
printf("%2d",t);
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

44. To print the following Pascal Triangle:


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

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k;
clrscr();
printf("Enter the number row of upper pyramid ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
- 30 -
for(j=1;j<=r-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf(" *");
}
printf("\n");
}
for(i=r-1;i>=1;i--)
{
for(j=r-i;j>=1;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf(" *");
}
printf("\n");
}
getch();
}

Output
Enter the number row of upper pyramid ------->5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

45. To print the following Pattern:


1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

#include<stdio.h>
#include<conio.h>
void main()
- 31 -
{
int r,i,j,k,m;
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=r-i;j++)
{
printf(" ");
}
m=i;
for(k=1;k<=i;k++)
{
printf("%4d",m++);
}
m=m-2;
for(k=1;k<i;k++)
{
printf("%4d",m--);
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

46. To print the following Pattern:


ABCDEEDCBA
ABCD DCBA
ABC CBA
AB BA
A A

#include<stdio.h>
#include<conio.h>
void main()
{
int r,i,j,k,t=65,m;
- 32 -
clrscr();
printf("Enter the number row ------->");
scanf("%d",&r);
printf("\n");
for(i=1;i<=r;i++)
{
for(j=r;j>=i;j--)
{
printf("%2c",t);
t++;
}
m=t-1;
t=65;
for(k=2;k<=2*i-1;k++)
{
printf(" ");
}
for(j=r;j>=i;j--)
{
printf("%2c",m);
m--;
}
printf("\n");
}
getch();
}

Output
Enter the number row ------->5

ABCDEEDCBA
ABCD DCBA
ABC CBA
AB BA
A A

47. To reverse a number

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,b,rev=0;
clrscr();
printf("Enter the Number");
scanf("%d",&n);
for(i=n;i>=1;i=i/10)
- 33 -
{
b=i%10;
rev=rev*10+b;
}
printf("The reverse number of the given number--->%d",rev);
getch();
}

Output
Enter the Number------>456
The reverse number of the given number--->654

48. To check the number is binary or not

#include<stdio.h>
#include<conio.h>
void main()
{
int c,r;
long int n,t;
clrscr();
printf("Enter the Number--->");
scanf("%ld",&n);
t=n;
r=c=0;
while(n>0)
{
if(n%10==0||n%10==1)
c++;
r++;
n=n/10;
}
if(c==r)
printf("%ld number is a binary number",t);
else
printf("%ld number is not a binary number",t);
getch();
}

Output
Enter the Number--->123456
123456 number is not a binary number
Enter the Number--->1011101
1011101 number is a binary number

49. To print the number and its ASCII value (Range 48 to 60)

- 34 -
#include<stdio.h>
#include<conio.h>
void main()
{
int i=48;
clrscr();
while(i<=60)
{
printf("\nNumber----->%d ASCII------>%c",i,i);
i++;
}
getch();
}

Output
Number----->48 ASCII------>0
Number----->49 ASCII------>1
Number----->50 ASCII------>2
Number----->51 ASCII------>3
Number----->52 ASCII------>4
Number----->53 ASCII------>5
Number----->54 ASCII------>6
Number----->55 ASCII------>7
Number----->56 ASCII------>8
Number----->57 ASCII------>9
Number----->58 ASCII------>:
Number----->59 ASCII------>;
Number----->60 ASCII------><

50. A menu program using do-while

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int ch,ch1;
int a,b,result;
clrscr();
printf("\nEnter the value of A and B--------");
scanf("%d%d",&a,&b);
do
{
printf("\n*************MENU**************");
printf("\n 1.ADDITION");
printf("\n 2.SUBTRACTION");
printf("\n 3.MULTIPLICATION");
- 35 -
printf("\n 4.DIVISION");
printf("\n 5.EXIT");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: result=a+b;
break;
case 2: result=a-b;
break;
case 3: result=a*b;
break;
case 4: result=a/b;
break;
case 5: return;
default: printf("\n Wrong choice");
}
printf("\nThe Result is-------%d",result);
}while(1);

getch();
}

Output
Enter the value of A and B--------12
*************MENU**************
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
Enter your choice--1

The Result is-------18


*************MENU**************
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
Enter your choice--2

The Result is-------6


*************MENU**************
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
- 36 -
4. DIVISION
5. EXIT
Enter your choice—3
The Result is-------72
*************MENU**************
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
Enter your choice--4
The Result is-------2
*************MENU**************
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
Enter your choice--5

51. Use of goto statement

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
printf("\n Enter how many number you want to print-->");
scanf("%d",&n);
printf("\n Displayed number----");
i=1;
loop1:
printf("\n %d",i);
i++;
if(i<n)
goto loop1:
break;
}

Output
Enter how many number you want to print-->9

Displayed number----
1
2
3
4
- 37 -
5
6
7
8
9

52. Use of break statement in loop

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
printf("\n Enter how many number you want to print(more than 10)");
scanf("%d",&n);
printf("\n Displayed number----");
for(i=1;i<=n;i++)
{
printf("\n %d",i);
if(i==9)
break;
}
getch();
}

Output
Enter how many number you want to print(more than 10)25
Displayed number----
1
2
3
4
5
6
7
8
9

53. Use of break statement as substitute of exit() function

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,count;
clrscr();
printf("\n Enter how many number you want to print(more than 10)");
- 38 -
scanf("%d",&n);
printf("\n Displayed number----");
for(i=1;i<=n;i++)
{
count=1;
for(;;)
{
printf("\n %d",count);
count++;
if(count==9)
break;
}
}
getch();
}

Output
Enter how many number you want to print(more than 10) 11

Displayed number----
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8

54. Use of block statement

- 39 -
#include<stdio.h>
#include<conio.h>
void main()
{
int i=5;
if(i==5)
{ /* block statement starting here*/
printf(“\n I am INDIAN”);
printf(“\n I am BENGALY”);
printf(“\n I am a MAN”);
} /* block statement terminate here*/
getch();
}

Output
I am INDIAN
I am BENGALY
I am a MAN

55. To print the input array elements

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20]; /* a is an integer array and it maximum size is 20*/
int size,i;
clrscr();
printf("\nEnter how many number you want to store (less than 20)---->");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("\nEnter the a[%d] no. element---->",i);
scanf("%d",&a[i]);
}
printf("\nThe displayed Array\n ");
for(i=0;i<size;i++)
{
printf("\na[%d]----->%d",i,a[i]);
}
getch();
}

Output
Enter how many number you want to store (less than 20)---->5

Enter the a[0] no. element---->10


- 40 -
Enter the a[1] no. element---->20
Enter the a[2] no. element---->30
Enter the a[3] no. element---->40
Enter the a[4] no. element---->50

The displayed Array


a[0]----->10
a[1]----->20
a[2]----->30
a[3]----->40
a[4]----->50

56. To find the maximum element from given input array elements

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20];/* a is an integer array and it maximum size is 20*/
int size,i,b;
clrscr();
printf("\nEnter how many number you want to store (less than 20)---->");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("\nEnter the a[%d] no. element---->",i);
scanf("%d",&a[i]);
}
printf("\nThe displayed Array\n ");
for(i=0;i<size;i++)
{
printf("\na[%d]----->%d",i,a[i]);
}
b=a[0];
for(i=0;i<size;i++)
{
if(b<a[i])
b=a[i];
}
printf("\nThe maximum element ----->%d",b);
getch();
}

Output
Enter how many number you want to store (less than 20)---->5

Enter the a[0] no. element---->20


- 41 -
Enter the a[1] no. element---->10
Enter the a[2] no. element---->50
Enter the a[3] no. element---->40
Enter the a[4] no. element---->30

The displayed Array


a[0]----->20
a[1]----->10
a[2]----->50
a[3]----->40
a[4]----->30
The maximum element ----->50

57. To add the corresponding elements of two 1-D(one dimension) arrays and store the
result in a third 1-D array
c[i]=a[i]+b[i];

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],c[20];
int size1,size2,i;
clrscr();
printf("\nEnter how many numbers you want to store (less than 20) Array 1---->");
scanf("%d",&size1);
printf("\nEnter how many numbers you want to store (less than 20) Array 2---->");
scanf("%d",&size2);
printf("\n Enter Array 1 elements");
for(i=0;i<size1;i++)
{
printf("\nEnter the a[%d] no. element---->",i);
scanf("%d",&a[i]);
}
printf("\n Enter Array 1 elements");
for(i=0;i<size2;i++)
{
printf("\nEnter the b[%d] no. element---->",i);
scanf("%d",&b[i]);
}
printf("\nThe displayed Array 1\n ");
for(i=0;i<size1;i++)
{
printf("\na[%d]----->%d",i,a[i]);
}
printf("\nThe displayed Array 2\n ");
for(i=0;i<size2;i++)
- 42 -
{
printf("\na[%d]----->%d",i,b[i]);
}
printf("\nDisplay the Addition Result \n ");
if(size1==size2)
{
for(i=0;i<size1;i++)
{
c[i]=a[i]+b[i];
printf("\nc[%d]----->%d",i,c[i]);
}
}
getch();
}

Output
Enter how many numbers you want to store (less than 20) Array 1---->4
Enter how many numbers you want to store (less than 20) Array 2---->4
Enter Array 1 elements

Enter the a[0] no. element---->1


Enter the a[1] no. element---->2
Enter the a[2] no. element---->3
Enter the a[3] no. element---->4

Enter Array 2 elements

Enter the b[0] no. element---->4


Enter the b[1] no. element---->3
Enter the b[2] no. element---->2
Enter the b[3] no. element---->1

The displayed Array 1


a[0]----->1
a[1]----->2
a[2]----->3
a[3]----->4
The displayed Array 2

a[0]----->4
a[1]----->3
a[2]----->2
a[3]----->1
Display the Addition Result

c[0]----->5
c[1]----->5
- 43 -
c[2]----->5
c[3]----->5

58. Conversion of Decimal number to Binary number

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20];
int i,ele,j;
clrscr();
printf("\nEnter a Decimal Number---->");
scanf("%d",&ele);
i=0;
while(ele>0)
{
a[i]=ele%2;
i++;
ele=ele/2;
}
printf("\nThe binary number is-------->");
for(j=i-1;j>=0;j--)
printf("\t%d",a[j]);
getch();
}

Output
Enter a Decimal Number---->8
The binary number is--------> 1 0 0 0

59. Conversion of Decimal number to Octal number

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20];
int i,ele,j;
clrscr();
printf("\nEnter a Decimal Number---->");
scanf("%d",&ele);
i=0;
while(ele>0)
{
a[i]=ele%8;
i++;
- 44 -
ele=ele/8;
}
printf("\nThe octal number is-------->");
for(j=i-1;j>=0;j--)
printf("\t%d",a[j]);
getch();
}

Output
Enter a Decimal Number---->12345

The octal number is--------> 3 0 0 7 1

60. Conversion of Binary number to Decimal number

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[20],b;
int i,ele,j;
clrscr();
printf("\nEnter a Binary Number---->");
scanf("%d",&ele);
i=0;
while(ele>0)
{
a[i]=ele%10;
i++;
ele=ele/10;
}
j=i-1;
b=0;
while(j>=0)
{
b=b+(a[j]*pow(2,j));
j--;
}
printf("\nThe Decimal number is-------->");
printf("\t%d",b);
getch();
}

Output
Enter a Binary Number---->1000
The Decimal number is--------> 8
- 45 -
61. Conversion of Octal number to Decimal number

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[20],b;
int i,ele,j;
clrscr();
printf("\nEnter a Octal Number---->");
scanf("%d",&ele);
i=0;
while(ele>0)
{
a[i]=ele%10;
i++;
ele=ele/10;
}
j=i-1;
b=0;
while(j>=0)
{
b=b+(a[j]*pow(8,j));
j--;
}
printf("\nThe Decimal number is-------->");
printf("\t%d",b);
getch();
}

Output
Enter a Octal Number---->142
The Decimal number is--------> 98

62. To represent a Multi Dimentional Array in Matrix input

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20];
int row,col,i,j;
clrscr();
printf("\nEnter number of row(less than 20)---->");
scanf("%d",&row);
printf("\nEnter number of coloum(less than 20)---->");
- 46 -
scanf("%d",&col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nEnter the a[%d][%d] no. element---->",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nThe displayed Matrix is\n\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
getch();
}

Output
Enter number of row(less than 20)---->3

Enter number of coloum(less than 20)---->3

Enter the a[0][0] no. element---->1


Enter the a[0][1] no. element---->2
Enter the a[0][2] no. element---->3
Enter the a[1][0] no. element---->4
Enter the a[1][1] no. element---->5
Enter the a[1][2] no. element---->6
Enter the a[2][0] no. element---->7
Enter the a[2][1] no. element---->8
Enter the a[2][2] no. element---->9

The displayed Matrix is

1 2 3
4 5 6
7 8 9

63. Transpose of a Matrix

#include<stdio.h>
#include<conio.h>
void main()
- 47 -
{
int a[20][20];
int row,col,i,j;
clrscr();
printf("\nEnter number of row(less than 20)---->");
scanf("%d",&row);
printf("\nEnter number of coloum(less than 20)---->");
scanf("%d",&col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nEnter the a[%d][%d] no. element---->",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nThe displayed Matrix is\n\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\nTranspose of the Matrix is\n\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\t%d",a[j][i]);
}
printf("\n");
}
getch();
}

Output
Enter number of row(less than 20)---->3
Enter number of coloum(less than 20)---->3
Enter the a[0][0] no. element---->1
Enter the a[0][1] no. element---->2
Enter the a[0][2] no. element---->3
Enter the a[1][0] no. element---->4
Enter the a[1][1] no. element---->5
Enter the a[1][2] no. element---->6
Enter the a[2][0] no. element---->7
- 48 -
Enter the a[2][1] no. element---->8
Enter the a[2][2] no. element---->9

The displayed Matrix is

1 2 3
4 5 6
7 8 9

Transpose of the Matrix is

1 4 7
2 5 8
3 6 9

64. Addition of two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20];
int row1,col1,row2,col2,i,j;
clrscr();
printf("\nEnter number of row1(less than 20)---->");
scanf("%d",&row1);
printf("\nEnter number of coloum1(less than 20)---->");
scanf("%d",&col1);
printf("\nEnter number of row2(less than 20)---->");
scanf("%d",&row2);
printf("\nEnter number of coloum2(less than 20)---->");
scanf("%d",&col2);
printf("\n Enter the elements of MATRIX 1");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("\nEnter the a[%d][%d] no. element---->",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nThe displayed Matrix1\n\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("\t%d",a[i][j]);
- 49 -
}
printf("\n");
}
printf("\n Enter the elements of MATRIX 2");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\nEnter the b[%d][%d] no. element---->",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\nThe displayed Matrix2\n\n");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n");
}
if(row1==row2&&col1==col2)
{
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
c[i][j]= a[i][j]+b[i][j];
}
}
}
printf("\nThe Addition result of Matrix1 and Matrix2\n\n");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}

Output
Enter number of row1(less than 20)---->2
Enter number of coloum1(less than 20)---->2

- 50 -
Enter number of row2(less than 20)---->2
Enter number of coloum2(less than 20)---->2
Enter the elements of MATRIX 1
Enter the a[0][0] no. element---->1
Enter the a[0][1] no. element---->2
Enter the a[1][0] no. element---->3
Enter the a[1][1] no. element---->4
The displayed Matrix1

1 2
3 4
Enter the elements of MATRIX 2
Enter the b[0][0] no. element---->4
Enter the b[0][1] no. element---->3
Enter the b[1][0] no. element---->2
Enter the b[1][1] no. element---->1
The displayed Matrix2

4 3
2 1

The Addition result of Matrix1 and Matrix2

5 5
5 5

65. Multiplication of two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20];
int row1,col1,row2,col2,i,j,k;
clrscr();
printf("\nEnter number of row1(less than 20)---->");
scanf("%d",&row1);
printf("\nEnter number of coloum1(less than 20)---->");
scanf("%d",&col1);
printf("\nEnter number of row2(less than 20)---->");
scanf("%d",&row2);
printf("\nEnter number of coloum2(less than 20)---->");
scanf("%d",&col2);
printf("\n Enter the elements of MATRIX 1");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
- 51 -
{
printf("\nEnter the a[%d][%d] no. element---->",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\nThe displayed Matrix1\n\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\n Enter the elements of MATRIX 2");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\nEnter the b[%d][%d] no. element---->",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\nThe displayed Matrix2\n\n");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n");
}
if(col1==row2)
{
printf("\nMatrix Multiplication is possiable");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
c[i][j]=0;
for(k=0;k<col1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nThe Multiplication result of Matrix1 and Matrix2\n\n");
- 52 -
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
}
else
printf("\nMatrix Multiplication is not possible");
getch();
}

Output
Enter number of row1(less than 20)---->2
Enter number of coloum1(less than 20)---->2
Enter number of row2(less than 20)---->2
Enter number of coloum2(less than 20)---->2
Enter the elements of MATRIX 1
Enter the a[0][0] no. element---->2
Enter the a[0][1] no. element---->2
Enter the a[1][0] no. element---->2
Enter the a[1][1] no. element---->2
The displayed Matrix1

2 2
2 2

Enter the elements of MATRIX 2


Enter the b[0][0] no. element---->2
Enter the b[0][1] no. element---->2
Enter the b[1][0] no. element---->2
Enter the b[1][1] no. element---->2

The displayed Matrix2

2 2
2 2

Matrix Multiplication is possible


The Multiplication result of Matrix1 and Matrix2

8 8
8 8

- 53 -
66. Printing of part of string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[10];
printf(“\n Enter first string”);
scanf(“%s”,a);
printf(“\n Enter second string”);
scanf(“%3s”,b);
printf(“\n First string--->%s”,a);
printf(“\n Second string--->%s”,b);
getch();
}

Output
Enter first string
Good morning
Enter second string
Rabindranath
First string--->Good
Second string--->Rab

67. Printing of a string in different ways

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[14]=”indian citizen”;
printf(“\n\n”);
printf(“%14s\n”,a);
printf(“%5s\n”,a);
printf(“%14.6s\n”,a);
printf(“%-14.6s\n”,a);
printf(“%14.0s\n”,a);
printf(“%.4s\n”,a);
printf(“%s\n”,a);
getch();
}

Output
indian citizen
indian citizen
- 54 -
indian
indian

indi
indian citizen

68. To read a total string at a time

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
clrscr();
printf("\n Enter a string---->");
gets(a); /* It is capable of receiving only one string at a time, the plus point*/ /*with
gets() is it can receive a multi-word string*/
printf("%s",a);
getch();
}

Output
Enter a string---->Ram Krishna
Ram Krishna

69. To calculate the number of words in a string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
int b,i;
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n %s",a);
i=0;
while(a[i]!='\0')
{
if(a[i]==' ')
b++;
i++;
}
printf("\n\nThe number of words of the string is----->%d",b+1);
- 55 -
getch();
}

Output
Enter a string---->Ram Krishna is a great man
Ram Krishna is a great man
The number of words of the string is----->6

70. To find how many times does a character occur in a given string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],c;
int b=0,i;
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n %s",a);
printf("\n Enter a character---->");
scanf("%c",&c);
i=0;
while(a[i]!='\0')
{
if(a[i]==c)
b++;
i++;
}
printf("\n\nTotal time use of given character in the string is----->%d",b+1);
getch();
}

Output
Enter a string---->Ram Krishna
Ram Krishna
Enter a character---->r
Total time use of given character in the string is----->2

71. To replace all occurrences of a character in a given string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
- 56 -
char a[20],c,r;
int i;
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n %s",a);
printf("\n Enter a character which want to replace---->");
scanf("%c",&c);
printf("\nEnter a character which want to use for substitution---->");
fflush(stdin); /*clear the buffer*/
scanf("%c",&r);
i=0;
while(a[i]!='\0')
{
if(a[i]==c)
{
a[i]=r;
}
i++;
}
printf("\nAfter the operation\n");
printf("\n %s",a);
getch();
}

Output
Enter a string---->ram krishna
ram krishna
Enter a character which want to replace---->r
Enter a character which wants to use for substitution---->s

After the operation


sam ksishna

72. To delete all occurrences of a character in a given string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],c;
int i;
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n %s",a);
- 57 -
printf("\n Enter a character which wants to remove---->");
scanf("%c",&c);
i=0;
while(a[i]!='\0')
{
if(a[i]==c)
{
a[i]=' ';
}
i++;
}
printf("\nAfter the operation\n");
printf("\n %s",a);
getch();
}

Output
Enter a string---->book is important
book is important
Enter a character which wants to remove---->o
After the operation
b k is imp rtant

73: To reverse a string (use of strrev() function).

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
int b;
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n %s",a);
strrev(a);
printf("\n\nAfter reversing");
printf(" \n %s",a);
getch();
}

Output
Enter a string---->Ram Krishna

Ram Krishna

- 58 -
After reversing
anhsirK maR

74. To copy a given string into another string (use of strcpy() function).

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[20];
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n %s",a);
strcpy(b,a);
printf("\n\nAfter copy the string");
printf(" \n %s",b);
getch();
}

Output
Enter a string---->Ram Krishna
Ram Krishna
After copy the string
Ram Krishna

75. To find Achromatic String into a given string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
int i;
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n\n%s",a);
i=0;
printf("\n\n%c.",a[0]);
while(a[i]!='\0')
{
if(a[i]==' ')
{
i++;
- 59 -
printf("%c.",a[i]);
}
i++;
}
getch();
}

Output
Enter a string---->Ram Krishna
Ram Krishna
R.K.

76. To compare two strings using of strcmp() function.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[20];
int i;
clrscr();
printf("\n Enter a string1---->");
gets(a);
printf("\n Enter a string2---->");
gets(b);
printf("\n\nString1------>%s",a);
printf("\n\nString2------>%s",b);
i=strcmp(a,b);
if(i==0)
printf("\nThe strings are equal");
else
printf("\nThe strings are not equal");
getch();
}

Output
Enter a string1---->Ram Krishna
Enter a string2---->Ram Krishna
String1------>Ram Krishna
String2------>Ram Krishna
The strings are equal
Enter a string1---->Ram Krishna
Enter a string2---->Krishna
String1------>Ram Krishna
String2------>Krishna
The strings are not equal
- 60 -
77. To convert the string into a string having all letters in upper case, use of strupr()
function.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[20];
int i;
clrscr();
printf("\n Enter a string1---->");
gets(a);
printf("\n\nString1------>%s",a);
strupr(a);
printf("\n\n After operation");
printf("\n\nString1------>%s",a);
getch();
}

Output
Enter a string1---->Ram Krishna
String1------>Ram Krishna
After operation
String1------>RAM KRISHNA

78. Combining two strings (use of strcat() function).

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[20];
int i;
clrscr();
printf("\n Enter a string1---->");
gets(a);
printf("\n Enter a string2---->");
gets(b);
printf("\n\nString1------>%s",a);
printf("\n\nString2------>%s",b);
strcat(a,b);
printf("\n\n After operation");
printf("\n\nString1------>%s",a);
getch();
}
- 61 -
Output
Enter a string1---->Ram
Enter a string2---->Gopal
String1------>Ram
String2------>Gopal
After operation
String1------>RamGopal

79. To find a sub string in a given string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],c[10];
int i,j=0,found=0,e1,e2;
clrscr();
printf("\n Enter a string---->");
gets(a);
printf("\n %s",a);
printf("\n Enter a sub string which you want to search---->");
gets(c);
i=0;
e1=strlen(a);
e2=strlen(c);
if(e2<=e1)
{
while(a[i]!='\0'&&c[j]!='\0')
{
if(a[i]==c[j])
{
i++;
j++;
found=1;
}
else
{
j=0;
i++;
found=0;
}
if(a[i]=='\0'&&c[j]!='\0')
{
found=0;
break;
}
- 62 -
}
if(found==1)
printf("\nThe sub string is found\n");
else
printf("\nThe sub string is not found");
}
else
printf("\nThe sub string is greater than main string");
getch();
}

Output
Enter a string---->Ram Krishna is great
Ram Krishna is great
Enter a sub string which you want to search---->is

The sub string is found

80. To find out a sub string in a string by using strstr()

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[25];
char b[10];
clrscr();
printf("\n Enter a string-->");
gets(s);
printf("\n Enter the sub string-->");
gets(b);
if(strstr(s,b)==NULL)
printf("\n String is not found");
else printf("\n String is found");
getch();
}

Output
Enter a string-->god is good
Enter the sub string-->is
String is found
Enter a string-->god is good
Enter the sub string-->doog
String is not found

- 63 -
81. Finding factorial of a number using function

#include<stdio.h>
#include<conio.h>
void factorial(int);
void main()
{
int a;
clrscr();
printf("Enter the Number-------->");
scanf("%d",&a);
factorial(a);
getch();
}
void factorial(int n)
{
int i,fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The number ---->%d th factorial---->%d",n,fact);
}

Output
Enter the Number-------->5
The number ---->5 th factorial---->120

82. To calculate the Least common multiplication between two numbers

#include<stdio.h>
#include<conio.h>
void lcm(int,int);
int gcd(int, int);
void main()
{
int a,b;
clrscr();
printf("Enter the Number A-------->");
scanf("%d",&a);
printf("Enter the Number B-------->");
scanf("%d",&b);
lcm(a,b);
getch();
}
void lcm(int n,int m)
{
- 64 -
int r;
if(n<m)
r=(n*m)/gcd(n,m);
else
r=(n*m)/gcd(m,n);
printf("\n LCM of %d and %d = %d",n,m,r);
}
int gcd(int n,int m)
{
int r;
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
return(n);
}

Output
Enter the Number A-------->12
Enter the Number B-------->15
LCM of 12 and 15 = 60

83. A C-Program regarding Strong number


/* If the sum of factorials of all digits of a number is equal to the number, it is called strong
number.
Example: 145 is a strong number
Factorial of 5+factorial of 4+factorial of 1=145 */

#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int a,t,s=0,r;
clrscr();
printf("Enter the Number-------->");
scanf("%d",&a);
t=a;
while(a>0)
{
r=a%10;
s=s+factorial(r);
a=a/10;
}
- 65 -
if(t==s)
printf("\n The number %d is strong number",t);
else
printf("\n The number %d is not strong number",t);
getch();
}
int factorial(int n)
{
int i,fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
return(fact);
}

Output
Enter the Number-------->145

The number 145 is strong number

Enter the Number-------->121

The number 121 is not strong number

84. To calculate the Greatest common divisor between two numbers

#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter the Number A-------->");
scanf("%d",&a);
printf("Enter the Number B-------->");
scanf("%d",&b);
if(a<b)
printf("\nGCD of %d and %d= %d",a,b,gcd(a,b));
else
printf("\nGCD of %d and %d= %d",a,b,gcd(b,a));
getch();
}
int gcd(int n,int m)
{
int r;
- 66 -
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
return(n);
}

Output
Enter the Number A-------->12
Enter the Number B-------->15

GCD of 12 and 15= 3

85. A C-Program where call by value is used

#include<stdio.h>
#include<conio.h>
int sample(int,int);
void main()
{
int x,y,r;
clrscr();
printf("\n Enter the value of X");
scanf("%d",&x);
printf("\n Enter the value of Y");
scanf("%d",&y);
r=x+y;
printf("\n The numbers are displayed before function call");
printf("\n X=%d\tY=%d\tR=%d",x,y,r);
r=sample(x,y);
printf("\n The numbers are displayed after function call are");
printf("\n X=%d\tY=%d\tR=%d",x,y,r);
getch();
}
int sample(int a,int b)
{
int result;
a=10;
b=20;
result=a+b;
printf(“\n Inside the function”);
printf(“\n A=%d B=%d R=%d”,a,b,result);
return(result);
}
- 67 -
Output
Enter the value of X 25
Enter the value of Y 35
The numbers are displayed before function call
X=25 Y=35 R=60
Inside the function
A=10 B=20 R=30
The numbers are displayed after function call are
X=25 Y=35 R=30

86. A C-Program where call by reference is used

#include<stdio.h>
#include<conio.h>
int sample(int*,int*);
void main()
{
int x,y,r;
clrscr();
printf("\n Enter the value of X");
scanf("%d",&x);
printf("\n Enter the value of Y");
scanf("%d",&y);
r=x+y;
printf("\n The numbers are displayed before function call");
printf("\n X=%d\tY=%d\tR=%d",x,y,r);
r=sample(&x,&y);
printf("\n The numbers are displayed after function call");
printf("\n X=%d\tY=%d\tR=%d",x,y,r);
getch();
}
int sample(int *a,int *b)
{
int result;
*a=10;
*b=20;
result=*a+*b;
return(result);
}

Output
Enter the value of X 25
Enter the value of Y 35
The numbers are displayed before function call
X=25 Y=35 R=60
The numbers are displayed after function call
X=10 Y=20 R=30
- 68 -
87. To Display an Array using a function

#include<stdio.h>
#include<conio.h>
void display(int [],int);
void main()
{
int a[20],i,n;
clrscr();
printf("\n Enter the size of array");
scanf("%d",&n);
printf("\n Enter the array element");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Display the Array");
display(a,n);
getch();
}
void display(int x[],int m)
{
int j;
for(j=0;j<m;j++)
printf("\n%d",x[j]);
}

Output:
Enter the size of array5
Enter the array element
1
2
3
4
5
Display the Array
1
2
3
4
5

88. To find out the factorial of a number using recursion

#include<stdio.h>
#include<conio.h>
long int factorial_num(int);
void main()
{
- 69 -
int n;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
printf("\n The Factorial of %d number is %ld",n,factorial_num(n));
getch();
}
long int factorial_num(int n)
{
if(n==0)
return(1);
else
return(n*factorial_num(n-1));
}

Output
Enter a number10
The Factorial of 10 numbers is 3628800

89. To generate the Fibonacci series using recursion

#include<stdio.h>
#include<conio.h>
void fib_num(int);
void main()
{
int n;
clrscr();
printf("\n Enter the number of term");
scanf("%d",&n);
printf("\n The Fibonacci series as follow\n");
fib_num(n);
getch();
}
void fib_num(int n)
{
static int a,b;
int c;
if(n<2)
{
a=0;
b=1;
}
else
{
fib_num(n-1);
c=b;
- 70 -
b=a+b;
a=c;
}
printf("\t%d",a);
}

Output
Enter the number of term10
The Fibonacci series as follow
0 1 1 2 3 5 8 13 21 34

90. To take & display a Student-Record

#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int roll;
char name[20];
char address[20];
char trade[10];
};
struct student s;
clrscr();
printf("\n Enter the Roll Number---");
scanf("%d",&s.roll);
printf("\n Enter the Name---");
scanf("%s",s.name);
printf("\n Enter the Address---");
scanf("%s",s.address);
printf("\n Enter the Trade---");
scanf("%s",s.trade);
printf("\n\n The Student Record\n\n" );
printf("\tRoll\tName\tAddress\tTrade\n\n");
printf("\t%d\t%s\t%s\t%s",s.roll,s.name,s.address,s.trade);
getch();
}

Output
Enter the Roll Number---1
Enter the Name---Vivak
Enter the Address---Kolkata
Enter the Trade---CSE
The Student Record
Roll Name Address Trade
- 71 -
1 Vivak Kolkata CSE

91. A C-Program handling a Student-Database

#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int roll;
char name[20];
int marks[10];
int total;
float avg;
};
struct student s;
int sub_no,i;
clrscr();
s.total=0;
printf("\n Enter the Roll Number---");
scanf("%d",&s.roll);
printf("\n Enter the Name---");
scanf("%s",s.name);
printf("\n Enter the Number of subject---");
scanf("%d",&sub_no);
printf(" \n Enter the marks");
for(i=0;i<sub_no;i++)
{
scanf("%d",&s.marks[i]);
s.total=s.total+s.marks[i];
}
s.avg=s.total/sub_no;
printf("\n\n The Student Record\n\n" );
printf("Marks");
for(i=0;i<sub_no;i++)
printf("\t%d",s.marks[i] );
printf("\n\n\tRoll\tName\tTotal\tAverage\n\n");
printf("\t%d\t%s\t%d\t%f",s.roll,s.name,s.total,s.avg);
getch();
}

Output
Enter the Roll Number---1
Enter the Name---Ratan
Enter the Number of subject---5
Enter the marks
- 72 -
90
85
84
80
78

The Student Record

Marks 90 85 84 80 78

Roll Name Total Average

1 Ratan 417 83.000000

92. To create a Employee database

#include<stdio.h>
#include<conio.h>
void main()
{
struct employee
{
int id;
char name[20];
char address[20];
};
struct employee e[50];
int size,i;
clrscr();
printf("\n Enter how many records you want to insert");
scanf("%d",&size);
for(i=1;i<=size;i++)
{
printf("\n %d.Enter the ID---",i);
scanf("%d",&e[i].id);
printf("\n %d.Enter the Name---",i);
scanf("%s",e[i].name);
printf("\n %d.Enter the Address---",i);
scanf("%s",e[i].address);
}
printf("\n\n The Employee Record\n\n" );
printf("\tID\tName\tAddress\n\n");
for(i=1;i<=size;i++)
{
printf("\n\t%d\t%s\t%s",e[i].id,e[i].name,e[i].address);
}
getch();
- 73 -
}

Output
Enter how many records you want to insert3

1.Enter the ID---1


1.Enter the Name---Avik
1.Enter the Address---Burdwan
2.Enter the ID---2
2.Enter the Name---Arpan
2.Enter the Address---Kolkata
3.Enter the ID---3
3.Enter the Name---Souvik
3.Enter the Address---Delhi

The Employee Record


ID Name Address
1 Avik Burdwan
2 Arpan Kolkata
3 Souvik Delhi3

93. To store numbers of students and date of birth

#include<stdio.h>
#include<conio.h>
void main()
{
struct date
{
int day;
int month;
int year;
};
struct student
{
int roll;
char name[20];
char address[20];
struct date dob;
};
struct student s[50];
int size,i;
clrscr();
printf("\n Enter how many record you want to insert");
scanf("%d",&size);
for(i=1;i<=size;i++)
{
- 74 -
printf("\n %d.Enter the Roll---",i);
scanf("%d",&s[i].roll);
printf("\n %d.Enter the Name---",i);
scanf("%s",s[i].name);
printf("\n %d.Enter the Address---",i);
scanf("%s",s[i].address);
printf("Date of Birth\n");
printf("Day--");
scanf("%d",&s[i].dob.day);
printf("Month--");
scanf("%d",&s[i].dob.month);
printf("Year--");
scanf("%d",&s[i].dob.year);
}
printf("\n\n The Students Record\n\n" );
printf("\tRoll\tName\tAddress\tDate of Birth\n\n");
for(i=1;i<=size;i++)
{
printf("\n\t%d\t%s\t%s\t%d:%d:%d",s[i].roll,s[i].name,s[i].address,s[i].dob.day,s[i].dob.month,s[
i].dob.year);
}
getch();
}

Output
Enter how many record you want to insert3
1.Enter the Roll---1
1.Enter the Name---Avik
1.Enter the Address---Burdwan
Date of Birth
Day--12
Month--03
Year--1982
2.Enter the Roll---2
2.Enter the Name---Arpan
2.Enter the Address---Kolkata
Date of Birth
Day--15
Month--05
Year--1982
3.Enter the Roll---3
3.Enter the Name---Souvik
3.Enter the Address---Delhi
Date of Birth
Day--02
Month--01
Year--1982
- 75 -
The Students Record

Roll Name Address Date of Birth


1 Avik Burdwan 12:3:1982
2 Arpan Kolkata 15:5:1982
3 Souvik Delhi 2:1:1982

94. Example of Pointer to a Structure

#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int roll;
char name[20];
char address[20];
char trade[10];
}*s1;
clrscr();
printf("\n Enter the Roll Number,Name,Address,Trade---");
scanf("%d%s%s%s",&s1->roll,s1->name,s1->address,s1->trade);
printf("\n%d %s %s %s",s1->roll,s1->name,s1->address,s1->trade);

getch();
}

Output
Enter the Roll Number,Name,Address,Trade---1
Antony
Delhi
CSE

1 Antony Delhi CSE

95. A C-Program handling the basic record of a person

#include<stdio.h>
#include<conio.h>
void main()
{
struct person
{
char sex;
float height;
float weight;
- 76 -
};
union bio
{
struct person rec;
};
union bio b;
b.rec.sex='M';
b.rec.height=5.9;
b.rec.weight=64.5;
clrscr();
printf(" Person Record\n\n" );
printf("\nSex--%c",b.rec.sex);
printf("\nHeight--%f",b.rec.height);
printf("\nWeight--%f",b.rec.weight);
getch();
}

Output
Person Record
Sex--M
Height--5.900000
Weight--64.500000

96. A basic C-Program using pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int j=5;
int ptr,**ptr2;
ptr=&j;
ptr2=&ptr;
printf("\nThe value of j=%d",j);
printf("\nThe address of j=%u",&j);
printf("\n\nthe address of j=%u",ptr);
printf("\nthe value of j=%d",*ptr);
printf(“\nthe value of j=%d”,**ptr2);
getch();
}

Output:
The value of j=5
The address of j=6524
the address of j=6524
the value of j=5
the value of j=5
- 77 -
97. One Dimension Array representation

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={7,9,4,8,2};
int *ptr;
int i=0;
ptr=&a[0];
clrscr();
while(*ptr!='\0')
{
printf("\n\nthe address of a[%d]=%u",i,ptr);
printf("\nthe value of a[%d]=%d",i,*ptr);
ptr++;
i++;
}
getch();
}

Output:
the address of a[0]=65516
the value of a[0]=7

the address of a[1]=65518


the value of a[1]=9

the address of a[2]=65520


the value of a[2]=4

the address of a[3]=65522


the value of a[3]=8

the address of a[4]=65524


the value of a[4]=2

/* The elements of array are stored in the memory as shown*/


a[0] a[1] a[2] a[3] a[4]
value 7 9 4 8 2

65516 65518 65520 65522 65524

98. Multi-Dimension Array representation by pointer

#include<stdio.h>
#include<conio.h>
- 78 -
void main()
{
int i,j,a[10][10],r,c;
clrscr();
printf("Enter the row of matrix----");
scanf("%d",&r);
printf("Enter the coloum of matrix----");
scanf("%d",&c);
printf("\nEnter the matrix elements----");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The displayed array\n\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",*(*(a+i)+j));
}
printf("\n\n");
}
getch();
}

Output
Enter the row of matrix----3
Enter the coloum of matrix----3
Enter the matrix elements----
20
21
22
23
24
25
26
27
28

The displayed array

20 21 22

23 24 25
- 79 -
26 27 28

99. One Dimension Array representation by pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int i,*a[10],n;
clrscr();
printf("Enter the size of array----");
scanf("%d",&n);
printf("\nEnter the array elements----");
for(i=0;i<n;i++)
scanf("%d",a[i]);
printf("\n The displayed array\n\n");
for(i=0;i<n;i++)
printf("\t%d",*a[i]);
getch();
}

Output
Enter the size of array----5
Enter the array elements----
12
1
34
45
56

The displayed array

12 1 34 45 56

100. Counting the Even and Odd number

#include<stdio.h>
#include<conio.h>
void main()
{
int i,*a[10],n,even=0,odd=0;
clrscr();
printf("Enter the size of array----");
scanf("%d",&n);
printf("\nEnter the array elements----");
for(i=0;i<n;i++)
- 80 -
scanf("%d",a[i]);
printf("\n The displayed array\n\n");
for(i=0;i<n;i++)
printf("\t%d",*a[i]);
for(i=0;i<n;i++)
{
if(*a[i]%2==0)
even++;
else
odd++;
}
printf("\n Total even number=%d",even);
printf("\n Total odd number=%d",odd);
getch();
}

Output
Enter the size of array----10

Enter the array elements----


11
12
13
14
15
16
17
18
19
20

The displayed array

11 12 13 14 15 16 17 18 19 20
Total even number=5
Total odd number=5

101. Handling the biggest and the lowest value using Pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int i,*a[10],n,*big,*low;
clrscr();
printf("Enter the size of array----");
scanf("%d",&n);
- 81 -
printf("\nEnter the array elements----");
for(i=0;i<n;i++)
scanf("%d",a[i]);
printf("\n The displayed array\n\n");
for(i=0;i<n;i++)
printf("\t%d",*a[i]);
*big=*a[0];
*low=*a[0];
for(i=0;i<n;i++)
{
if(*big<*a[i])
*big=*a[i];
if(*low>*a[i])
*low=*a[i];
}
printf("\n Biggest number=%d",*big);
printf("\n Lowest number=%d",*low);
getch();
}

Output
Enter the size of array----10
Enter the array elements----
10
11
12
3
14
15
34
16
17
18

The displayed array

10 11 12 3 14 15 34 16 17 18
Biggest number=34
Lowest number=3

102. Making the cosine table of given values

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.141
double cos(double);
- 82 -
double table(double (*f)(),double, double,double);
void main()
{
clrscr();
printf("\n The Cosine Table");
table(cos,0.0,PI,0.5);
getch();
}
double table(double (*f)(), double min, double max, double step)
{ /* (*f)() is a pointer to function*/
double a, value;
for(a=min;a<=max;a+=step)
{
value=(*f)(a);
printf("\n%5.2f %10.4f",a,value);
}
}

Output
The Cosine Table
0.00 1.0000
0.50 0.8776
1.00 0.5403
1.50 0.0707
2.00 -0.4161
2.50 -0.8011
3.00 -0.9900

103. To declare a pointer to pointer variable and to display the contents of these pointers

#include<stdio.h>
#include<conio.h>
void main()
{
int p;
int *ptr1,**ptr2;
clrscr();
printf("\n Enter a number--");
scanf("%d",&p);
ptr1=&p;
ptr2=&ptr1;
printf("\nThe entered number=%d",p);
printf("\n The first pointer value=%d",*ptr1);
printf("\n The second pointer value =%d",**ptr2);
getch();
}

- 83 -
Output
Enter a number--99

The entered number=99


The first pointer value=99
The second pointer value =99

104. To represent the student record using structure pointer

#include<stdio.h>
#include<conio.h>
struct student
{
char name[10];
int roll;
float avg;
};
void main()
{
struct student record,*ptr;
int n,i;
clrscr();
printf("\nEnter the name--");
scanf("%s",record.name);
printf("\nEnter the roll--");
scanf("%d",&record.roll);
printf("\nEnter the average--");
scanf("%f",&record.avg);
printf("\nThe Student Record\n");
ptr=&record;
printf("\n %s\t\t %d\t\t %f",ptr->name,ptr->roll,ptr->avg );
getch();
}

Output
Enter the name--MOON
Enter the roll--101
Enter the average--95.23
The Student Record
MOON 101 95.230003

- 84 -

You might also like