Meaning of Format Specification /N /R /T /B and Uses
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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
- 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
#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
#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
#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
#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
*
* *
* * *
* * * *
* * * * *
#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
#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
#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
*
**
***
****
*****
- 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
*
* *
* * *
* * * *
* * * * *
#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
#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
#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
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
#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
#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
#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
#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------><
#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
#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
#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
#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
- 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
#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
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
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
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
#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
#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
#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
#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
1 2 3
4 5 6
7 8 9
#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
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
#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
5 5
5 5
#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
2 2
2 2
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
#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
#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
#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
#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
#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
#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
#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.
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
Marks 90 85 84 80 78
#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
#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
#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
#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
#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
#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
20 21 22
23 24 25
- 79 -
26 27 28
#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
12 1 34 45 56
#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
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
10 11 12 3 14 15 34 16 17 18
Biggest number=34
Lowest number=3
#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
#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 -