C File
C File
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int r1,r2,i,s,m;
clrscr ();
printf ("\n Enter the starting range:");
scanf ("%d",&r1);
printf ("\n Enter the ending range:");
scanf ("%d",&r2);
while (r1 <= r2)
{
s = 0;
m = r1;
while ( m>= 1 )
{
i = m%10;
s = s+( i * i * i );
m = m/10;
}
if (s == r1)
printf ("\n The number is:\t %d",s);
r1 = r1+1;
}
getch ();
}
output
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int avg,sum = 0;
int i;
int marks [15];
for (i=0;i<=14;i++)
{
printf ("Enter marks = \n");
scanf ("%d",&marks [i] );
}
for (i=0;i<=14;i++)
avg = sum/15;
printf ("\n average marks=%d",avg);
getch ();
}
Output
Enter the marks 20
Enter the marks 50
Enter the marks 60
Enter the marks 56
Enter the marks 44
Enter the marks 36
Enter the marks 64
Enter the marks 50
Enter the marks 70
Enter the marks 80
Enter the marks 82
Enter the marks 90
Enter the marks 46
Enter the marks 54
Enter the marks 13
Average marks = 54
/* To print the total of marks of five students using arrays */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int n [5],i;
int m [5],j,sum = 0;
for (i=0;i<=4;i++)
{
printf ("Enter marks for student number( %d ) = \n",i+1);
for (j=0;j<=4;j++)
{
printf ("Enter marks for subject(%d)",j+1);
scanf ("%d",m[j]);
sum = sum+m[j];
}
for (i=0;i<=4;i++)
{
for (j=0;j<=4;j++)
{
printf (“ sum of marks %d”, sum);
}
}
}
getch ();
}
Output
Enter the marks for 1 student 20 45 56 67 78
Enter the marks for 2 student 50 70 77 55 34
Enter the marks for 1 student 60 35 56 78 65
Enter the marks for 1 student 56 77 66 33 44
Enter the marks for 1 student 44 90 96 76 67
#include<stdio.h>
#include<conio.h>
2
void main()
{
clrscr ();
struct book
{
char name [20];
int price;
int pages;
};
struct book b [6];
int i;
for(i=0;i<=5;i++)
{
scanf ("%s%d%d",&b[i].name,&b[i].price,&b[i].pages);
}
printf ("%s%d%d",b[2].name,b[2].price,b[2].pages);
getch ();
}
Output
BOOK NAME PRICE PAGES
Bible 178.50 400
Classic stories 222.00 120
Let us C 118.00 70
PCS 190.00 348
Maths 80.80 234
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a,b,c,d,e,f,g;
printf ("Enter marks for 6 subjects = \n");
scanf ("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
g = (a+b+c+d+e+f)/6;
if (g>=90)
{
printf("You are a intelligent student");
}
else if (g>=70&&g<90)
{
printf("You are a good student");
}
else
{
printf("You are a poor student");
}
getch();
}
Output
Enter the marks for 6 subjects = 56 67 76 89 90 68
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int i;
printf ("\t \t Enter any number = ");
scanf ("%d",&i);
(i<=1000)?printf("\t\tgood"):printf("\t\tbad");
getch ();
}
Output
Good
/*To count the no. of digits of a number*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a,b=0,c;
printf ("Enter any number=\n");
scanf ("%d",&a);
c = a;
while(c>0)
{
c = c/10;
b = b+1;
}
printf ("the no. of digits in given number is = %d",b);
getch();
}
Output
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a [25],i,j,m,t;
printf ("Enter 25 numbers :- \n");
for(i=0;i<=24;i++)
{
printf ("%d:\t",i+1);
scanf ("%d", & a[i]);
}
for(i=0;i<=24;i++)
{
for(j=0;j<24-i;j++)
{
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf ("\n\n\n\t\t sorted numbers are : \n");
for (i=0;i<=24;i++)
printf ("%d\n",a[i]);
getch();
}
Output
Enter numbers Sorted
34 6
45 7
67 34
6 34
7 45
533 67
4234 223
34 533
223 4234
/* Program for factorial of a number*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a,facto;
printf ("Enter any number=\n");
scanf ("%d",&a);
facto = 1;
while (a>=1)
{
facto =facto*a;
printf ("%d*",a);
a=a-1;
}
printf ("\nthe factorial is:%d",facto);
getch ();
}
Output
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a=0,b=1,c=0,k=0;
printf ("enter limit for the series to be generated = ");
scanf ("%d",&k);
printf ("\n%d",a);
printf ("\n%d",b);
c = a+b;
while (c<=k)
{
printf("\n%d",c);
a = b;
b = c;
c = a + b;
}
getch ();
}
Output
1
2
3
5
8
13
21
34
/* Program of fibonnaci series using recursssion */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int old = 0,current = 1;
void fibo(int,int);
printf ("%d\t%d\t",old,current);
fibo (old,current);
printf ("\n\n\n\n\nPress any key to exit....");
getch ();
}
void fibo (int old,int current)
{
static int terms=2;
int rac;
if (terms<20)
{
rac = old+current;
printf ("%d\t",rac);
terms = terms+1;
fibo(current,rac);
}
else
return;
}
Output
1
2
3
5
8
13
21
34
press any key to exit
/* Program using structures */
#include<stdio.h>
#include<conio.h>
struct student
{
int age =15 ;
};
void main()
{
clrscr ();
struct student stud;
void show (struct student stud);
stud.age = 12;
show(stud);
getch ();
}
output
The age is 12
/* Program to join two strings without using inbuilt functions */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
char str1[10], str2[10], str3[10];
int i,j,k;
printf ("Enter string 1 = ");
gets (str1);
printf(“Enter string 2 = “);
gets (str2);
for (i=0;i<strlen(str1);i++)
{
str3[i] = str1[i];
}
for (i=0,j=strlen (str3);i<strlen(str2);i++)
{
str3[j] = str2[i];
j++;
}
str3[j]=’\0’;
puts (str3);
getch ();
}
Output
Major singh
/* Greatest of three numbers using conditional operator*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a,b,c;
printf ("Enter three numbers a,b,c\n");
scanf ("%d%d%d",&a,&b,&c);
(a>b)? (a>c)?printf("a is greatest"):printf("c is greatest"): (b>c)?printf("b is
greatest"):printf("c is greatest");
getch ();
}
Output
c is greatest
/* Subtraction of 2nd from 1st number */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a,b,c;
printf ("Enter 1st number = ");
scanf ("%d",&a);
printf ("Enter 2nd number = ");
scanf ("%d",&b);
if(a>b)
{
c=a - b;
printf(“%d”,c);
}
else
printf(“1st number should be greater than 2nd”);
getch ();
}
output
3
/* program to print the pattern of sgiven v shape */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int i=1,x=71,b=0,j,val,k;
clrscr ();
while (i<=7)
{ j = 65;
val = x;
while (j<=val)
{
printf ("%c ",j);
j++;
}
if (i==1)
val--;
k = 1;
while (k<=b)
{
printf (" ");
k++;
}
b = 2*i-1;
while (val>=65)
{
printf ("%c ",val);
val--;
}
printf ("\n");
x--;
i++;
}
printf ("\n\n\n\n\nPress any key to halt.....");
getch ();
}
/* To find greatest of 10 numbers using arrays */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a[10],i,j,t;
printf ("Enter 10 numbers:\n");
for(i=0;i<=9;i++)
{
scanf("%d",&a[i]);
for(i=0;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
printf ("\n The greatest number is:%d",a[9]);
getch ();
}
Output
Enter numbers
89
55
45
65
78
55
32
13
14
22
the greatest number is 89
/* To print a pattern of * */
#include<stdio.h>
#include<conio.h>
main()
{
clrscr ();
int i,j,n,k;
printf ("How many lines\n");
scanf ("%d",&n);
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
printf ("*");
printf ("\n");
}
getch ();
}
Output
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
float a,b,c,d,e,f,g;
printf ("Enter marks for five subjects=\n");
scanf ("%f%f%f%f%f",&a,&b,&c,&d,&e);
f = a + b + c + d + e;
g = f * 0.2;
printf ("total = %f \n percentage = %f", f , g);
getch ();
}
Output
Total = 413
Percentage = 82.6
/* To calculate the power of a number raised to other*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a,b,c = 1;
printf ("Enter two numbers a&b=\n");
scanf ("%d%d",&a,&b);
c = a;
while (b>1)
{
c=c*a;
b=b-1;
}
printf ("The result of a^b is=%d",c);
getch();
}
Output
2
5
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr ();
int a,b,c,d,i;
printf ("\nenter the first number\t");
scanf ("%d",&a);
printf ("\nenter the second number\t");
scanf ("%d",&b);
while (a<=b)
{
for(i=a+1;i<=b;i++)
{
c=(a*a)+(i*i);
d=sqrt(c);
if(c==d*d)
printf(" a=%d b=%d c=%d",a,i,d) ;
}
printf("\n");
a=a++;
}
getch();
}
output
a= 3 b= 4 d=5
/*To reverse the digits of any number*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int b=0,n,a;
printf("Enter any no.=\n");
scanf("%ld",&n);
while(n>0)
{
a=n%10;
n=n/10;
b=b+1;
printf("%ld",a);
}
getch ();
}
output
4321
/* Sorting of number using arrays */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a[25],i,j,t;
printf ("\nEnter 25 numbers:\n");
for(i=0;i<=24;i++)
scanf ("%d",&a[i]);
for(i=0;i<=23;i++)
{
for(j=i+1;j<=24;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf ("\n\n\n\t\t sorted numbers are:\n");
for(i=0;i<=24;i++)
printf ("%d\n",a[i]);
getch();
}
/* To calculate the sum of 1st and last digit of a number*/
#include<stdio.h>
#include<conio.h>
void main()
{
long int s,n,b,sum;
printf ("enter the number:");
scanf ("%ld",&n);
b = n%10;
while (n>=1)
{ s=n%10;
n=n/10;
}
sum = s + b;
printf ("%ld",sum);
getch ();
}
output
enter the number
1223
sum 4
/*To calculate the sum of digits*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
long int a,b,c = 0,l;
printf ("Enter any number=\n");
scanf ("%ld",&a);
l = 0;
while (a>0)
{
b = a%10;
a = a/10;
l = l+b;
}
printf ("%ld",l);
getch ();
}
output
10
/* To calculate the table of given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr ();
int a,b = 1,c;
printf ("Enter any number of which you want a table=\n");
scanf ("%d",&a);
while (b<=10)
{
c = a*b;
b = b+1;
printf ("%d\n",c);
}
getch ();
}
output
2
4
6
8
10
12
14
16
18
20
/* Program of building tower using recursion */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
void transfer(int,char,char,char);
int n;
printf("\nEnter the no. of disks:");
scanf("%d",&n);
transfer(n,'S','D','T');
getch();
}
void transfer (int x,char to,char from,char temp)
{
if(x>0)
{
transfer (x-1,to,temp,from);
printf ("\nMove %d from %c to %c",x-1,to,from);
transfer (x-1,temp,from,to);
}
return ;
}
output
Move 1 from S to D
Move 2 from S to T
Move 1 from D to T
Move 3 from S to D
Move 1 from T to S
Move 2 from T to D
Move 1 from S to D
/* To change the case of character without using inbuilt functions */
#include <conio.h>
#include <stdio.h>
void main()
{
char word[20];
int i;
printf(“enter the string”)
gets (word);
for (i=0; i<strlen(word);i++)
{
if (word[i] > 65 && word[i] <90)
{
word[i] = word[i] + 32;
}
else
{
word[i] = word[i] – 32;
}
}
puts(word);
getch();
}
Output
MAJOR
/* To reverse the complete string */
#include <conio.h>
#include <stdio.h>
void main()
{
char word[20];
int a;
printf(“enter the string”);
gets(word);
for(a=strlen(word);a>0;a--);
{
printf(“%c”,word[a]);
}
getch( );
}
output
rojam
/* To sort the Names */
#include <stdioh>
#include<conio.h>
void main()
{
char name[5][10];
printf(“enter the names”);
int a,ptr,j;
char temp;
for(a=-0;a<5;a++)
{
for (j=0;j<10;j++)
{
ptr=strcmp(names[a][0],&names[a+1][0]);
if (ptr>0)
{
temp=names[Ia[0];
names[a][0];
names[a][0]=names[a+1][0];
names[a+1][0]=temp;
}
}
}
for(a=0;a<5;a++)
{
for(j=0;j<10;j++)
{
printf(”%c”,names[a][j]);
}
}
getch();
}
output
enter the names sorted names
major manu
manu major
varun shangly
sunila sunila
shangly varun
/* Subtraction of 2nd no. from 1st number */
#include <conio.h>
#include <stdio.h>
void main()
{
int a,b,c;
printf(“enter the 1st no.”);
scanf(%d”,&a);
printf(“enter the second no.”);
scanf(“%d”,&b);
if (a>b)
{
c=a-b;
printf(“%d”,c);
else
{
printf(“first number should be greater then second”);
}
getch();
}
output
3
/* Multiplication of 2 demission array */
#include <conio.h>
#include <stdio.h>
void main()
{
int a[5][5], b[5][5], c[5][5];
int x,y,z;
printf(“enter element for a”);
for(x=0;a<5;a++)
{
for(y=0;b<5:b++)
{
scanf(“%d”&a[x][y]);
}
}
printf(“enter elements for b”);
for(x=0;a<5;a++)
{
for(y=0;y<5;y++)
{
scanf(“%d”,&b[x][y]);
}
}
for(x=0;a<5;a++)
{
for(y=0;y<5;y++)
{
for (z=0,c[x][y]=0;k<5;k++)
{
c[x][y]=c[x][y]+a[x][z]+b[z][x];
}
}
}
for(x=0;a<5;a++)
{
for(y=0;y<5;y++)
{
printf(“%d”,c[x][y]);
}
}
getch();
}
/*To sum until the sum is less than 10*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int a,b,c=0,l;
printf("Enter any number=\n");
scanf("%ld",&a);
r:
l=0;
while (a>0)
{
b=a%10;
a=a/10;
l=l+b;
}
c=l;
if((c>0) && (c<9))
printf("%ld",c);
else
{
a=c;
goto r;
}
getch();
}
output
6
/* Sum of 2 dimension array*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][2],b[2][2],c[2][2],i,j;
printf("Enter the values of A:\n");
for (i=0;i<=1;i++)
{
for (j=0;j<=1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the values of B:\n");
for (i=0;i<=1;i++)
{
for (j=0;j<=1;j++)
scanf("%d",&b[i][j]);
}
printf("The sum is=\n");
for (i=0;i<=1;i++)
{
for (j=0;j<=1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\n",c[i][j]);
}
}
getch();
}
output
enter value for a: 1 2
3 4
sum is 2 4
6 8
/* Area of the Rectangle */
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
printf(“enter length, breadth of rectangle”);
scanf(“%f %f”,&a,&b);
c=a*b;
printf(“area =%f”,c);
getch();
}
output
area = 8
/* Convert Fahrenheit temperature to degree */
#include<stdio.h>
#include<conio.h>
void main()
{
float c,f;
clrscr()
printf(“enter temperature in Fahrenheit”);
scanf(“%f”,&f);
c=(5*(f-32)/9);
printf(“c=%f”,c);
getch();
}
output
43.5 degree
/* Calculate The Simple Interest */
#include <stdio.h>
#include <conio.h>
void main()
{
float si, p,r;
int t;
clrscr();
printf (“enter principal, rate, time”);
scanf (“%f %f %d”,&p, &r, &t);
si = (p*r*t)/100;
printf (“si =%f”,si);
getch ( );
}
output
SI = 4050
/* Swaping Without Using 3rd Variable */
#include <stdio.h>
#include <conio.h>
void main()
{
int a=10, b=20;
clrscr();
a = a+b;
b = a-b;
a = a-b;
printf (“%d %d”, a, b);
getch ( );
}
output
10
20
20
10
/* Area & Perimeter Of Rectangle, Area & Circumference Of Circle */
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
float l,b,r,p,ar,c,ac;
printf (“enter length, breadth, radius”);
scanf (“%f%f%f”,&l,&b,&r);
ar = l*b;
p = 2*(l+b);
ac = (22*r*r)/7;
c = (2*22*r)/7;
printf (“ar=%f,p=%f,sc=%f,c=%f”,ar,p,ac,c);
getch ( );
}
output
ar = 6
p = 10
sc = 50.2
c = 25.1
/* Calculate Gross Salary*/
#include<stdio.h>
#include<conio.h>
void main()
{
int bs, hra, da, gs;
clrscr();
printf(“enter basic salary”);
scanf(“%d”,&ba);
if (bs<1500)
{
hra=bs*10/100;
da=bs*90/100;
}
else
{
hra=500;
da=98/100*bs;
}
gs = bs+hra+da;
printf (“gross salary=%d,gs);
getch ( );
}
output
#include<stdio.h>
#include<conio.h>
void main()
{
int m1, m2, m3,per;
clrscr();
printf (“enter marks”);
scanf (“%d %d %d”, &m1, &m2, &m3);
per = (m1+m2+m3)/3;
if (per>=60)
printf (“first division”);
if (per>=50)&&(per<60)
printf (“second division”);
if (per>=40)&&(per<50)
printf (“third division”);
if (per<40)
printf(“fail”);
getch ( );
}
output
enter marks = 23 45 56
third division
/* Profit Or Loss*/
#include <stdio.h>
#include<conio.h>
void main()
{
int sp, p, l, cp;:
printf (“enter the values of sp & cp”);
scanf (“%d %d”&sp, &cp);
p = sp-cp;
l = cp-sp;
if (sp>cp);
printf(“profit is %d”,p);
{
if (cp>sp)
printf(“loss is %d”,l);
}
getch ( )
}
output
profit is 23000
/* Even Or Odd*/
#include<stdio.h>
#include <conio.h>
void main()
{
int a,r;
clrscr( );
printf (“enter a number”);
scanf( “%d”, &a);
if (a%2==0)
printf (“the number is even”);
else
printf (“ the number is odd”);
getch ( );
}
output
#include <stdio.h>
#include <conio.h>
void main(()
{
int y;
clrscr ( );
printf (“enter the value of the year”);
scanf (“%d”, &y);
if (y%4==0)
printf (“it is a leap year”);
else
printf (“it is not a leap year”);
getch ( );
}
output
#include<stdio.h>
#include<conio.h>
void main()
{
int p,n,count=1;
float r,si;
clrscr();
while(count<=3)
{
printf (“\n enter values of p,n&r”);
scanf (“%d %d %f”, &p,&n, &r);
si = p*n*r/100;
printf (“simple intrest= rs %f”, si);
}
count = count+1;
getch ( );
}
output
#include<stdio.h>
#include<conio.h>
void main(0
{
int num,i;
clrscr();
printf(“enter number”);
scanf(“%d”, &num);
i=2;
while(i<num-1)
{
if (num%i==0)
{
printf(“not a prime”);
break;
}
i++;
}
if (i==num)
printf (“prime number”);
getch ( );
}
output
enter number 12
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
for (a=1:a<=3;a++)
{
for(b=1;b<=3;b++)
{
for (c=1;c<=3;c++)
{
if (a==b|b==c|c==a)
continue;
else
{
printf (“%d %d %d”,I,j,k);
printf (“\n”);
}
}
}
}
getch ( );
}
output
123
132
213
231
321
312
/*Print The Screen With Smiling Face */
#include <stdio,h>
#include <conio.h>
void main(0
{
int j,k,l=1;
clrscr ( );
for (j=0;j<=80;j++)
{
for(k=0; k<=25;k++)
{
printf(“%c”,n);
}
}
getch();
}
output
* *
* *
* *
* *
* *
* *
* * *
/* Print First “N” Integers*/
#include<stdio.h>
#include <conio.h>
void main()
{
int j, n;
clrscr ( );
printf (“enter number”);
scanf (“%d”, &n);
for (j=1;j<=n;j+=)
{
printf(“\n%d”,j);
}
getch ( );
}
output
enter number = 10
1
2
3
4
5
6
7
8
9
10
/* Sum Of First “N” Integers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int I,s=0,n;
clrscr ( );
printf (“enter the number”);
scanf (“%d”, &n);
for (I=1;I<=n;I++)
{
s=s+I;
}
printf (“\n\n sum %d”, s);
getch ( );
}
output
enter number = 10
1
2
3
4
5
6
7
8
9
10
sum = 55
/*Sum Of “N” Even Integers*/
#include<stdio.h>
#include<conio.h>
void main();
{
int I,n,sum=0;
clrscr ( );
prinf (“enter a no”);
scanf (“%d”, &n);
for (I=0;I<=n;I+2)
{
sum = sum+I;
printf(“\n the sum is %d”, sum);
}
getch ( );
}
output
enter number = 10
10
12
14
16
18
20
22
24
26
28
sum = 190
/* Sum Of “N” Odd Number*/
#include<stdio.h>
#include<conio.h>
void main();
{
int I, n, sum=0;
clrscr ( );
prinf (“enter a no”);
scanf (“%d”, &n);
for (I=1;I<=n;I+2)
{
sum = sum+I;
printf (“\n the sum is %d”, sum);
}
getch ( );
}
output
enter number = 10
11
13
15
17
19
21
23
25
27
29
sum is 200
/* Solve The Series For “N” Terms I.E. 1*1+2*2+3*3+….N*N */
#include<stdio.h>
#include<conio.h>
void main()
{
int I, j, n, sum=0;
clrscr ( );
prinf (“enter a no”);
scanf (“%d”, &n);
for (I=1;I<=n;I++)
{
j=I*I;
sum=sum+j;
}
printf (“\n the sum is %d”, sum);
getch ( );
}
output
enter number = 5
the sum is 55
/* Function To Swap Two Number By Call By Value */
#include <stdio.h>
#include<conio.h>
void main()
{
int a=10, b=20);
clrscr ( );
swapv (a, b);
printf (“\n %d %d”, a, b);
getch ( );
}
output
10
20
20
10
/*Function To Swap Two Numbers By Call By Reference*/
#include<stdio.h>
#include<conio.h>
void main ( )
{
int a=10,b=20;
clrscr ( );
swapr(&a, &b);
printf (“\n a=%d b=%d”,a,b);
getch ( );
}
output
10
20
20
10
/* Recursive Function To Calculate The Factorial Of A Number */
#include <stdio.h>
#include <conio.h>
void main ( )
{
int a,fact;
clrscr ( );
printf (“enter a number “);
scanf (“%d”, &a);
fact = rec(a);
printf (”factorial %d”, fact);
getch ( );
}
int rec(int x)
{
int f:
if (x==1)
return (1)
else
f=x*rec (x-1);
return (f);
}
output
enter a number 5
factorial 120
/* Macro To Test Whether A Chracter Entered Is Small Case Letter Or
Not*/
#include<stdio.h>
#include<conio.h>
#define SMALLCASE (y) (y>=97 && y<=122)
void main()
{
int SMALLCASE (int ch);
char ch;
clrscr ( );
printf (“enter any letter”);
scanf (“%c”, &ch);
if (SMALLCASE (ch) );
{
printf (“small case”);
else
printf (“not small case”);
}
getch ( );
}
output
#include<stdio.h>
#include<conio.h>
#define UPPERCASE (y) (y>=65 && y<=90)
void main ( )
{
char ch;
clrscr ( );
printf (“enter any letter”);
scanf (“%c”, &ch);
if (UPPERCASE(ch) );
{
printf (“upper case”);
else
printf (“not upper case”);
}
getch ( );
}
output
It is upper case
/*Macro To Check Whether A Character Entered Is An Alphabet Or
Not*/
#include<stdio.h>
#include<conio.h>
#define ISALPHABET (y) ((y>=97 && y<=122 || y>=65 && y<=90))
void main ( )
{
char ch;
clrscr ( );
printf (“enter any letter”);
scanf (“%c”, &ch);
{
if (ISALPHABET (ch) );
printf (“an alphabet”);
else
printf (“not an alphabet”);
}
getch ( );
}
output
It is an alphabet
/* Macro To Obtain Greater Of Two*/
#include <stdio.h>
#include<conio.h>
#define ISBIGGER(a, b) (a>b)
void main ( )
{
int x, y;
clrscr ( );
printf (“enter two numbers”);
scanf (“%d %d” &x, &y);
if (ISBIGGER(x, y))
{
printf (“\n first no is greater”);
else
printf (“\n second no is greater”);
}
getch ( );
}
output
second no is greater
/* Write Macro Definition With Argument For Calculation Of Area &
Perimeter Of A Triangle, Square & A Circle */
#include<stdio.h>
#include<conio.h>
#define carea (r) (3.14*r*r)
#define cperi )(r) (2*3.14*r)
#define sarea (x) (x*x)
#define speri (x) (4*x)
#define tarea (x,y,z) (0.5*x*y)
#define yperi (x,y,z) (x+y+z)
void main ( )
{
float r,s,b,a,h,car,tar,sar,cpr,tpr,spr;
clrscr ( );
printf_(“\n for a circle…”);
printf (“\n \n enter radius”);
scanf (“%f”, &r);
car = carea (r);
cpr = cperi (r);
printf (“area = %f”, car);
printf (“\n perimeter =%f”, cpr);
#include <stdio.h>
#include <conio.h>
void main ( )
{
int ar [10], a=0;
clrscr ( );
printf (“enter elements\n”);
for (a=0;a<10;a++)
{
scanf(“%d”, &ar[a]);
}
for (a=0;a<10;a++)
{
if (ar[a]>0)
printf(“\n number is positive”,ar[a]);
else
printf(“\n number is negative”, ar[a]);
}
for (a=0;a<10;a++)
{
if (ar [a]%2==0)
printf (“\n no. is even, ar[a]);
else
printf (“\n no. is odd”, ar[a]);
}
getch ( );
}
output
enter element 10
It is even number
It is positive number
/* To Find Transpose Of A Matrix*/
#include <stdio.h>
#include <conio.H>
void main ( )
{
int arr[3][3];
int I, j;
clrscr ( );
printf (“enter elements”);
for (a=0;a<3;a++)
{
for (j=0;j<3;j++)
{
scanf(“%d”, &arr[a][j]);
}
}
for (a=0;a<3;a++)
{
for (j=0;j<3;j++)
{
printf(“transpose=”, arr[j][a]);
}
}
getch ( );
}
output
enter value for a: 1 2
4 4
transpose is 1 4
2 4
/* Queue Program*/
/*based on first in first out method*/
#include<stdio.h>
#include<alloc.h>
main ( )
{
int n, choice;
char ans=’y’;
switch (choice)
{
case 1:
printf(“\n enter value for queue :”);
scanf(%d”, &n);
push (p, n);
display (p);
break;
case 2:
pop(p);
break;
default:
printf(“\n invalid choice”);
}
printf(“\n do you want to continue (y/n”);
fflush(stdin);
ans = getchar ( );
}
#include<stdio.h>
#include <conio.h>
#include <alloc.h>
numbers are 12 13 14 15
/*Program To Insert In The Middle Of Link List*/
#include<stdio.h>
#include <conio.h>
#include <alloc.h>
numbers are 12 13 14 15 16
/* Inserting Node In The End Of Link List*/
#include<stdio.h>
#include <conio.h>
#include <alloc.h>
numbers are 12 13 14 15 16
/* Deleting Node From The Beginning Of Link List*/
#include<stdio.h>
#include <conio.h>
#include <alloc.h>
list *q;
main( )
{
if (q==NULL)
{
printf(“\n link list doesn’t exist”);
return;
}
if (q->link==NULL)
{
free(q->link);
printf (“\n the deleted value is %d”, q->data);
printf (\n list is empty now”);
p=NULL;
}
else
{
p=q-> link;
printf (“deleted node is%d”, q-> data);
free (q);
}
getch ( );
}
output
numbers are 13 14 15 16
/* Deleting Node From The Middle*/
#include<stdio.h>
#include <conio.h>
#include <alloc.h>
list *p,*q,*temp;
main( )
{
int value, loc;
printf(“enter the value to delete:”);
scanf(“%d”, &loc);
if (p==NULL)
{
printf(“\n link list doesn’t exist”);
}
else
temp=p;
if (p->data==value)
{
printf(“\n deleted value is %d”, p->data);
free(p);
p=NULL;
}
else
{
q=q->link;
while (q!=NULL)
{
if (q -> data==value)
{
temp->link=q-> link;
printf(“\n the deleted value is %d“, q->data);
free(q);
break;
}
else
{
q=q-> link;
temp=temp-> link;
}
}
}
getch( );
}
output
numbers are 12 13 15 16
/* Program To Delete The Last Node Of The Link List*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
numbers are 12 13 14 15
/* Linear Search*/
/* searches each element in the array and displays the location of that item*/
#include <stdio.h>
#include < conio.h>
main( )
{
int arr[20]
int n,I,item, loc,f=0;
char ans =’y’;
clrscr();
while (ans ==’y’ || ans ==’y’)
{
printf (“\n enter size of array);
scanf(“%d”, &n);
if (n>20)
{
printf(“size of array is greater than declare”);
break;
}
printf(“\n enter values in the array:\n”);
for (I=0; I<n;I++)
{
printf(“\t enter %d value”, I+1);
scanf(“%d”, arr[I]);
}
printf(“\n the elements values are \n”);
for(I=0;I<n;I++)
{
printf(“\t %d”, arr[I]);
}
printf(“\n enter value to search”);
scanf(“%d” ,&item);
loc =1;
f=0;
for(I=0; I<n;I++)
{
if (arr[I]== item)
{
printf(“ element to be located %d”, arr[I]);
printf(“\n location :%d”, loc);
f =1;
break;
}
loc loc+1;
}
(f==0)
printf(“element %d does not exist”, item);
printf(“\n want to continue (y/n): “);
fflush (stdin);
ans = getchar();
}
getch ( );
}
output
element to be located : 4
location found : 3
want to execute again (y/n) : y
element to be located : 4
element does not exist
want to execute again (y/n) : n
/* To Create A Stack*/
/* based on last in first out method*/
#include<stdio.h>
#include<alloc.h>
main ( )
{
int n, choice;
char ans=’y’;
switch (choice)
{
case 1:
printf(“\n enter value for queue :”);
scanf(%d”, &n);
push (p, n);
display (p);
break;
case 2:
pop(p);
display (p);
break;
default:
printf(“\n invalid choice”);
}
printf(“\n do you want to continue (y/n”);
fflush(stdin);
ans = getchar ( );
}
getch ( );
#include<stdio.h>
#include<alloc.h>
main( )
{
int a[15], beg, end, loc, n, I, ctr=1;
char ans = ‘y’;
int quick (int a[], int beg, int end);
clrscr();
while (ans ==’y’ || ans==’y’)
{
printf(“\n enter number of elements to be sorted”);
scanf(“%d”, &n);
ctr=1;
printf(“\n enter elements into array\n”);
for(I=0;I<n;I++)
{
printf(“enter %d number”, I+1);
scanf(“%d”, &a[I]);
}
printf(“\n the elements are \n”);
for (I=0;I<n;I++)
{
printf(“/t %d”, a[I]);
}
if (n>1)
{
s=(stack*)malloc (sizeof (stack));
s-> lower =0;
s-> upper =n-1;
s->ptr=NULL;
}
while(s!=NULL)
{
beg=s->lower;
end=s-> upper;
if (s-> ptr==NULL)
{
free(s);
s=NULL;
}
else
{
q=s;
s=s->ptr;
free(q);
}
loc=quick(a,beg,end);
if (beg<loc-1)
{
q=(stack*)malloc(sizeof(stack));
q->lower=beg;
q-> upper =loc-1;
q-> ptr =s;
s=q;
}
printf(“\n the %d list of sorted elements is: \n”, ctr);
for (I=0;I<n;I++)
{
printf(“\t %d”, a[I]);
}
ctr=ctr+1;
}
printf(“\n want to execute again”);
fflush(stdin);
ans = getchar();
}
getch();
}
output
enter number of elements to be sorted : 5
elements are : 6 5 4 3 2
the 1 list of sorted elements is :
2 5 4 3 6
the 2 list of sorted elements is :
2 5 4 3 6
the 3 list of sorted elements is :
2 3 4 5 6
the 4 list of sorted elements is :
2 3 4 5 6
the 5 list of sorted elements is :
2 3 4 5 6
/* Binary Search Tree*/
#include<stdio.h>
#include<alloc.h>
tree *p;
main()
{
int ch, n, choice;
char ans =’y’;
clrscr();
while (ans==’y’) || ans==’y’)
{
printf(“\n select the operation”);
printf(“\n 1 for add a node”);
printf(“\n 2 for delete a node”);
printf(“\n 3 for display”);
printf(“enter the choice”);
scanf(“%d”, &ch);
switch(ch)
{
case 1:
printf(“enter value for node”);
scanf(“%d”, &n);
addbst (p,n);
printf(“\n the values un preorder are:\n”);
preorder(p);
break;
case 2:
printf(“enter value for node”);
scanf(“%d”, &n);
delbst (p,n);
if (flag==0)
{
printf(“\n the values in preorder are:\n”);
preorder(P):
}
else
printf(“\t all nodes are deleted”);
break;
case 3:
printf(“1 – preorder”);
printf(“2 – inorder”);
printf(“3 – postorder”);
printf(“enter choice:”);
scanf(“%d”, &choice);
switch (choice)
{
case1:
printf(“value in preorder are\n”);
preorder(p);
break;
case 2:
printf(“value in inorder are\n”);
inorder(p);
break;
case 3:
printf(“value in postorder are\n”);
postorder(p);
break;
default:
printf(“invalid choice”);
}
break;
default:
printf(“\n invalid choice”);
printf(“\n \t select a valid choice”);
}
printf(“want to continue again”);
fflush(stdin);
ans = getchar();
}
getch();
}
#include<stdio.h>
#include<string.h>
main()
{
char text[80], text 1[80], text2[80];
int 1=0, j=0, pos= -1, f=0;
int 11, 12;
char ans=’y’;
clrscr();
Output
Length : 5
Enter string to replace : IM
Enter string to inserted: EE
main()
{
char str1[80], str2[80], ans =’y’;
int I,j,m, pos, f;
clrscr();
while (ans ==’y’ || ans ==’y’)
{
printf(“\n \n enter the main string”);
fflush(stdin);
gets(str1);
prntf (“enter the string to deleted”);
fflush (stdin)
gets*str2);
m = strlen (str2);
pos = 0;
f = 0;
j = 0;
for (I=0; str1[I];I++)
{
if(j>=m)
break;
if (sre1[I]= str2[j])
{
if (j==0)
pos=I;
j++;
f= 0;
}
else
{
j=0;
f=1;
}
}
if (pos!= -1 && f==0)
{
for (I= pos; I<=(Pos+m);I++)
{
for (j=pos; str1[j]!= ‘\0’;j++)
str1[j]= sre1[j+1];
str[j]=’\0’;
}
printf(“the final string in it %s”, str1);
}
else
{
printf(“ the string to ne deleted not exist”);
}
printf(:want to execute again”);
fflush(stin);
ans= getche();
}
getch ( );
}
output
#include<stdio.h>
#include<conio.h>
main()
{
int I, ctr;
char str[80];
char ans=’y’;
output
major
the length is 5
want to execute again : n