Vous êtes sur la page 1sur 93

/* To find armstrong numbers between a given range*/

#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

enter the starting range: 1


enter the ending range : 153

the number 153 is Armstrong


/* To print the average marks of 15 students using array */

#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++)

sum=sum + marks [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

Sum of marks for 1 student = 266


Sum of marks for 2 student = 286
Sum of marks for 3 student = 294
Sum of marks for 4 student = 276
Sum of marks for 5 student = 373
/* To print the price, name, pages of different books using structure */

#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

Bible 178.50 400


Classic stories 222.00 120
Let us C 118.00 70
PCS 190.00 348
Maths 80.80 234
/* To calculate the average and result of six subjects by “if, else” condition*/

#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

YOU are a good student


/* Program to find greater of numbers using conditional operator*/

#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

Enter any number = 4000

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

Enter any number = 1234

The no. of digits in given number is 4


/* To print sorted integer array */

#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

Enter any number 5

The factorial is 120


/*Program for fibonacci series*/

#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

Enter the limit for series to be generated = 50

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

Enter the limit for series to be generated = 50

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 ();
}

void show (struct student stud)


{
printf ("The age is=%d",stud.age);
}

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

Enter string 1 = major

Enter string 2 = singh

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

Enter three numbers a b c


12 13 23

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

Enter 1st number = 13

Enter 2nd number = 10

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

How many lines = 10


*
**
***
****
*****
******
*******
********
*********
**********
/*Calculation of percentage of 5 subjects*/

#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

Enter marks for five subjects =


85
75
76
89
88

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

Enter two numbers a & b

2
5

The result of a^b is 32


/* To prove pythagoruos theorum*/

#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

enter the first number 3

enter the second number 4

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

enter any number = 1234

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

enter any number = 1234

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

enter the number if which you want a table = 2

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

Enter the number of disks = 3

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

Enter the string major

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

enter the string major

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

Enter 1st number = 13

Enter 2nd number = 10

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

enter any number = 12345

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

enter value fro b: 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

enter length, breadth of rectangle


2 4

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

enter temperature in Fahrenheit


110

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

enter principal rate time


30000 4.5 3

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

enter length, breadth and radius


2 3 4

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

enter basic salary


15000

gross salary = 30200


/* STUDENT’S DIVISION*/

#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

enter values of sp & cp


35000 12000

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

enter the number = 8

the number is even


/*Leap Year Or Nit*/

#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

enter the value of year 1986

it is not a leap year


/* Calculation Of Simple Intrest For Three Using While Loop*/

#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

enter value for p n r 2000 3.5 2


enter value for p n r 3000 3.5 5
enter value for p n r 5000 5.5 6

simple interest = 140 525 1650


/* Whether Number Is Prime Or Not*/

#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

not a prime number


/* Print Combination Of 1, 2, 3 Without Repition*/

#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>

int swapv(int, int);

void main()
{
int a=10, b=20);
clrscr ( );
swapv (a, b);
printf (“\n %d %d”, a, b);
getch ( );
}

int swapv (int x, int y)


{
int t;
t=x;
x=y;
y=t;
printf (“%d %d”, x, y);
}

output

10
20

20
10
/*Function To Swap Two Numbers By Call By Reference*/

#include<stdio.h>
#include<conio.h>

int swapr(int, int);

void main ( )
{
int a=10,b=20;
clrscr ( );
swapr(&a, &b);
printf (“\n a=%d b=%d”,a,b);
getch ( );
}

int swapr (int *x, int *y)


{
int t;
t = *x;
*y = t;
}

output

10
20

20
10
/* Recursive Function To Calculate The Factorial Of A Number */

#include <stdio.h>
#include <conio.h>

int rec (int);

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

It is not small case


/*Macro To Test Whether A Character Entered Is Upper Case Letter
Or Not*/

#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

enter two number 23 45

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);

printf (“\n \n for a square…..”);


printf (“\n \n enter sode “);
scanf (“%f”, &s);
sar = sarea(s);
spr = speri (s);
printf (“\n area =%f”, sar);
printf (“\n perimeter =%f”, spr);

printf (“\n \n for a triangle..”);


printf (“\n enter base, altitude, height”);
scanf (“%f %f %f “, &b, &a, &h);
tar = tarea (b, a, h);
tpr = tperi (b, a, h);
printf (“\n area=%f”, tar);
printf (“\n perimeter= %f”, tpr);
getch ( );
}
/* To Find If The Number Entered Is Positive Or Negative , Even Or
Odd*/

#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>

typedef struct node


{
int value;
struct node *ptr;
}list;

list *p, *temp;

main ( )
{
int n, choice;
char ans=’y’;

void push (list *q, int num);


void pop (list *q);
void display (list *q);

while (ans==’y’ || ans==’y’)


{
printf(“\n select an operation to perform”);
printf(“\n 1- for push operation”);
printf(“\n 2- for pop operation”);
printf(“\n enter the choice”);
scanf(“%d”, &choice);

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 ( );
}

void push(list *q, int num)


{
if(q== NULL)
{
p=(list*)malloc(sizeof (list));
p - > value = num;
q - > ptr = NULL;
}
else
{
while (Q - > ptr != NULL)
{
q = q - > ptr;
q - > ptr = (list *)malloc(sizeof (list));
q - > ptr - > value = num;
q - > ptr - > = NULL;
}
}

void pop(list *q)


if (p-> ptr ==NULL)
{
printf(“queue is emplty”);
}
else
{
temp = p -> ptr:
free(p);
p= temp;
display(p);
}

void display(list *q)


{
printf(“the values after the operation are”);
while (q!= NULL)
{
printf(“\t %d”, q - > value);
q = q -> ptr;
}
}
/* Program To Add In Beginning Of The Linear Link List*/

#include<stdio.h>
#include <conio.h>
#include <alloc.h>

typedef struct node


{
int data;
struct node *link;
}list;

list *p, *temp;


main( )
{
int num;
clrscr();
printf(“enter the number”);
scanf(“%d”, &num);
if (p==NULL)
{
p=(list*)malloc(sizeof(list));
p->data=num;
p->link=NULL;
printf(“the entered number is %d”, p);
}
else
{
q=p;
temp=(list*)malloc(sizeof (list));
temp->data=num;
temp-> link=q;
p=temp;
printf(“the entered number is %d”, p);
}
getch ( );
}
output

entered numbers are 12 13 14 15 16


deleted node is 16

numbers are 12 13 14 15
/*Program To Insert In The Middle Of Link List*/

#include<stdio.h>
#include <conio.h>
#include <alloc.h>

typedef struct node


{
int data;
struct node *link;
}list;

list *p, *q, *temp;


main( )
{
int num,loc;
clrscr();
printf(“enter the number”);
scanf(“%d %d”, &num, &loc);
if (p==NULL)
{ printf(“error doesn’t exist any list”);
break;
}
else
{ q=p;
while (q!= NULL)
{ if (q->data==loc)
{ temp=(list*)malloc(sizeof (list));
temp->data=num;
temp-> link=q->link;
q-> link =temp;
printf(“added number is %d”, q-> link );
}
else
q=q->link;}
}
getch( );
}
Output

entered numbers are 12 13 15 16


enter position and node to insert 15 14

numbers are 12 13 14 15 16
/* Inserting Node In The End Of Link List*/

#include<stdio.h>
#include <conio.h>
#include <alloc.h>

typedef struct node


{
int data;
struct node *link;
}list;

list *p, *q;


main( )
{
int num;
clrscr();
printf(“enter the number”);
scanf(“%d”, &num);
if (p==NULL)
{
printf(“error doesen’t exist any list”);
break;
}
else
q=p;
{
while (q->link!= NULL)
{
q=q->link;
q->link->data=num;
q-> link -> link=NULL;
printf(“entered number is %d”, q-> link);
}
}
gech();
}
output

entered numbers are 12 13 14 15


inserted node is 16

numbers are 12 13 14 15 16
/* Deleting Node From The Beginning Of Link List*/

#include<stdio.h>
#include <conio.h>
#include <alloc.h>

typedef struct node


{
int data;
struct node *link;
}list;

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

entered numbers are 12 13 14 15 16


deleted node is 12

numbers are 13 14 15 16
/* Deleting Node From The Middle*/

#include<stdio.h>
#include <conio.h>
#include <alloc.h>

typedef struct node


{
int data;
struct node *link;
}list;

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

entered numbers are 12 13 14 15 16


enter the location of node to delete 14
deleted node is 14

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>

typedef struct node


{
int dat;
struct node *link;
}list;

list *q, *p;


main( )
{
if (q==NULL)
{
printf(“\n link list doesn’t exist “);
break;
}
if (q-> link==NULL)
{
free(q);
p=NULL;
printf(“\n list is empty”);
}
else
{
while (q-> link -> link != NULL)
{
q=q-> link;
free(q-> link -> link);
q-> link= NULL;
}
}
getch( );
}
output

entered numbers are 12 13 14 15 16


deleted node is 16

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

entered size of array : 5


enter values in array :
enter 1 value : 6
enter 2 value : 5
enter 3 value : 4
enter 4 value : 3
enter 5 value : 2
elements are : 6 5 4 3 2
enter value to search : 4

element to be located : 4
location found : 3
want to execute again (y/n) : y

entered size of array : 5


enter values in array :
enter 1 value : 16
enter 2 value : 15
enter 3 value : 14
enter 4 value : 13
enter 5 value : 12
elements are : 16 15 14 13 12
enter value to search : 4

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>

typedef struct node


{
int value;
struct node *ptr;
}list;

list *p, *q;

main ( )
{
int n, choice;
char ans=’y’;

void push (list *q, int num);


void pop (list *q);
void display (list *q);
p= NULL;
while (ans==’y’ || ans==’y’)
{
printf(“\n select an operation to perform”);
printf(“\n 1- for push operation”);
printf(“\n 2- for pop operation”);
printf(“\n enter the choice”);
scanf(“%d”, &choice);

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 ( );

void push(list *q, int num)


{
if(q== NULL)
{
p=(list*)malloc(sizeof (list));
p - > value = num;
q - > ptr = NULL;
}
else
{
while (q - > ptr != NULL)
{
q = q - > ptr;
q - > ptr = (list *)malloc(sizeof (list));
q - > ptr - > value = num;
q - > ptr - > = NULL;
}
}

void pop(list *q)


{
if (p==NULL)
{
printf(“\n the stack is empty”);
}
else
{
q=p;
while (q-> ptr-> ptr!==NULL)
{
q=q->ptr;
}
}
q -> ptr= NULL:
free(q->ptr->ptr);
}
void display(list *q)
{
printf(“the values after the operation are”);
while (q!= NULL)
{
printf(“\t %d”, q - > value);
q = q -> ptr;
}
}
/* Quick Sort*/

#include<stdio.h>
#include<alloc.h>

type struct node


{
int lower;
int upper;
struct node *ptr;
}stack;

stack *s, *q;

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();
}

int quick(int a[], int beg, int end)


{
int left=beg, right= end, loc= beg, temp;
while ((a[loc]<=a[right]) && (loc != right))
right--;
if (loc==right)
return(loc);
if (a[loc]>a[right])
{
temp = a[loc];
a[loc]= a[right];
a[right] = temp;
loc = right;
}
while ((a[left]<= a[loc] ) && (loc != left))
left++;
if (left==loc)
return (loc);
if (a[loc]<a[left])
{
temp =a [loc];
a[loc]= a[left];
a[left] = temp;
loc= left;
}
getch( );
}

output
enter number of elements to be sorted : 5

enter elements into array : 6 5 4 3 2

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>

typedef stryuct node


{
int value;
struct node *left;
struct node *right;
}tree;

tree *p;

main()
{
int ch, n, choice;
char ans =’y’;

void addbst (tree *root, int num);


void delbst (tree *ptr, int num);
void casea (tree *root, tree *loc, tree , *par);
void caseb (tree *root, tree *loc, tree , *par);
void preorder(tree *root);
void inorder (tree *root);
void postorder ( tree *root);

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();
}

void addbst (tree *root, int num)


{
terr *temp = root;
if (root ==NULL)
{
root =(tree*) malloc (sizeof (tree));
root -> value = num;
root -> left = NULL;
root -> right = NULL;
p = root;
}
else
{
while (tenp != NULL)
{
if (num == temp -> value)
{
printf(“\n error message”);
return;
}
else
{
if (num< temp-> value)
{
if (temp -> left ==NULL)
{
temp -> left = (tree*)malloc (sizeof (tree));
temp -> left -> value= num;
temp-> left-> left= NULL;
temp -> left -> right= NULL;
break;
}
else
temp = temp-> left;
}
else
{
if (temp -> right ==NULL)
}
temp ->right= (tree*)malloc(sizeof(tree));
temp -> right-> value= num;
temp -> right -> right = NULL;
temp-> right -> left = NULL;
break;
}
else
temp= temp -> right;
}
}
}
}
getch();
}

void delbst(tree *ptr, int num)


{
tree *par, *loc, *suc, *parsuc, *save, *root;
loc = NULL;
root = ptr;
if (root ==NULL)
{
printf(“\n tree Is empty”);
return;
}
if (root -> value ==num)
{
par = NULL;
loc = root;
printf(“\n deleted node is %d”, root -. Value);
}
else
{
if (num < root -> value)
{
ptr = root -> left;
save = root;
}
else
{
ptr = root -> Right;
save = root;
}
while (ptr!= NULL)
{
if (num ==ptr -> value)
{
loc= ptr;
par= save;
break;
}
if (num< ptr -> value)
{
save = ptr;
ptr = ptr -> left;
}
else
{
save = ptr;
ptr = ptr -. Right;
}
}
}
if (loc == NULL)
{
printf(“ node does not exist in the tree”);
return;
}
if ((loc -. Left != NULL) && (loc -> right != NULL))
{
caseb(root, loc, par);
else
{
casea(root, loc, par);
}
}

void casea(tree *root, tree *loc, tree *par)


{tree *chaild = NULL;
if (loc -> left ==NULL && loc -> roght ==NULL)
{
child= NULL;
}
else
{
if (loc-> left != NULL)
{
child = loc -> Left;
}
else
{
child= loc-> right;
}
}
if (par != NULL)
{
if (loc == par -> left)
{
par -> left = child;
}
else
{
par -> right = child;
}
}
else
{
root = child;
p= root;
}
return;
}

void caseb( tree*root, tree *loc, tree *par)


{
int inf;
tree *suc, *parsuc, *ptr, *save;
ptr = loc -> right;
while (ptr -> left != NULL)
{
save = ptr;
ptr = ptr -> left;
}
suc = ptr;
parsuc = save;
inf = suc -> value;
casea (root, suc, parsuc);
loc-> value = inf;
}

void preorder (tree * root)


{
if (root !=NULL)
{
printf(“\t %d”, root -> value);
preorder (root -> left);
preorder (root -> right);
}
else
return;
}

void inorder(tree *root)


{
if (root!= NULL)
{
inorder (root -> left);
printf(“\t %d”, root -> value);
inorder (root -> right);
}
else
return;
}

void postorder (tree * root)


{
postorder (root -> left);
postorder(root -> right);
printf(“\t %d”, root -> value);
}
else
return;
}
/* Replacing A Complete String*/

#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();

while (ans ==’y’ || ans ==’y’)


{
printf(“\n enter the string”);
fflush(stdin);
scanf(“%[^\n ]s”, text);
printf(“\n \t length = %d \n”, strlen(text));
printf(“enter string to be replaced”);
fflush(stdin);
scanf(“%[^\n]s”, text1);
11= strlen (text1);
printf(“enter string to be insert”);
fflush(stdin);
scanf(“%[^\n]s”, text2);
12= strlen (text2);
f=1;
pos=-1;
j=0;
for(I=0; text[I]!= ‘\0’; I++)
{
if (j>=11)
break;
if (text [I] == text1 [j])
{
if(j==0)
pos =I;
j++;
f=0;
}
else
{
j=0;
f=1;
}
}
if (pos!= -1 && f==0)
{
printf(“the position of ‘%s’ in ‘%s’ is %d, text1, text, pos+1);
}
else
{
printf(“string ’%s’ does not exist”, text1);
}
if(11==12)
{
j=0;
for(I=pos; I<(pos+12);I++)
{
text[I]= text2[j];
j++;
}
printf(“\n the new string is \n \t %s”, text);
}
else
{
if (11>12)
{
for (I=pos; I<(pos+(11-12)); I++)
for (j=pos; text[j]!= ‘\0’;j++)
text[j]= text [j+1];
text[j]=’\0’;
j=o;
for(I=pos; I<(pos; I<(pos+12); I++)
{
text[I]= text2[j];
j++;
}
printf(“\n the new string is: \n \t %s”, text);
}
else
{
for(I=pos; I< (pos+(12-11));I++)
{
for (j= strlen (text); j>0;j--)
{
if (j==pos);
break;
text[j+1] = text[j];
}
}
text[strlen (text)+2]=’\0’;
j=0;
for (I=pos; I<(pos+12); I++)
{
text[I] = text2[j];
j++;
}
text[I]=’ ’;
printf(“\n length = %d”, strlen (text));
printf(“\n the new string is : \n \t %s”, text);
}
}
printf(“\n want to execute again”);
fflush(stdin);
ans = getchar();
}
getch();
}

Output

Enter string : SIMRAT

Length : 5
Enter string to replace : IM
Enter string to inserted: EE

The new string is : SEERAT

Want to execute again: n


/* Deleting A Word*/
#include <stdio.h>

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

enter the string :


hello my name is major singh

enter string to be deleted : singh


deleted string is singh

new string is : hello my name is major

want to execute again : n


/* Count the string*/

#include<stdio.h>
#include<conio.h>

main()
{
int I, ctr;
char str[80];
char ans=’y’;

while (ans ==’y’ || ans ==’y’)


{
printf(“\n enter the string”);
fflush(stdin);
scanf(“%[^\n]s”,str);
ctr =0;
for (I=0; str[I]!=’\0’;I++)
{
ctr++;
}
printf(“the length of %s %d”, str, ctr);
printf(“\n want to execute again”);
fflush(stdin);
ans =getchar();
}
getch();
}

output

enter the string :

major

the length is 5
want to execute again : n

Vous aimerez peut-être aussi