You are on page 1of 56

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

/*program for understand simple if logic*/


main()
{
int a,b,c,d;
float ratio;
clrscr();
printf("enter four integer values\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(c-d!=0)
{
ratio=(float)(a+b)/(float)(a-b);
printf("Ratio=%f\n",ratio);
}
printf("\nc-d value is zero");
getch();
}
************************************************************************
/*program for understand if-else logic*/
main()
{
int m1,m2,m3;
clrscr();
printf("enter m1,m2,m3 marks");
scanf("%d%d%d",&m1,&m2,&m3);
if(m1>=40&&m2>=40&&m3>=40)
{
printf("\npass");
}
else
{
printf("\nfail");
}
getch();
}
************************************************************************
/*program for understand nested if */
main()
{
int m1,m2,m3;
clrscr();
printf("enter m1,m2,m3 marks");
scanf("%d%d%d",&m1,&m2,&m3);
if(m1>=40)
{
if(m2>=40)

{
if(m3>=40)
{
printf("\nPASS");
}
else
{
printf("\nFAIL");
}
}
else
{
printf("\nFAIL");
}
}
else
{
printf("\nFAIL");
}
getch();
}
************************************************************************
/*program for understand else-if ladder*/
main()
{
int m1,m2,m3;
clrscr();
printf("enter m1,m2,m3 marks\n");
scanf("%d%d%d",&m1,&m2,&m3);
if(m1<40)
{
printf("\nFAIL");
}
else if(m2<40)
{
printf("\nFAIL");
}
else if(m3<40)
{
printf("\n FAIL");
}
else
{

printf("\nPASS");
}
getch();
}
************************************************************************
/* program to find out grade of the student*/
main()
{
int m1,m2,m3,avg;
clrscr();
printf("enter m1,m2,m3 marks\n");
scanf("%d%d%d",&m1,&m2,&m3);
if(m1>=40&&m2>=40&&m3>=40)
{
avg=(m1+m2+m3)/3;
printf("\navg=%d",avg);
if(avg>=70)
printf("\n grade A with distinction");
else if(avg>=60)
printf("\n grade A");
else if(avg>=50)
printf("\n grade B");
else
printf("\n grade C");
}
else
{
printf("\n fail ");
}
getch();
}
************************************************************************
/*ELECTRICITY BILL*/
/* AN ELECTRIC POWER DISTRIBUTION COMPANY CHAREGES ITS
DOMESTIC CONSUMER AS FOLLOWS
CONSUMPTION UNITS
RATE OF CHARGE
0-200
Rs.O.50 PER UNIT
201-400
Rs.100 PLUS Rs.0.65 PER UNIT EXCESS OF 200
401-600
Rs.230 PLUS Rs.0.80 PER UNIT EXCESS OF 400
600 ABOVE
RS.390 PLUS Rs.1.00 PER UNIT ECCESS OF 600*/
main()
{
int units,custno;
float charges;
clrscr();
printf("ENTER CUSTOMER NO: AND UNITS CONSUMED\n");

scanf("%d%d",&custno,&units);
if(units<=200)
charges=0.5*units;
else if(units<=400)
charges=100+0.65*(units-200);
else if(units<=600)
charges=230+0.8*(units-400);
else
charges=390+(units-600);
printf("\n customer no:%d:charges=%.2f",custno,charges);
getch();
}
************************************************************************
/* program to undrestand switch*/
main()
{
int num;
clrscr();
printf("\n enter a number\n");
scanf("%d",&num);
switch(num)
{
case 1:
printf("\n one");
break;
case 2:
printf("\n two");
break;
case 3:
printf("\n three");
break;
default:
printf("\n default");
break;/*optional*/
}
getch();
}
************************************************************************
#include<stdio.h>
main()
{
char ch;
clrscr();
printf("enter ur choice\n");
scanf("%c",&ch);
switch(ch)

{
case 'r': printf("red");
break;
case 'g': printf("green");
break;
case 'w': printf("white");
break;
default: exit(0);
}
}
************************************************************************
/*write a program to provide multiple functions such as
1.add 2.sub 3.mul 4. division 5. remainder 0. exit*/
main()
{
int a,b,ch;
clrscr();
printf("\n\tMENU");
printf("\n\t[1]ADD");
printf("\n\t[2]SUB");
printf("\n\t[3]MUL");
printf("\n\t[4]DIV");
printf("\n\t[5]REMINDER");
printf("\n\t[0]EXIT");
printf("\n\nENTER UR CHOICE\n");
scanf("%d",&ch);
if(ch<=5&&ch>0)
{
printf("\nEnter 2 numbers\n");
scanf("%d%d",&a,&b);
}
switch(ch)
{
case 1:
printf("\nADD=%d",a+b);
break;
case 2:
printf("\nSUB=%d",a-b);
break;
case 3:printf("\nMUL=%d",a*b);
break;
case 4:
printf("\nDIV=%d",a/b);
break;
case 5:
printf("\n REMINDER=%d",a%b);

break;
case 0:
exit();/*terminates the program*/
break;
default:
printf("\n INVALID CHOICE");
}
getch();
}
************************************************************************
/*write a program to convert years into
1.minutes 2.hours 3.days 4.months 5.seconds*/
main()
{
long int ch,min,hrs,ds,mon,yrs,sec;
clrscr();
printf("\n[1]MINUTS");
printf("\n[2]HOURS");
printf("\n[3]DAYS");
printf("\n[4]MONTHS");
printf("\n[5]SECONDS");
printf("\n[0]EXIT");
printf("\n ENTER UR CHOICE\n");
scanf("%ld",&ch);
if(ch>0&&ch<6)
printf("Enter Years");
scanf("%ld",&yrs);
mon=yrs*12;
ds=mon*30;
ds=ds+yrs*5;
hrs=ds*24;
min=hrs*60;
sec=min*60;
switch(ch)
{
case 1:
printf("\nMINUTES:%ld",min);
break;
case 2:
printf("\nHOURS:%ld",hrs);
break;
case 3:
printf("\nDAYS:%ld",ds);
break;
case 4:
printf("\nMONTHS:%ld",mon);

break;
case 5:
printf("\nSECONDS:%ld",sec);
break;
case 0:
exit();
break;
default:
printf("\n INVALID CHOICE");
}
getch();
}
************************************************************************
/*program to find out even or odd using goto*/
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr();
printf("enter a number:\n");
scanf("%d",&x);
if(x%2==0)
goto ss;
else
printf("ODDNUM:%d",x);
exit();
ss:
printf("\nEVENNUM:%d",x);
getch();
}
************************************************************************
/*program to check leapyear or not*/
main()
{
int year;
clrscr();
input:
printf("\nenter year\n");
scanf("%d",&year);
if(year%4==0)
goto sm;
else
printf("\n%d is a normal year",year);
goto input;

sm:
printf("%d is a leapyear",year);
getch();
}
************************************************************************
/*program to display numbers from 1 to 10*/
#include<stdio.h>
main()
{
int i;
clrscr();
i=1;
while(i<=10)
{
printf("%d\t",i);
i++;
}
getch();
}
************************************************************************
/*program to display num from 1 to n*/
#include<stdio.h>
main()
{
int i,n;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
i=1;
while(i<=n)
{
printf("%d\t",i);
i++;
}
getch();
}
************************************************************************
/*disply even numbers in given n limit*/
#include<stdio.h>
main()
{
int i,n;
clrscr();
printf("enter n value\n");

scanf("%d",&n);
i=2;
while(i<=n)
{
printf("%d\t",i);
i=i+2;
}
getch();
}
************************************************************************
/*program to find out sum of the numbers from 1 to n*/
main()
{
int i=1,sum=0,n;
clrscr();
printf("\nenter the limit");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\n SUM:%d",sum);
getch();
}
************************************************************************
/*program to find sum of digits of a num*/
main()
{
int sum=0,r,n;
clrscr();
printf("\n enter a number\n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("\n sum of digits:%d",sum);
getch();
}
************************************************************************

************************************************************************
/*program to make the reverse of a given num*/
main()
{
int n,rev=0,r;
clrscr();
printf("\n enter the number\n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("\n REVERSE NUM:%d",rev);
getch();
}
************************************************************************
/*program to check the given num palindrome*/
main()
{
int n,rev=0,r,temp;
clrscr();
printf("\n enter a number\n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
if(rev==temp)
printf("\npalindrome:%d",temp);
else
printf("\n not a palindrome:%d",temp);
getch();
}
************************************************************************
/*program to find out MAX and MIN of a given numbers*/
main()
{
int num,hn,c=1,max,min;
clrscr();
printf("\n how many numbers\n");
scanf("%d",&hn);

10

printf("\n enter a number\n");


scanf("%d",&num);
max=num;
min=num;
while(c<hn)
{
printf("\n enter a number\n");
scanf("%d",&num);
if(num>max)
max=num;
else if(num<min)
min=num;
c++;
}
printf("\n MAX=%d MIN=%d",max,min);
getch();
}
************************************************************************
/*program to understand do-while*/
main()
{
int i=7;
clrscr();
do
{
printf("\n this is a program for do while loop");
i++;
}while(i<=5);
getch();
}
************************************************************************
/*display message using do-while*/
main()
{
int i=1;
clrscr();
do
{
printf("\n******srilekha*****");
i++;
}while(i<=10);
getch();
}
************************************************************************

11

************************************************************************
/*write a program to find out the cubes of 1 to 10 numbers*/
#include<math.h>
main()
{
long int i,j=1;
clrscr();
printf("\nnumbers\t\t\tcubes\n");
do
{
i=pow(j,3);
printf("%4ld %22ld\n",j,i);
j++;
}while(j<=10);
getch();
}
************************************************************************
/*program to understand for loop*/
main()
{
int i,n;
clrscr();
printf("\n enter the limit\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d",i);
}
getch();
}
************************************************************************
/*program using for loop*/
main()
{
int i,n;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
for(i=n;i>0;i--)
printf("%d",i);
getch();
}
************************************************************************

12

************************************************************************
/*program to check prime or not*/
main()
{
int i,n;
clrscr();
printf("\nenter number");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
printf("\n not a primr:%d",n);
exit();
}
}
printf("\n prime num:%d",n);
getch();
}
************************************************************************
/*program to find out the factorial of a given num*/
main()
{
int n;
long int fact;
clrscr();
printf("\n enter number\n");
scanf("%d",&n);
for(fact=1;n>=1;n--)
{
fact=fact*n;
}
printf("\n factorial=%ld",fact);
getch();
}
************************************************************************
/*program to display fibonacci series*/
/*0 1 1 2 3 5 8 13 21 34 55........n*/
main()
{
int n,f1=0,f2=1,sum=0,i=1;
clrscr();
printf("enter number of operations\n");
scanf("%d",&n);
while(i<=n)

13

{
printf("%d",f1);
sum=f1+f2;
f1=f2;
f2=sum;
i++;
}
getch();
}
************************************************************************
/*write a program to evaluate the following series*/
/*x=1/1+1/4+1/9+....1/n2*/
/*y=1/1+1/8+1/27+....1/n3*/
#include<math.h>
main()
{
int i,n;
float x=0,y=0;
clrscr();
printf("enter n terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
x=x+(1/pow(i,2));
y=y+(1/pow(i,3));
}
printf("value of x=%f",x);
printf("\nvalue of y=%f",y);
getch();
}
************************************************************************
/*write a program to find sum of the following series*/
/*1+2+3+...n*/
/*1^2+2^2+3^2....n^2*/
main()
{
int sum=0,ssum=0,i,j;
clrscr();
printf("enter number\n");
scanf("%d",&j);
printf("numbers:");
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n\nSquares:");
for(i=1;i<=j;i++)

14

{
printf("%5d",i*i);
sum=sum+i;
ssum=ssum+i*i;
}
printf("\n\nsum of numbers from 1 to %d :%d",j,sum);
printf("\n\nsum of squares of 1 to %d: %d",j,ssum);
getch();
}
************************************************************************
/* program to under stand nested switch*/
#include<stdio.h>
main()
{
int x,y;
clrscr();
printf("\nenter a number:\n");
scanf("%d",&x);
switch(x)
{
case 0:
printf("\n number is even");
break;
case 1:
printf("\n number is odd");
break;
default:
y=x%2;
switch(y)
{
case 0:
printf("\n number is even");
break;
default:
printf("\n number is odd");
}
}
getch();
}
************************************************************************

15

************************************************************************
/*write a program use nested if...else statment in switch()*/
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
printf("\n enter any ASCII Number:\n");
scanf("%d",&i);
switch(i)
{
case 'A':
printf("\n capital A");
break;
case 'B':
printf("\n capital B");
break;
case 'C':
printf("\n capital C");
break;
default:
if(i>47&&i<58)
printf("\n Digit:[%c]",i);
else if(i>=58&&i<=64)
printf("\nSymbol:[%c]",i);
else if(i>64&&i<91)
printf("\n Small:[%c]",i);
else
printf("\n invalidchoice");
}
getch();
}
************************************************************************
/*read an integer through keyboard.sort odd&even numbers by using while loop
write a program to add sum of odd & even numbers separately & disply result*/
main()
{
int a,c=1,odd=0,even=0;
float b;
clrscr();
printf("enter number\n");
scanf("%d",&a);
printf("ODD\tEVEN\n");

16

while(c<=a)
{
b=c%2;
while(b==0)
{
printf("\t%d",c);
even=even+c;
b=1;
}
b=c%2;
while(b!=0)
{
printf("\n%d",c);
odd=odd+c;
b=0;
}
c++;
}
printf("\n================");
printf("\n%d\t%d",odd,even);
getch();
}
************************************************************************
/*program to disply mathematical tables from 1 to 10*/
main()
{
int i,j;
clrscr();
for(i=1;i<=10;i++)
{
printf("\n");
for(j=1;j<=10;j++)
{
printf(" %d*%d=%d",i,j,i*j);
}
}
getch();
}
************************************************************************
/*program to disply following triangle
1
12
123
1234
12345
*/

17

main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%2d",j);
}
}
getch();
}
************************************************************************
/*program to display following traingle
1
22
333
4444
55555
*/
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%2d",i);
}
}
getch();
}
************************************************************************
/*program to display following traingle
12345
1234
123
12
1
*/
main()
{
int i,j;
clrscr();

18

for(i=5;i>=1;i--)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%2d",j);
}
}
getch();
}
************************************************************************
/*program to disply following traingle
54321
4321
321
21
1
*/
main()
{
int i,j;
clrscr();
for(i=5;i>=1 ; i--)
{
printf("\n");
for(j=i;j>=1;j--)
{
printf("%2d",j);
}
}
getch();
}
************************************************************************
/*program to display following traingle
1
121
12321
1234321
123454321
*/
main()
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n");
for(j=5;j>i;j--)

19

{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(k=i-1;k>=1;k--)
{
printf("%d",k);
}
}
getch();
}
********************************************************************
/*program to disply following traingle
1
121
12321
1234321
1 23454321
1234321
12321
121
1
*/
main()
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n");
for(j=5;j>i;j--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(k=i-1;k>=1;k--)
{
printf("%d",k);
}
}
for(i=i-2;i>=1;i--)

20

{
printf("\n");
for(j=5;j>i;j--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(k=i-1;k>=1;k--)
{
printf("%d",k);
}
}
getch();
}
************************************************************************
/*program to diplay the stars as shown below
*
**
***
****
*****
*/
main()
{
int x,i,j;
clrscr();
printf("how many lines stars(*) should be printed?:");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
for(j=1;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}
************************************************************************

21

************************************************************************
/*program to display the series of numbers as given below
1
12
123
1234
4321
321
21
1

*/
main()
{
int i,j,x;
clrscr();
printf("\n enter value of x:\n");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
for(j=1;j<=i;j++)
printf("%2d",j);
printf("\n");
}
printf("\n");
for(i=x;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("%2d",j);
printf("\n");
}
getch();
}
************************************************************************
/*program to genarate the pyramid structure
0
101
21012
3210123
*/
main()
{
int k,i,j,x,p=34;
clrscr();
printf("\n enter a number\n");
scanf("%d",&x);
for(j=0;j<=x;j++)

22

{
gotoxy(p,j+1);
/*position curosr on screen (x,y cordinate)*/
for(i=0-j;i<=j;i++)
printf("%3d",abs(i));
p=p-3;
}
getch();
}
************************************************************************
/*program to check given num is octal or not*/
main()
{
int n,r,flag;
clrscr();
printf("\n enter a number:\n");
scanf("%d",&n);
flag=0;
while(n!=0)
{
r=n%10;
if(r>7)
{
flag=1;
break;
}
n=n/10;
}
if(flag==0)
printf("\n octal number");
else
printf("\n not an octal");
getch();
}
************************************************************************
/*program to check given number is prime or not using break-for*/
main()
{
int i,n,flag;
clrscr();
printf("\n enter number\n");
scanf("%d",&n);
flag=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)

23

{
flag=1;
break;
}
}
if(flag==0)
printf("\n prime number");
else
printf("\n not a prime number");
getch();
}
************************************************************************
/*program to disply prime numbers from 1 to n*/
main()
{
int i,j,n,flag;
clrscr();
printf("enter limit\n");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%4d",i);
}
getch();
}
************************************************************************
/*program to check the given number is ARM STRONG number or not*/
main()
{
int n,r,sum=0,fact,temp;
clrscr();
printf("enter a number\n");
scanf("%d",&n);

24

temp=n;
while(n>0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(sum==temp)
printf("\n ARM STRONG=%d",temp);
else
printf("\n not an arm strong number=%d",temp);
getch();
}
************************************************************************
/*program to find perpect squares from 1 to 500*/
#include<math.h>
main()
{
int i,count,x;
float c;
clrscr();
printf("\n\n perfect square from 1 to 500\n");
count=0;
for(i=1;i<=500;i++)
{
c=sqrt(i);
x=floor(c);/*for rounding up floor() is used*/
if(c==x)
{
printf("%5d",i);
count++;
}
}
printf("\n\n total perfect squares=%d\n",count);
getch();
}
************************************************************************
/*program to detect the largest number out of five numbers and display it*/
main()
{
int a,b,c,d,e,sum=0,i;
clrscr();
printf("\n enter five numbers:");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum=a+b+c+d+e;
for(i=sum;i<=sum;i--)

25

{
if(i==a || i==b || i==c || i==d || i==e)
{
printf("THE LARGEST NUM:%d",i);
exit();
}
}
}
************************************************************************
/*program to detect the smallest number out of five numbers and display it*/
main()
{
int a,b,c,d,e,sum=0,i;
clrscr();
printf("enter five numbers:\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum=a+b+c+d+e;
for(i=1;i<=sum;i++)
{
if(i==a || i==b || i==c || i==d || i==e)
{
printf("the smallest number:%d",i);
exit();
}
}
}
************************************************************************
/* write a program to print the five entered numbers in ascending order*/
main()
{
int a,b,c,d,e,sum=0,i;
clrscr();
printf("\n enter five numbers");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\n numbers in ascending order:\n");
sum=a+b+c+d+e;
for(i=1;i<=sum;i++)
{
if(i==a || i==b || i==c || i==d || i==e)
{
printf("%3d",i);
}
}
getch();
}

26

************************************************************************
/*write a program for digital clock*/
#include<dos.h>
#include<conio.h>
#include<stdio.h>
main()
{
int h,m,s;
clrscr();
for(h=1;h<=12;h++)
{
clrscr();
for(m=1;m<=59;m++)
{
clrscr();
for(s=1;s<=59;s++)
{
gotoxy(8,8);
printf("hh mm ss");
gotoxy(8,9);
printf("%d %d %d",h,m,s);
sleep(1);
}
}
}
getch();
}
/*write a program to evaluate the series given below
sum of series of x-x^3/3!+x^5/5!-x^7/7!+....x^n/n! */
#include<stdio.h>
#include<math.h>
main()
{
int n,i,x,c=3,f=1,l;
float sum;
clrscr();
printf("enter x & n:");
scanf("%d%d",&x,&n);
sum=x;
for(i=3;i<=n;i=i+2)
{
f=1;
if(c%2!=0)
{
for(l=1;l<=i;l++)
f=f*l;

27

sum=sum-pow(x,i)/f;
}
else
{
for(l=1;l<=i;l++)
f=f*l;
sum=sum+pow(x,i)/f;
}
c++;
}
printf("\n sum of series numbers:%f",sum);
getch();
}
************************************************************************
/* write a program to evaluate the series given below
x+x^2/2!+x^4/4!+x^6/6!+...x^n/n! */
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int f=1,l,i,x,y;
float sum;
clrscr();
printf("enter the value of x & y:\n");
scanf("%d%d",&x,&y);
sum=x;
for(i=2;i<=y;i=i+2)
{
f=1;
for(l=1;l<=i;l++)
f=f*l;
sum=sum+pow(x,i)/f;
}
printf("\n sum of series:%f",sum);
getch();
}
************************************************************************
/*program to convert decimal number to binary number*/
main()
{
int x,y=40;
clrscr();
printf("enter a number:\n");
scanf("%d",&x);
printf("\n binary number:");

28

while(x!=0)
{
gotoxy(y--,4);
printf("%d",x%2);
x=x/2;
}
getch();
}
************************************************************************
/*program to convert binary to equivalent decimal number*/
#include<conio.h>
#include<math.h>
#include<process.h>
main()
{
long int n;
int x,y=0,p=0;
clrscr();
printf("\n enter a binary number:");
scanf("%ld",&n);
while(n!=0)
{
x=n%10;
if(x>1 || x<0 )
{
printf("\n invalid digit");
exit(1);
}
y=y+x*pow(2,p);
n=n/10;
p++;
}
printf("\n equivalent decimal number is: %d",y);
getch();
}
************************************************************************

29

/*program to read and display array elements*/


#include<stdio.h>
#include<stdlib.h>
main()
{
int a[50];
int n,i;
clrscr();
printf("enter how many elements\n");
scanf("%d",&n);
if(n<1 || n>50)
{
printf("\ninvalid no of elements");
exit(0);
}
printf("\n enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n ur elements are");
for(i=0;i<n;i++)
{
printf("%2d",a[i]);
}
getch();
}
************************************************************************
/*program to find the sum of the elements in the array*/
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[50];
int n,i,sum=0;
clrscr();
printf("\n enter how many elements\n");
scanf("%d",&n);
if(n<1 || n>50)
{
printf("\n invalid choice");
exit(0);
}
printf("\n enter the elements");
for(i=0;i<n;i++)
{

30

scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i]; /*summing the array elements*/
}
printf("\n sum=%d",sum);
getch();
}
************************************************************************
/*program to find max and min of array elements*/
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[50];
int n,i,max,min;
clrscr();
printf("\n enter how many elements");
scanf("%d",&n);
if(n<1 || n>50)
{
printf("\n invalid no of elements");
exit(0);
}
printf("\n enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=min=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
else if(a[i]<min)
{
min=a[i];
}
}
printf("\n MAX=%d and MIN=%d",max,min);
getch();
}
************************************************************************

31

/*program to read and display m*n matrics*/


#include<stdio.h>
main()
{
int a[10][10];
int r,c,i,j;
clrscr();
printf("\n how many rows and columns");
scanf("%d%d",&r,&c);
printf("\n enter the matrix elements");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n matrix elements are");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%3d",a[i][j]);
}
}
getch();
}
************************************************************************
/*program to add two matrix*/
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[10][10],b[10][10],c[10][10];
int ar,ac,br,bc,i,j;
clrscr();
printf("\n how many rows and columns of matrix A");
scanf("%d%d",&ar,&ac);
printf("\n how many rows and columns of matrix B");
scanf("%d%d",&br,&bc);
if(ar!=br || ac!=bc)
{
printf("\n invalid Addition");

32

exit(0);
}
printf("\n enter the matrix elements of matrix A");
for(i=0;i<ar;i++)
{
for(j=0;j<ac;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the elements the elements of matrix B");
for(i=0;i<br;i++)
{
for(j=0;j<bc;j++)
{
scanf("%d",&b[i][j]);
}
}
/*adding*/
for(i=0;i<ar;i++) /*for(i=0;i<br;i++)*/
{
for(j=0;j<ac;j++) /*for(j=0;j<bc;j++*/
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n results Matrix Elements are ");
for(i=0;i<ar;i++)
{
printf("\n");
for(j=0;j<ac;j++)
{
printf("%3d",c[i][j]);
}
}
getch();
}
************************************************************************
/*program to perform multiplication of 2 matrixs*/
#include<stdio.h>
main()
{
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
printf("enter matrix A elements");

33

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("enter matrix B elements");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
printf("\nmatrix multiplication of A*B");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%3d",a[i][j]*b[i][j]);
}
getch();
}
************************************************************************
/*program to explain the working of three-dimensional arrayy*/
#include<stdio.h>
#include<conio.h>
main()
{
int array_3d[3][3][3];
int a,b,c;
clrscr();
/*printf("enter element\n"); */
for(a=0;a<3;a++)
for(b=0;b<3;b++)
for(c=0;c<3;c++)
array_3d[a][b][c]=a+b+c;
for(a=0;a<3;a++)
{
printf("\n");
for(b=0;b<3;b++)
{
for(c=0;c<3;c++)
printf("%3d",array_3d[a][b][c]);
printf("\n");
}

34

}
getch();
}
************************************************************************
/*program to display the number of days of given month of a year*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
int month[12]={1,3,5,7,8,10,12,4,6,9,11,2};
int i,mn;
clrscr();
printf("enter number of month");
scanf("%d",&mn);
for(i=0;i<=11;i++)
{
if(mn==month[i])
goto compare;
}
printf("\n invalid month");
exit(1);
compare:
if(i+1==12)
printf("month(%d) contains 28 days.",month[i]);
if(i+1<8)
printf("month(%d) contains 31 days.",month[i]);
if(i+1>7&&i+1!=12)
printf(" month(%d) contains 30 days",month[i]);
getch();
}
************************************************************************
/*program to arrange the numbers in increasing and decreasing order*/
main()
{
int i,j,sum=0,a[10];
clrscr();
printf("enter ten elements");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf(" \nnumber in ascending order:");
for(i=0;i<=sum;i++)
{

35

for(j=0;j<10;j++)
{
if(i==a[j])
printf("%3d",a[j]);
}
}
printf("\nnumbers in descending order:");
for(i=sum;i>=0;i--)
{
for(j=0;j<10;j++)
{
if(i==a[j])
printf("%3d",a[j]);
}
}
getch();
}
************************************************************************
/*the elements of the martrix of the order up to 3*3 and transopse its elements*/
#include<stdio.h>
#include<conio.h>
main()
{
int mat[3][3];
int i,j;
clrscr();
printf("enter elements of matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("transpose of the matrix \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",mat[j][i]);
}
printf("\n");
}
getch();
}
************************************************************************

36

/*program for linear search*/


#include<stdio.h>
main()
{
int a[]={1,3,5,10,13,20,26,31,36,40,43,57,58,60,66,73,78,80};
int num,i,flag;
clrscr();
printf("\n enter the number");
scanf("%d",&num);
flag=0;
for(i=0;i<18;i++)
{
if(num==a[i])
{
flag=1;
printf("\n found at a[%d]",i);
}
}
if(flag==0)
{
printf("\n not found");
}
getch();
}
************************************************************************
/*program to implement binary search*/
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[]={1,3,5,10,13,20,26,32,35,40,45,57,59,60,66,73,78,80};
int num,l=0,h=17,m;
clrscr();
printf("\n enter the num\n");
scanf("%d",&num);
while(1)
{
m=(l+h)/2;
if(num==a[m])
{
printf("\n found at a[%d]",m);
exit(0);
}
if(num>a[m])
{
l=m+1;

37

}
else
{
h=m-1;
}
if(l>h)
{
printf("\n ur num not found");
exit(0);
}
}
}
************************************************************************
/*program to read and display two strings*/
#include<stdio.h>
#include<string.h>
main()
{
char str1[30], str2[30];
clrscr();
printf("\n enter two strings");
scanf("%s%s",str1,str2);
printf("str1=%s \n str2=%s",str1,str2);
getch();
}
************************************************************************
/*program to print "WELCOME" by using different formats of initialization of string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[8]={'W','E','L','C','O','M','E','\0'};
char str2[8]="WELCOME";
char str3[]="WELCOME";
clrscr();
printf("\n str1=%s",str1);
printf("\n str2=%s",str2);
printf("\n str3=%s",str3);
getch();
}
************************************************************************

38

************************************************************************
/* program to read and display string using gets() & puts()*/
#include<stdio.h>
#include<string.h>
main()
{
char str[5];
clrscr();
printf("\n enter the string:\n");
gets(str);/* read string*/
printf("\nur string\n");
puts(str);/* display string*/
getch();
}
************************************************************************
/*program to read and display string using fgets() & fputs()*/
#include<stdio.h>
main()
{
char str[30];
int n;
n=sizeof(str);
clrscr();
printf("enter ur string\n");
fgets(str,n,stdin);/*prototype in <stdio.h>*/
fputs(str,stdout); /*prototype in <stdio.h>*/
getch();
}
************************************************************************
/* program to display the string 'SREELEKHA' using various format specification*/
main()
{
char sre[10]="SREELEKHA";
clrscr();
printf("%s\n",sre);
printf("%.5s\n",sre);
printf("%.8s\n",sre);
printf("%.10s\n",sre);
printf("%-10.4s\n",sre);
printf("%10.4s\n",sre);
printf("%8s\n",sre);
printf("%-8s\n",sre);
printf("%12s\n",sre);
printf("%-12s\n",sre);
getch();
}

39

************************************************************************
/*program to find length of string without using string function*/
#include<stdio.h>
main()
{
char str[30];
int i;
clrscr();
printf("enter the string");
scanf("%s",str);
for(i=0;str[i]!='\0';i++); /*semicolon required*/
printf("length of str=%d",i);
getch();
}
************************************************************************
/*program to find out of length of string using strlen()*/
#include<string.h>
main()
{
char str[30]="srelekha";
int l=strlen(str); /* strlen() prototype in <string.h>*/
clrscr();
printf("\n length of string=%d",l);
getch();
}
************************************************************************
/*copying two strings*/
#include<stdio.h>
main()
{
char str1[30],str2[30];
int i;
clrscr();
printf("enter a string\n");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("\n str1=%s\t str2=%s",str1,str2);
getch();
}
************************************************************************

40

************************************************************************
/* copying string using strcpy()*/
#include<string.h>
main()
{
char str1[30],str2[30];
clrscr();
printf("\n enter a string1\n");
gets(str1);
strcpy(str2,str1);
printf("\n str1=%s \t str2=%s",str1,str2);
getch();
}
************************************************************************
/*comparision of strings*/
#include<stdio.h>
#include<stdlib.h>
main()
{
char str1[30],str2[30];
int i;
clrscr();
printf("enter string1\n");
scanf("%s",str1);
printf("enter string2\n");
scanf("%s",str2);
for(i=0;str1[i]!='\0' || str2[i]!='\0';i++)
{
if( (str1[i]-str2[i]>0))
{
printf("\n %s greater than %s ",str1,str2);
exit(0);
}
else if(( str1[i]-str2[i]<0))
{
printf("\n %s greater than the %s",str2,str1);
exit(0);
}
}
printf("\n both are equal");
}
************************************************************************

41

************************************************************************
/*comparison of strings using strcmp()*/
#include<string.h>
main()
{
char str1[30],str2[30];
int n;
clrscr();
printf("\n enter string1\n");
scanf("%s",str1);
printf("\n enter string2\n");
scanf("%s",str2);
n=strcmp(str1,str2);
if(n==0)
printf("\n %s and %s are equal",str1,str2);
else if(n>0)
printf("\n %s greater than %s",str1,str2);
else
printf("\n %s greater than %s",str2,str1);
getch();
}
************************************************************************
/* concatenation of two strings*/
#include<stdio.h>
main()
{
char str1[50],str2[50],str3[101];
int i,j;
clrscr();
printf("enter the string1\n");
scanf("%s",str1);
printf("enter the string2\n");
scanf("%s",str2);
/*copying str1---->str3*/
for(i=0;str1[i]!='\0';i++)
{
str3[i]=str1[i];
}
/* appending str2----->str3*/
for(j=0;str2[j]!='\0';j++,i++)
{
str3[i]=str2[j];
}
str3[i]='\0';
printf("\n str1=%s & str2=%s",str1,str2);

42

printf("\n result string=%s",str3);


getch();
}
************************************************************************
/*concatenation using strcat()*/
#include<string.h>
main()
{
char str1[50]="sreenu";
char str2[50]="siva";
char str3[101];
/*copying str1--->str3*/
strcpy(str3,str1);
/*appending str2--->str3*/
strcat(str3,str2);
clrscr();
printf("\n %s \n %s",str1,str2);
printf("\n %s",str3);
getch();
}
************************************************************************
/*reversing of a string*/
#include<stdio.h>
main()
{
char str[30];
char temp;
int i,j,l;
clrscr();
printf("\n enter a string");
scanf("%s",str);
for(l=0;str[l]!='\0';l++);
for(i=0,j=l-1;i<l/2;i++,j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
printf("\n reverse of string=%s",str);
getch();
}
************************************************************************

43

************************************************************************
/* reversing of a string using strrev() */
#include<string.h>
main()
{
char str[30]="sreenu";
clrscr();
printf("\n%s",strrev(str));
getch();
}
************************************************************************
/* searching string using linear search*/
#include<stdio.h>
#include<stdlib.h>
main()
{
char str[5][20]={"cpp","unix","linux","visix","java"};
char temp[20];
int i,flag;
clrscr();
printf("\n enter the string u want to search");
scanf("%s",temp);
flag=0;
for(i=0;i<5;i++)
{
if(strcmp(temp,str[i]==0))
{
flag=1;
printf("\n string found and string: %s",temp);
break;
}
}
if(flag==0)
{
printf("\n string not found");
}
getch();
}
************************************************************************

44

************************************************************************
/*sorting of a string*/
#include<stdio.h>
main()
{
char str[5][30];
char temp[30];
int i,j;
clrscr();
printf("\n enter the strings");
for(i=0;i<5;i++)
{
scanf("%s",str[i]);
}
/*bubble sorting*/
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(strcmp(str[j],str[j+1]>0))
{
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}
}
printf("\n after sorting");
for(i=0;i<5;i++)
{
printf("\n%s",str[i]);
}
getch();
}
************************************************************************
/*program to understand functioncalls*/
void atk();
void nlr();
void hyd();
main()
{
clrscr();
printf("\n i am in home");
atk();
nlr();

45

hyd();
getch();
}
void atk()
{
printf("\n i am in atk");
nlr();
printf("\n i am come back atk");
}
void nlr()
{
printf("\n i am in nlr");
hyd();
printf("\n i am come back nlr");
}
void hyd()
{
printf("\n i am in hyd");
}
************************************************************************
/*function with no arguments and no return value*/
#include<stdio.h>
main()
{
clrscr();
printf("\n start");
f1();
printf("\n one");
f2();
printf("\n two");
f3();
printf("\n three");
printf("\n end");
getch();
}
f1()
{
printf("\n in function one");
}
f2()
{
printf("\n in function two");
}
f3()
{
printf("\n in function three");
}

46

************************************************************************
/* function with arguments and no return values*/
main()
{
int a=10,b=20;
clrscr();
display(a,b); /* here a,b are called "parameters"*/
getch();
}
display(int x,int y) /*x,y are called arguments*/
{
printf("\n %d %d",x,y);
}
************************************************************************
/*function with arguments and return value*/
main()
{
int a=10,b=20,c;
c=sum(a,b);
clrscr();
printf("\n sum=%d",c);
getch();
}
sum(int x, int y)
{
int res;
res=x+y;
return res;
}
************************************************************************
*program to understand function default return type*/
main()
{
int a=10,b=15;
float avg;
avg=aveg(a,b);
clrscr();
printf("\n average value =%f",avg);
getch();
}
aveg(int x, int y)
{
float res;
res=(x+y)/2.0;
return res;
}

47

/* in c language function default returns integer type of data*/


/*To return other type of data then its prototype must be declared*/
/*prototype declared with in function or global section*/
/*for better progaming prototype declared in global section*/
/*if the function is declared globally then it can be called from any function*/
************************************************************************
/*program to under stand function prototype with in function*/
/* correction of before average example*/
void sreenu();
float aveg(int , int);
main()
{
int a=10,b=15;
float avg;
float aveg( int , int); /*function prototpe in main()*/
avg=aveg(a,b);
clrscr();
printf("\naverage value =%f",avg);
getch();
}
float aveg(int x, int y)
{
float res;
res=(x+y)/2.0;
return res;
************************************************************************
/*program to understand function prototype declared in gloabal section*/
float aveg(int, int);/*function prototype */
main()
{
int a=10,b=15;
float avg;
avg=aveg(a,b);
clrscr();
printf("\n average value=%f",avg);
getch();
}
float aveg(int x ,int y)
{
float res;
res=(x+y)/2.0;
return res;
}
************************************************************************

48

/*program to undrestand return type void*/


main()
{
int a=10;
float b=20.5;
char ch='a';
void display(int , float , char);/* void: it specifies no type */
clrscr();
/* void is an empty data type*/
display(a,b,ch);
getch();
}
void display(int x, float y,char z)
{
printf("\n %d %f %c",x,y,z);
}
************************************************************************
#include<math.h>
main()
{
int i =1,n,f1=0 ,f2=2,sum=0;
printf("enter num");
scanf("%d",&n);
while(i<=n)
{
printf("%d",f1);
sum=f1+f2;
f1=f2;
f2=sum;
i++;
}
}
************************************************************************
PROGRAMS ON STRUCTURES
#include<stdio.h>
struct book
{
int id;
char title[10];
float cost;
char author[15];
char pub[15];
};
main()
{
struct book b1;

49

clrscr();
printf("enter book details\n");
printf("\n enter book id\n");
scanf("%d",&b1.id);
printf("\n enter book title\n");
scanf("%s",b1.title);
printf("enter book cost\n");
scanf("%f",&b1.cost);
printf("enter book author\n");
scanf("%s",b1.author);
printf("enter book publisher\n");
scanf("%s",b1.pub);
printf(" book details are\n");
printf("\n book id\n");
printf("%d",b1.id);
printf("\n book title\n");
printf("%s",b1.title);
printf(" \nbook cost\n");
printf("%f",b1.cost);
printf("\n book author\n");
printf("%s",b1.author);
printf(" \nbook publisher\n");
printf("%s",b1.pub);
getch();
}
************************************************************************
#include<stdio.h>
struct book
{
int id;
char title[10];
float cost;
char author[15];
char pub[15];
};
main()
{
int n;
n=sizeof(struct book);
clrscr();
printf("structre size:%d",n);
}
************************************************************************

50

************************************************************************
include<stdio.h>
struct emp
{
int id;
char name[15];
int sal;
char dept[15];
};
main()
{
struct emp e[2];
int i;
clrscr();
for(i=0;i<2;i++)
{
printf("enter emp details\n");
printf("enter emp id\n");
scanf("%d",&e[i].id);
printf("enter emp name\n");
scanf("%s",e[i].name);
printf("enter emp sal\n");
scanf("%d",&e[i].sal);
printf("enter emp dept\n");
scanf("%s",e[i].dept);
}
printf("\nempid\tempname\tempsal\tempdept\n");
for(i=0;i<2;i++)
{
printf("\n%d\t%s\t%d\t%s",e[i].id,e[i].name,e[i].sal,e[i].dept);
}
}
************************************************************************
PROGRAMES ON POINTERS
main()
{
int i=3;
int *p;
p=&i;
clrscr();
printf("\naddress of i=%u",&i);
printf("\naddress of i=%u",p);
printf("\naddress of p=%u",&p);
printf("\nvalue of i=%d",i);
printf("\nvalue of i=%d",*p);
printf("\nvalue of i=%d",*(&i));

51

printf("\n value of p=%u",p);


/*getch();*/
}
************************************************************************
main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
clrscr();
printf("\n address of i=%u",&i);
printf("\n address of i=%u",j);
printf("\n address of i=%u",*k);
printf("\n address of j=%u",&j);
printf("\n address of j=%u",k);
printf("\n address of k=%u",&k);
printf("\n value of i=%d",i);
printf("\n value of i=%d",*(&i));
printf("\n value of i=%d",*j);
printf("\n value of i=%d",**k);
printf("\n value of j=%u",j);
printf("\n value of k=%u",k);
getch();
}
************************************************************************
main()
{
int a[]={1,2,3,4};
int *p;
int i;
p=&a[0];/*p=a*/
clrscr();
for(i=0;i<4;i++)
{
printf("\n adress of a[%d]=%u",i,&a[i]);
printf(" value of a[%d]=%d",i,*p);
p++;
}
getch();
}
************************************************************************
main()
{
int a[]={1,2,3,4};

52

int i;
clrscr();
for(i=0;i<4;i++)
{
printf("\nvalue of a[%d]=%d",i,*(a+i));
}
getch();
}
************************************************************************
main()
{
int a[3][2]={1,2,3,4,5,6};
int i,j;
clrscr();
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\n value of a[%d][%d]=%d",i,j,*(*(a+i)+j));
}
}
getch();
}
************************************************************************
main()
{
char name[]="sreenu";
char *p;
p=name;
clrscr();
while(*p!='\0')
{
printf("%c",*p);
p++;
}
getch();
}
main()
{
char name[]="sreenu";
char *p;
p=name;
clrscr();
while(*p!='\0')

53

{
printf("%c",*p);
p++;
}
getch();
}
************************************************************************
PROGRAMES ON FILES
#include<stdio.h>
main()
{
FILE *fp;
char ch;
fp=fopen("e:\\sreenu\\file1","w");
ch=getchar();
while(ch!=EOF)
{
fputs( ch, fp);
ch=getchar();
}
fclose(fp);
printf("\n sucess fully created");
printf("\n file contents are\n");
fp=fopen("e:\\sreenu\\file1","r");
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}
************************************************************************
#include<stdio.h>
main()
{
int sno;
char sna[20],ch='y';
FILE * fp;
fp=fopen("student","w");
while(ch=='y')
{
printf("enter sno\n");
scanf("%d",&sno);
printf("enter sna\n");
scanf("%s",&sna);

54

fprintf(fp,"sno=%d\n",sno);
fprintf(fp,"sna=%s\n",sna);
printf("do u want 2 continue(y/n)\n");
ch=getch();
}
fclose(fp);
}
************************************************************************
#include<stdio.h>
main()
int n,i;
FILE *fp, *fp1;
fp=fopen("even","w");
fp1=fopen("odd","w");
printf("how many numbers u want\n");
scanf("%d",&n);
i=1;
while<i<=n)
{
if(i%2==0)
{
fprintf(fp,"even number =%d\n",i);
}
else
{
fprintf(fp1,odd number=%d\n",i);
}
i++;
}
fclose(fp);
fclose(fp1);
}
************************************************************************
#include<stdio.h>
main()
{
int stno,total;
char stna[20],ch='y';
FILE *fp;
fp=fopen("student","w");
while(ch=='y')
{
printf("enter stno\n");
scanf("%d",&stno);
printf("enter stna\n");
scanf("%s",&stna);

55

printf("enter total\n");
scanf("%d",&total);
fprintf(fp,"stno=%d\n",stno);
fprintf(fp,"stna=%s\n",stna);
fprintf(fp,"total=%d\n",total);
printf("do you want 2 contiue(y\n");
ch=getch();
}
fclose(fp);
}
************************************************************************

ALL THE BEST.

56

You might also like