500 C Programs For BOOK Print
500 C Programs For BOOK Print
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
//clrscr();
printf("WELCOME TO C PROGRAMS");
printf("\n THIS IS MY FIRST C PROGRAM WITH DEVC++ EDITOR");
printf("\n THIS C PROGRAM WRITTEN BY B.RAVINDER GOUD");
getch();
return 0;
}
/* OUTPUT:
WELCOME TO C PROGRAMS
THIS IS MY FIRST C PROGRAM WITH DEVC++ EDITOR
THIS C PROGRAM WRITTEN BY B.RAVINDER GOUD
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a=1234;
char c='A';
char str[]="RAVINDER B";
float f=1324.234513;
// clrscr();
printf("\n%c",c);
printf("\n%10c",c);
printf("\n%-10c",c);
1
printf("\n%s",str);
printf("\n%20s",str);
printf("\n%-20s",str);
printf("\n%20.4s",str); // 20.4s prints after 20 spaces with 4 characters
printf("\n%f",f);
printf("\n%20f",f);
printf("\n%-20f",f);
printf("\n%20.2f",f); // 20.2 means in a width is 20
// only 2 digits after decimal will be printed
getch();
return 0;
}
/* OUTPUT:
1234
1234
1234
A
A
A
RAVINDER B
RAVINDER B
RAVINDER B
RAVI
1324.234497
1324.234497
1324.234497
1324.23
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1234;
float f=12345.676747;
char c='A';
char str[]="C IS A PROGRAMMING LANGUAGE";
2
// clrscr();
getch();
return 0;
}
/* OUTPUT:
*/
/* PROGRAM ON SCANF() FUNCTION */
#include<stdio.h>
#include<conio.h>
//input formats
int main()
3
{
int i;
float f;
char str[20];
clrscr();
printf("Enter 4 digit number:");
fflush(stdin);
scanf("%4f",&f); // reads only 4 digits
printf("\n given no=%f",f);
getch();
return 0;
}
/* OUTPUT:
4
Enter tel no:9490230057
Telephone no=9490230057
#include<stdio.h>
#include<conio.h>
int begin()
{
printf("Hello....Welcome to C");
getch();
return 0;
}
/* OUTPUT:
Hello....Welcome to C
*/
int main()
{
clrscr();
printf("\n MINUMUM VALUE OF CHAR=%d",CHAR_MIN); //or SCAHR_MIN
printf("\n MAXIMUM VALUE OF CHAR=%d",CHAR_MAX);
printf("\n NUMBER OF BITS IN A BYTE=%d",CHAR_BIT);
printf("\n MAXIMUM VALUE OF SIGNED CHAR=%d",UCHAR_MAX);
5
printf("\n\n MINIMUM VALUE OF SHORT INTEGER=%d",SHRT_MIN);
printf("\n MAXIMUM VALUE OF SHORT INTEGER=%d",SHRT_MAX);
printf("\n MAXIMUM VALUE OF UNSIGNED SHORT INTEGER=
%u",USHRT_MAX);
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
//clrscr();
6
printf("\n C IS A PROCEDURE ORIENTED PROGRAMMING LANGUAGE");
printf("\n \v C EXTENSION IS .C"); // \v PRINTS VERTICAL TAB
printf("\n C WAS WRITTEN BY \"DENNIS RITCHIE\" "); // \" PRINTS
DOUBLE QUOTES
printf("\n C WAS DEVELOPED IN YEAR \'1972\' "); // \' PRINTS SINGLE
QUOTES
printf("\n \\ DENOTES A BACKSLASH CHARACTER "); // \\ PRINTS
BACKSLASH CHARACTER
getch();
return 0;
}
/* OUTPUT:
ESCAPE CHARACTERS EXAMPLE
-------------------------
C IS A PROGRAMMING LANGUAGE
? C EXTENSION IS .C
C DEVELOPED BY "DENNIS RITCHIE"
C DEVELOPED IN '1972'
\ IS A BACKSLASH CHARACTER
*/
/* PROGRAM TO PRINT ADDITION,MULTIPLICATION,DIVISION,
SUBTRACTION,REMAINDER OF GIVEN 2 NUMBERS */
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
int sum,mul,sub,div,rem;
clrscr();
printf(" Enter any 2 numbers \n");
scanf("%d%d",&x,&y);
sum=x+y;
mul=x*y;
sub=x-y;
div=x/y;
rem=x%y;
7
printf("\n Remainder of %d and %d is %d",x,y,rem);
getch();
return 0;
}
/* OUTPUT:
Enter any 2 numbers
5
2
Addition of 5 and 2 is 7
Multiplication of 5 and 2 is 10
Subtraction of 5 and 2 is 3
Division of 5 and 2 is 2
Remainder of 5 and 2 is 1
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
clrscr();
printf(" Enter any 2 numbers \n");
scanf("%d%d",&x,&y);
getch();
return 0;
}
/* OUTPUT:
Enter any 2 numbers
5
8
2
Addition of 5 and 2 is 7
Multiplication of 5 and 2 is 10
Subtraction of 5 and 2 is 3
Division of 5 and 2 is 2
Remainder of 5 and 2 is 1
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,x;
float result;
clrscr();
printf("\n Result=%f",result);
getch();
return 0;
}
/* OUTPUT:
Enter a,b,x values
123
Result=5.000000
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,y;
float result;
clrscr();
9
printf("Enter x,y values \n");
scanf("%d%d",&x,&y);
result=2.5*log(x)+cos(32)+x*2-y*2+2*x*y;
printf("\n Result=%f",result);
getch();
return 0;
}
/* OUTPUT:
Enter x,y values
10 5
Result=116.590683
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
float result;
clrscr();
printf("\n Result=%f",result);
getch();
return 0;
}
/* OUTPUT:
Enter x,y values
6 4
Result=7.333333*/
/* PROGRAM TO EVALUATE (x power 3+x power 2+x)/(y power
3+y power 2+y)+m/n+72.5 */
#include<stdio.h>
#include<conio.h>
10
#include<math.h>
int main()
{
int x,y,m,n;
float result;
//clrscr();
printf("\n Result=%f",result);
getch();
return 0;
}
/* OUTPUT:
Enter x,y,m,n values
10 5 12 4
Result=12.195439
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int k,m;
clrscr();
printf("Enter number of kilo meters \n");
scanf("%d",&k);
m=k*1000;
printf("Value in meters=%d \n",m);
getch();
return 0;
}
11
/* OUTPUT:
Enter number of kilo meters
5
Value in meters=5000
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,result;
printf("Enter any two integers: ");
scanf("%d%d",&x,&y);
result=x+~y+1;
printf("%d-%d=%d",x,y,result);
getch();
return 0;
}
/* OUTPUT:
Enter any two integers: 20
5
20-5=15
*/
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
printf("Now we are printing symbol %%");
printf("\n Now we are printing symbol %%d");
12
getch();
return 0;
}
/* OUTPUT:
Now we are printing symbol %
Now we are printing symbol %d
*/
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
if(printf("%c",59))
{
}
getch();
return 0;
}
/* OUTPUT:
;
*/
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
if(printf("HELLO WORLD"))
{
}
getch();
return 0;
}
/* OUTPUT:
13
HELLO WORLD
*/
#include<stdio.h>
#include<conio.h>
int main()
{
float f,c;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter fahrenheit value:50
Value in centigrade=10.000000
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float i;
clrscr();
14
printf("Enter any value \n");
scanf("%f",&x);
/* OUTPUT:
Enter any value
4
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int l,b,h,v;
clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter the values length, breadth and height:
4 8 6
Cylinder Volume=192
15
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int s,a,b,c;
float area;
clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter s,a,b,c values:
20 2 5 6
Area of Circle=274.954529
*/
#include<stdio.h>
#include<conio.h>
#define pi 3.14
int main()
{
int x,l,b,r;
//clrscr();
16
scanf("%d%d",&l,&b);
printf("\n Rectangle Area=%d and Perimeter=%d",l*b,2*(l+b));
printf("\n Triangle Area=%f",1/2.0*(l*b));
getch();
return 0;
}
/* OUTPUT:
Enter side for square
4
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n; // p-Principle n-No of years r-Rate of interest
float p,r,si;
clrscr();
si=(p*n*r)/100;
printf("\n Simple Interest=%f",si);
getch();
return 0;
17
}
/* OUTPUT:
Enter p,n,r values
1000
12
2.5
Simple Interest=300.000000
*/
/* PROGRAM TO CALCULATE COMPOUND INTEREST */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n; // p-Principle n-No of years r-Rate of interest
float p,r,ci;
//clrscr();
ci=p*pow(1+(r/100),n);
printf("\n Compound Interest=%f",ci);
getch();
return 0;
}
/* OUTPUT:
Enter p,n,r values
1000
12
2.5
Compound Interest=1344.888794
*/
#include<stdio.h>
#include<conio.h>
int main()
{
18
int i;
float f;
char c;
double d;
clrscr();
printf("\n size of char is %d",sizeof(char));
printf("\n size of int is %d",sizeof(int));
printf("\n size of float is %d",sizeof(f));
printf("\n size of double is %d",sizeof(d));
getch();
return 0;
}
/* OUTPUT:
size of char is 1
size of int is 2
size of float is 4
size of double is 8
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,*b;
printf("\n a=%d",a);
printf("\n a=%d",a+=10); //or a=a+10
printf("\n a=%d",a*=2);
printf("\n a=%d",a-=5);
printf("\n a=%d",a/=4);
printf("\n a=%d",a%=2);
getch();
return 0;
}
/* OUTPUT:
a=10
a=20
a=40
a=35
19
a=8
a=0
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int years,days,months,weeks,rdays;
//clrscr();
printf("Enter number of days\n");
scanf("%d",&days);
years=days/365;
months=days/30;
weeks=days/7;
rdays=days%30;
getch();
return 0;
}
/* OUTPUT:
Enter number of days
800
Years= 2
Months= 26
Weeks =114
Remaining days = 20
*/
#include <stdio.h>
#include <conio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
20
//clrscr();
/* OUTPUT:
Today's date and time : Sat Nov 23 10:52:05 2013
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
char ch;
clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter a character:A
ASCII value of A is 65
Enter a ASCII value: 68
Charcater of 68 is D
*/
#include<stdio.h>
#include<conio.h>
21
int main()
{
int x=3,y=9,z;
clrscr();
z=x&y; // bit wise and
printf("\n %d & %d = %d",x,y,z);
z=x|y; // bit wise or
printf("\n %d | %d = %d",x,y,z);
z=x^y; // bit wise xor
printf("\n %d ^ %d = %d",x,y,z);
z=~y; // bit wise compliment
printf("\n ~ %d = %ld",y,z);
z = ( y >> 1 ) ; // 1 bit wise right shift divides by 2
printf("\n %d>>1 = %d",y,z); //
z=( y << 1); // 1 bit wise left shift multiplies by 2
printf("\n %d<<1 = %d",y,z);
getch();
return 0;
}
/* OUTPUT:
3&9=1
3 | 9 = 11
3 ^ 9 = 10
~ 9 = 61276150
9>>1 = 4
9<<1 = 18
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int sub1,sub2,sub3,sum;
float avg;
clrscr();
printf("\n Enter 3 subject marks \n");
scanf("%d%d%d",&sub1,&sub2,&sub3);
sum=sub1+sub2+sub3;
avg=sum/3.0; // or avg=(sb1+sub2+sub3)/3;
22
printf("\n Total marks=%d and \n Average=%f", sum, avg);
getch();
return 0;
}
/* OUTPUT:
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
//clrscr();
float x,n;
printf("Enter a value in radians\n");
scanf("%f",&n);
x=n;
x=x*(3.142/180.0); //converting radians into degrees
printf("\n sin(%f)=%f",n,sin(x));
printf("\n cos(%f)=%f",n,cos(x));
printf("\n tan(%f)=%f",n,tan(x));
printf("\n exp(2)=%f",exp(2));
printf("\n log(10) with base e=%f",log(10));
printf("\n log10(10) with base 10=%f",log10(10));
printf("\n pow(2,4)=%f",pow(2,4));
printf("\n sqrt(9)=%f",sqrt(9));
printf("\n absolute value of abs(-5)=%f",fabs(-5));
printf("\n ceil(2.4)=%f",ceil(2.4));
printf("\n ceil(2.6)=%f",ceil(2.6));
printf("\n ceil(-2.4)=%f",ceil(-2.4));
printf("\n ceil(-2.6)=%f",ceil(-2.6));
printf("\n floor(2.4)=%f",floor(2.4));
23
printf("\n floor(2.6)=%f",floor(2.6));
printf("\n floor(-2.4)=%f",floor(-2.4));
printf("\n floor(-2.6)=%f",floor(-2.6));
getch();
return 0;
}
/* OUTPUT:
Enter a value in radians
30
sin(30.000000)=0.500059
cos(30.000000)=0.865991
tan(30.000000)=0.577441
exp(2)=7.389056
log(10) with base e=2.302585
log10(10) with base 10=1.000000
pow(2,4)=16.000000
sqrt(9)=3.000000
absolute value of abs(-5)=5.000000
ceil(2.4)=3.000000
ceil(2.6)=3.000000
ceil(-2.4)=-2.000000
ceil(-2.6)=-2.000000
floor(2.4)=2.000000
floor(2.6)=2.000000
floor(-2.4)=-3.000000
floor(-2.6)=-3.000000
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10;
// clrscr();
a=10;
printf("\n a++ = %d a = %d ++a = %d",a++,a,++a); // 11 11 11
24
printf("\na-- = %d\n",a--); //12
printf("a = %d\n",a); //11
printf("--a = %d\n",--a); //10
a=10;
printf("\n a-- = %d a = %d --a = %d",a--,a,--a); // 9 9 9
getch();
return 0;
}
/* OUTPUT:
a++ = 10
a = 11
++a = 12
a++ = 11 a = 11 ++a = 11
a-- = 12
a = 11
--a = 10
a-- = 9 a = 9 --a = 9
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
//clrscr();
getch();
return 0;
}
25
/* OUTPUT:
Enter two numbers:
10 15
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter two numbers:
10 15
*/
/* ANOTHER METHOD:
b=a+b-(a=b);
*/
26
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
//clrscr();
a=a^b;
b=a^b;
a=a^b;
getch();
return 0;
}
/* OUTPUT:
Enter two numbers:
10 15
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
//clrscr();
printf("Enter x,y values\n");
scanf("%d%d",&x,&y);
27
if(x>y)
printf("\n %d is big",x);
else
printf("\n %d is big",y);
getch();
return 0;
}
/* OUTPUT:
20 is big
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
//clrscr();
printf("Enter x,y,z values\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y&&x>z)
printf("\n %d is big",x);
else if(y>x&&y>z)
printf("\n %d is big",y);
else
printf("\n %d is big",z);
getch();
return 0;
}
/* OUTPUT:
45 is big
*/
28
/* PROGRAM TO FIND A BIGGEST NUMBER FROM THE GIVEN
THREE NUMBERS USING NESTED-IF */
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
//clrscr();
printf("Enter 3 numbers\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
if(x>z)
printf("\n %d is biggest",x);
}
else if(y>z)
printf("\n %d is biggest",y);
else
printf("\n %d is biggest",z);
getch();
return 0;
}
/* OUTPUT:
Enter 3 numbers
10 20 30
30 is big
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
//clrscr();
29
printf("\n Enter a character\n");
scanf("%c",&ch);
if(ch=='a')
printf("\n %c is a vowel",ch);
else if(ch=='e')
printf("\n %c is a vowel",ch);
else if(ch=='i')
printf("\n %c is a vowel",ch);
else if(ch=='o')
printf("\n %c is a vowel",ch);
else if(ch=='u')
printf("\n %c is a vowel",ch);
else
printf("\n %c is a consonant",ch);
getch();
return 0;
}
/* OUTPUT:
Enter a character z
z is a consonant
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
30
Enter a character z
z is a consonant
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
//clrscr();
if((x==y)&&(y==z)&&(z==x))
printf("\n Three sides are same so it is equilateral triangle");
else if((x!=y)&&(y!=z)&&(z!=x))
printf("\n Three sides are not same so it is scalene triangle");
else
printf("\n Two sides are same so it is iscoceles triangle");
getch();
return 0;
}
/* OUTPUT:
Enter 3 sides of triangle:10 20 10
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
31
//clrscr();
if((x<90)&&(y<90)&&(z<90))
printf("\n Three angles are less than 90 So it is acute triangle");
else if((x>90)||(y>90)||(z>90))
printf("\n Three sides are greater than 90 So it is obtuse triangle");
else
printf("\n It is right angled triangle");
getch();
return 0;
}
/* OUTPUT:
Enter 3 Angles of triangle:45 55 80
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Enter a character");
scanf("%c",&ch);
if(ch>=65&&ch<=90)
printf("\n %c is a upper-case letter",ch);
32
else if(ch>=97&&ch<=122)
printf("\n %c is a lower-case letter",ch);
else if(ch>=48&&ch<=57)
printf("\n %c is a digit",ch);
else if((ch>=0||ch<=47)||(ch>=58||ch<=64)||(ch>=91||ch<=96)||
(ch>=123||ch<=127))
printf("\n %c is a special symbol",ch);
getch();
return 0;
}
/* OUTPUT:
Enter a character$
$ is a special symbol
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,big;
//clrscr();
printf("Enter x,y values\n");
scanf("%d%d",&x,&y);
big=x>y?x:y;
printf("\n %d is big",big);
getch();
return 0;
}
/* OUTPUT:
45 is big
*/
33
/* PROGRAM TO FIND MAXIMUM NUMBER FROM THE GIVEN
THREE NUMBERS USING CONDITIONAL OPERATORS */
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z,big;
//clrscr();
printf("Enter x,y,z values\n");
scanf("%d%d%d",&x,&y,&z);
big=x>y?(x>z?x:z):(y>z?y:z);
printf("\n %d is big",big);
getch();
return 0;
}
/* OUTPUT:
46 is big
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
//clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n%2==0)
printf("\n %d is even",n);
else
//if(n%2==1)
printf("\n %d is odd",n);
34
getch();
return 0;
}
/* OUTPUT:
Enter a number:10
10 is even
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
//clrscr();
printf("Enter a number:");
scanf("%d",&n);
(n%2==0)?printf("\n %d is even",n) :printf("\n %d is odd",n);
getch();
return 0;
}
/* OUTPUT:
Enter a number:27
27 is odd
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
35
//clrscr();
printf("Enter a character\n");
scanf("%c",&ch);
if(ch>=97&&ch<=122)
{
printf("\n You entered lower case=%c",ch);
printf("\n Character in upper case=%c",ch-32);
}
else if(ch>=65&&ch<=90)
{
printf("\n You entered upper case=%c",ch);
printf("\n Character in lower case=%c",ch+32);
}
getch();
return 0;
}
/* OUTPUT:
Enter a character
A
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z,temp;
//clrscr();
printf("Enter x,y,z values:\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
temp=x;
x=y;
y=temp;
}
36
if(y>z)
{
temp=y;
y=z;
z=temp;
}
if(x>y)
{
temp=x;
x=y;
y=temp;
}
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,d;
float result;
if(c-d!=0)
{
37
result=((c*d)-(a*b)/(a+b))/(c-d);
printf("\n Result=%f",result);
}
else
{
printf("Divide by zero is not possible");
}
getch();
return 0;
}
/* OUTPUT:
Enter a,b,c,d values
30 20 10 10
Divide by zero is not possible
Second run:
Enter a,b,c,d values
30 20 10 5
Result=7.000000
*/
#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
if(x>1)
{
y=4*x-8;
printf("\n Y Value=%d",y);
}
if(x==0)
{
y=2*x+3;
printf("\n Y Value=%d",y);
}
38
getch();
return 0;
}
/* OUTPUT:
Enter x,y values
50
Y Value=12
*/
#include<stdio.h>
#include<conio.h>
main()
{
int x=0,k;
printf("Enter k value");
scanf("%d",&k);
if(k==3)
{
x+=2;
printf("\n X Value=%d",x);
}
else if(k==10)
{
x+=3;
printf("\n X Value=%d",x);
}
else if(k==15)
{
x+=4;
printf("\n X Value=%d",x);
39
}
getch();
return 0;
}
/* OUTPUT:
Enter k value 10
X Value=3
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
//clrscr();
printf("Enter a number\n");
scanf("%d",&n);
if(n==0)
printf("\n %d is zero",n);
if(n>0)
printf("\n %d is positive number",n);
if(n<0)
printf("\n %d is negative number",n);
getch();
return 0;
}
/* OUTPUT:
Enter a number
10
10 is positive number
*/
40
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
//clrscr();
printf("Enter a number\n");
scanf("%d",&n);
(n>0)?printf("\n %d is positive number",n):printf("\n %d is negative
number",n);
getch();
return 0;
}
/* OUTPUT:
Enter a number
-24
#include<stdio.h>
#include<conio.h>
int main()
{
int num;
//clrscr();
printf("Enter a number:");
scanf("%d",&num);
if(num%4==0)
{
if(num%9==0)
printf("%d is divisible by 4 and 9",num);
else
printf("%d is divisible only by 4",num);
}
else
41
{
if(num%9==0)
printf("%d is divisible only by 9",num);
else
printf("%d is not divisible by 4 and 9",num);
}
getch();
return 0;
}
/* OUTPUT:
Enter a number:36
36 is divisible by 4 and 9
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1;
//clrscr();
printf(" FIRST 10 NATURAL NUMBERS ARE \n");
xyz:printf("%5d",i);
i++;
if(i<=10)
goto xyz; //here xyz is label name
printf("\n End of main");
getch();
return 0;
}
/* OUTPUT:
FIRST 10 NATURAL NUMBERS ARE
1 2 3 4 5 6 7 8 9 10
End of main
*/
42
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,sum=0;
//clrscr();
xyz:sum=sum+i;
//printf("%5d",i);
i++;
if(i<=10)
goto xyz; //here xyz is label name
printf("\n Sum of first 10 natural numbers=%d",sum);
getch();
return 0;
}
/* OUTPUT:
Sum of first 10 natural numbers=55
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
//clrscr();
printf("Enter any year\n");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0))
printf("\n %d is a leap year",year);
else
printf("\n %d is not a leap year",year);
getch();
return 0;
}
/* OUTPUT:
Enter any year
2000
43
2000 is a leap year
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
//clrscr();
printf("Enter any year\n");
scanf("%d",&year);
(year%4==0&&year%100!=0)||(year%400==0)?
printf("\n %d is a leap year",year):
printf("\n %d is not a leap year",year);
getch();
return 0;
}
/* OUTPUT:
Enter any year
1998
#include<stdio.h>
#include<conio.h>
int main()
{
int a=5,b=3,c=7,result;
//clrscr();
result=a>b&&a>c;
printf("\n Result is %d",result);
result=a>b||a>c;
printf("\n Result is %d",result);
44
printf("\n Logical not of %d is %d",a,!a);
printf("\n Logical not of %d is %d",a>b,!(a>b));
getch();
return 0;
}
/* OUTPUT:
Result is 0
Result is 1
Logical not of 5 is 0
Logical not of 1 is 0
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a=5,b=3;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
5>3 Result is 1
5>=3 Result is 1
5<3 Result is 0
5<=3 Result is 0
5==3 Result is 0
5!=3 Result is 1
*/
45
//PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION
ax2+bx+c=0
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main( )
{
int a,b,c,d;
float r1,r2;
//clrscr( );
printf("Enter a,b,c values\n");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=((-b+sqrt(d))/(2*a));
r2=((-b-sqrt(d))/(2*a));
printf("\n Roots are %f,%f",r1,r2);
}
else if (d==0)
{
r1=r2=(-b/(2*a));
printf("\n Roots are %f,%f",r1,r2);
}
else
{
printf("\n Roots are imaginary");
}
getch();
return 0;
}
/* OUTPUT:
Enter a,b,c values
251
*/
#include<stdio.h>
46
#include<conio.h>
int main()
{
long int sal;
float hra,da,pf,it,gs,e,d;
//clrscr();
printf("Enter basic salary \n");
scanf("%ld",&sal);
if((sal>=1000)&&(sal<2500))
{
hra=sal*0.09;
da=sal*0.08;
pf=sal*0.03;
it=sal*0.02;
}
else if((sal>=2500)&&(sal<5000))
{
hra=sal*0.12;
da=sal*0.9;
pf=sal*0.05;
it=sal*0.04;
}
else if((sal>=5000)&&(sal<7500))
{
hra=sal*0.15;
da=sal*0.13;
pf=sal*0.08;
it=sal*0.07;
}
else
{
hra=sal*0.18;
da=sal*0.15;
pf=sal*0.09;
it=sal*0.05;
}
e=hra+da;
d=pf+it;
gs=sal+e-d;
printf("Basic salary =%ld\n",sal);
printf("hra=%f\n da=%f \n",hra,da);
printf("Earning=%f\n",e);
printf("Deductions=%f\n",d);
printf("Gross salary=%f\n",gs);
getch();
}
47
/* OUTPUT:
Enter basic salary
10000
Basic salary =10000
hra=1800.000000
da=1500.000000
Earning=3300.000000
Deductions=1400.000000
Gross salary=11900.000000
*/
#include<stdio.h>
#include<conio.h>
int main()
{
float amount,discount,net;
//clrscr();
printf("Enter purhase amount\n");
scanf("%f",&amount);
if(amount>500&&amount<1000)
discount=amount*(10/100.0);
else if(amount>1000&&amount<2000)
discount=amount*(10/100.0);
else if(amount>2000)
discount=amount*(20/100.0);
net=amount-discount;
printf("Total net to be paid is %f",net);
getch();
return 0;
}
/* OUTPUT:
48
10000
Total net to be paid is 8000.000000
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int sub1,sub2,sub3,sum;
float avg;
//clrscr();
printf("\n Enter 3 subject marks \n");
scanf("%d%d%d",&sub1,&sub2,&sub3);
sum=sub1+sub2+sub3;
avg=sum/3.0; // or avg=(sb1+sub2+sub3)/3;
if(sub1>=40&&sub2>=40&&sub3>=40)
{
if(avg>=70)
printf("\n Distinction");
else if(avg>=60&&avg<70)
printf("\n First Division");
else if(avg>=50&&avg<60)
printf("\n Second Division");
else if(avg>=40&&avg<50)
printf("\n Third Division");
}
else
printf("\n Fail");
getch();
return 0;
}
/* OUTPUT:
49
40
40
40
*/
SWITCH STATEMENT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int x,y,n;
char ch='y';
//clrscr();
do
{
printf("Enter 2 numbers\n");
scanf("%d%d",&x,&y);
printf("Enter a digit 1-add 2-sub 3-mul 4-div 5-rem 6-exit\n");
scanf("%d",&n);
switch(n)
{
case 1:printf("\n Sum=%d",x+y); break;
case 2:printf("\n Sub=%d",x-y); break;
case 3:printf("\n Mul=%d",x*y); break;
case 4:printf("\n Div=%d",x/y); break;
case 5:printf("\n Rem=%d",x%y); break;
case 6: exit(0);
default:
printf("\n Enter number between 1-6");
}
printf("\n Do you want to continue(y/n):");
50
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y');
getch();
return 0;
}
/* OUTPUT:
Enter 2 numbers
32
Enter a digit 1-add 2-sub 3-mul 4-div 5-rem 6-exit
3
Mul=6
Do you want to continue(y/n):y
Enter 2 numbers
20 10
Enter a digit 1-add 2-sub 3-mul 4-div 5-rem 6-exit
1
Sum=30
Do you want to continue(y/n):n
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int x,y,n;
char ch='y',op;
//clrscr();
do
{
printf("Enter 2 numbers\n");
scanf("%d%d",&x,&y);
printf("Enter an operator +-add --sub *-mul /-div %-rem e-exit\n");
51
fflush(stdin);
scanf("%c",&op);
switch(op)
{
case '+':printf("\n Sum=%d",x+y); break;
case '-':printf("\n Sub=%d",x-y); break;
case '*':printf("\n Mul=%d",x*y); break;
case '/':printf("\n Div=%d",x/y); break;
case '%':printf("\n Rem=%d",x%y); break;
case 'e': exit(0);
default:
printf("\n Enter correct operator");
}
printf("\n Do you want to continue(y/n):");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y');
getch();
return 0;
}
/* OUTPUT:
Enter 2 numbers
84
Enter an operator +-add --sub *-mul /-div rem e-exit
*
Mul=32
Do you want to continue(y/n):n
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch='y',op;
//clrscr();
52
do
{
printf("Enter a character\n");
fflush(stdin);
scanf("%c",&op);
switch(op)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("\n %c is a vowel",op);
break;
default:
printf("\n %c is consonant or digit or special symbol",op);
}
printf("\n Do you want to continue(y/n):");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y');
getch();
return 0;
}
/* OUTPUT:
Enter a character
u
u is a vowel
Do you want to continue(y/n):y
Enter a character
h
h is consonant or digit or special symbol
Do you want to continue(y/n):n
*/
#include<stdio.h>
#include<conio.h>
int main()
{
53
int n;
//clrscr();
printf("Enter a number between 0 and 9\n");
scanf("%d",&n);
switch(n)
{
case 0:printf("ZERO"); break;
case 1:printf("ONE"); break;
case 2:printf("TWO"); break;
case 3:printf("THREE"); break;
case 4:printf("FOUR"); break;
case 5:printf("FIVE"); break;
case 6:printf("SIX"); break;
case 7:printf("SEVEN"); break;
case 8:printf("EIGHT"); break;
case 9:printf("NINE"); break;
default:
printf("\n Your entered number is not between 0-9");
}
printf("\n End of main");
getch();
}
/* OUTPUT:
Enter a number
5
FIVE
End of main
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
//clrscr();
printf("Enter a number between 1 and 12\n");
scanf("%d",&n);
switch(n)
54
{
case 1:printf("JAN"); break;
case 2:printf("FEB"); break;
case 3:printf("MAR"); break;
case 4:printf("APR"); break;
case 5:printf("MAY"); break;
case 6:printf("JUNE"); break;
case 7:printf("JUL"); break;
case 8:printf("AUG"); break;
case 9:printf("SEP"); break;
case 10:printf("OCT"); break;
case 11:printf("NOV"); break;
case 12:printf("DEC"); break;
default:
printf("\n Your entered number is not between 1-12");
}
printf("\n End of main");
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int ch,min,hours,days,months,years,sec;
//clrscr();
55
if(ch>=1&&ch<=6)
{
printf("Enter Years:");
scanf("%d",&years);
}
months=years*12;
days=months*30;
days=days+years*5;
hours=days*24;
min=hours*60;
sec=min*60;
switch(ch)
{
case 1:
printf("\n Minutes=%d",min);
break;
case 2:
printf("\n Hours=%d",hours);
break;
case 3:
printf("\n Days=%d",days);
break;
case 4:
printf("\n Months=%d",months);
break;
case 5:
printf("\n Seconds=%d",sec);
break;
case 6:
exit(0);
break;
default:
printf("\n Your choice is not between 1 and 6");
}
getch();
return 0;
}
/* OUTPUT:
l-MINUTES
2-HOURS
3-DAYS
4-MONTHS
5-SECONDS
6-EXIT
56
Enter Your Choice:4
Enter Years:2
Months=24
*/
#include<stdio.h>
#include<conio.h>
//------------------------------------------
int main()
{
int date,month,year;
//clrscr();
find_dayofweek(date,month,year);
getch();
return 0;
}
//----------------------------------------------
int find_dayofweek(int date, int month, int year)
{
int dayofweek,yy,century;
yy=year%100;
century=year/100;
57
dayofweek=1.25*yy+leaptest(date,month,year)+date-2*(century%4);
//function of DAY for Gregorian
switch(dayofweek)
{
case 0:
printf("DAY=Saturday");
break;
case 1:
printf("DAY=Sunday");
break;
case 2:
printf("DAY=Monday");
break;
case 3:
printf("DAY=Tuesday");
break;
case 4:
printf("DAY=Wednesday");
break;
case 5:
printf("DAY=Thursday");
break;
case 6:
printf("DAY=Friday");
break;
default:
printf("Given date is wrong");
}
return 0;
}
//------------------------------------------------
int leaptest(int date, int month,int year)
{
int fmonth,leap;
58
fmonth=3+(2-leap)*((month+2)/(2*month))+(5*month+month/9)/2;
fmonth=fmonth%7; //bring it in range of 0 to 6
return fmonth;
}
/* OUTPUT:
Enter the year:2014
Enter the month:5
Enter the date:6
Given Date:6/5/2014
DAY = Tuesday
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
long n,division,n1;
int flag,digit,pos,tot_digit;
// clrscr();
printf("\n Enter any number: ");
scanf("%ld",&n);
if(n==0)
{
printf("You entered Zero\n\n");
getch();
exit(0);
}
if(n>99999)
{
printf("Please enter number between 0 and 100000\n");
getch();
exit(0);
}
tot_digit=0;
division=1;
n1=n;
59
while(n1>9)
{
n1=n1/10;
division=division*10;
tot_digit++;
}
tot_digit++;
pos = tot_digit;
while(n!=0)
{
digit=n/division;
n=n%division;
division=division/10;
switch(pos)
{
case 2:
case 5: if(digit==1)
flag=1;
else
{
flag=0;
switch(digit)
{
case 2: printf("Twenty ");break;
case 3: printf("Thirty ");break;
case 4: printf("Forty ");break;
case 5: printf("Fifty ");break;
case 6: printf("Sixty ");break;
case 7: printf("Seventy ");break;
case 8: printf("Eighty ");break;
case 9: printf("Ninty ");
}
}
break;
case 1:
case 4: if(flag==1)
{
flag=0;
switch(digit)
{
case 0 : printf("Ten ");break;
case 1 : printf("Eleven ");break;
case 2 : printf("Twelve ");break;
case 3 : printf("Thirteen ");break;
case 4 : printf("Fourteen ");break;
case 5 : printf("Fifteen ");break;
case 6 : printf("Sixteen ");break;
60
case 7 : printf("Seventeen ");break;
case 8 : printf("Eighteen ");break;
case 9 : printf("Ninteen ");
}
}
else
{
switch(digit)
{
case 1 : printf("One ");break;
case 2 : printf("Two ");break;
case 3 : printf("Three ");break;
case 4 : printf("Four ");break;
case 5 : printf("Five ");break;
case 6 : printf("Six ");break;
case 7 : printf("Seven ");break;
case 8 : printf("Eight ");break;
case 9 : printf("Nine ");
}
}
if(pos==4)
printf("Thousand ");
break;
case 3:
if(digit>0)
{
switch(digit)
{
case 1 : printf("One");break;
case 2 : printf("Two ");break;
case 3 : printf("Three ");break;
case 4 : printf("Four ");break;
case 5 : printf("Five ");break;
case 6 : printf("Six ");break;
case 7 : printf("Seven ");break;
case 8 : printf("Eight ");break;
case 9 : printf("Nine ");
}
printf("Hundred");
}
break;
}
pos--;
}
if(pos==4&&flag==0)
printf("Thousand");
else
61
if(pos==4&&flag==1)
printf("Ten Thousand");
if(pos==1&&flag==1)
printf("Ten ");
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char x;
int pinno, repeat,option;
int withdraw,deposit,amount;
int main()
{
//clrscr();
printf("--------------------------------------\n");
printf("ATM Banking System Transactions\n");
printf("--------------------------------------\n");
printf("Enter your PIN number\n");
scanf(" %d",&pinno);
if (pinno==1234)
printf("Welcome...Your access is granted\n");
else
{
printf("Sorry your pin number is wrong");
getch();
exit(0);
}
MYMENU:
printf("-------------------------------------------\n");
printf("Choose your Option\n");
62
printf("1-Inquiry Balance\n");
printf("2-Withdraw\n");
printf("3-Deposit\n");
printf("4-Exit:\n");
printf("------------------------------------------\n");
scanf(" %d",&option);
printf("\n\n");
switch(option)
{
case 1:
printf("Your balance %d\n\n",amount);
printf("Do you want another transaction(y/n)?:");
scanf(" %c",&x);
if((x == 'y')||(x== 'Y'))
{
goto MYMENU;
}
else if((x =='n')||(x== 'N'))
{
printf("\nThank You for Banking");
getch();
// exit();
}
break;
case 2:
if(amount==0)
{printf("You have an Insufficient Fund\nPress Enter");
getch();
goto MYMENU;
}
else
{
printf("Your Balance %d\n\n",amount);
printf("Enter Amount to Withdraw: ");
scanf("%d",&withdraw);
amount = amount - withdraw;
printf("\nYour Current Balance %d\n\n",amount);
printf("Do you want another transaction(y/n)?:");
scanf(" %c",&x);
if((x =='y')||(x=='Y'))
{
goto MYMENU;
}
else if ((x =='n')||(x=='N'))
{
63
printf("\nThank You for Banking");
getch();
// exit();
}}
break;
case 3:
printf("Your Balance: %d\n",amount);
printf("\nEnter Amount to Deposit: ");
scanf("%d",&deposit);
amount= amount + deposit;
printf("\nYour Current Balance %d\n",amount);
printf("Do you want another transaction(y/n)?:");
scanf(" %c",&x);
if ((x =='y')||(x=='Y'))
{
goto MYMENU;
}
else if ((x =='n')||(x=='N'))
{
printf("\n\nThank You for Banking");
getch();
// exit();
}
break;
case 4:
printf("Do you Want to Exit(y/n)?: ");
scanf(" %c",&x);
if ((x=='y')||(x=='Y'))
{
printf("\n\nThank You & Come Again");
getch();
exit(0);
}
else if ((x =='n')||(x=='N'))
{
goto MYMENU;
}
break;
default:
while(repeat);
getch();
return 0;
}
}
64
/* OUTPUT:
--------------------------------------
ATM Banking System Transactions
--------------------------------------
Enter your PIN number
1234
Welcome...Your access is granted
--------------------------
Choose your Option
1-Inquiry Balance
2-Withdraw
3-Deposit
4-Exit:
------------------------------------------
1
Your balance: 0
Your Balance: 0
65
Your Balance: 2000
*/
PREPROCESSOR DIRECTIVES
#include <stdio.h>
#include<conio.h>
int main()
{
printf("Current time: %s",__TIME__); //calculate the current time
66
printf("\nCurrent date: %s",__DATE__); //calculate the current date
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
# define pi 3.14 // MACRO
int main()
{
float r;
//clrscr();
#ifdef pi
printf("\n pi is defined");
#else
printf("\n pi is not defined");
#endif
getch();
return 0;
}
/* output:
pi is defined
*/
#include<stdio.h>
#include<conio.h>
# define pi 3.14 // MACRO
int main()
67
{
float r;
//clrscr();
printf("\n ENTER RADIUS VALUE");
scanf("%f",&r);
printf("\n circle area=%f",pi*r*r);
printf("\n circle perimeter=%f",2*pi*r);
//pi=pi+100; ERROR
getch();
return 0;
}
/* output:
circle area=78.500000
circle perimeter=31.400000
*/
int main()
{
int a=2,b=30,c=10;
//clrscr();
X
if(a>b AND a>c)
printf("\n %d is big",a);
else
printf("\n %d is not big ",a);
X
if(y)
printf("\n %d is big",b);
else
printf("\n %d is not big ",b);
getch();
}
/* OUTPUT:
68
welcome to c
2 is not big
welcome to c
30 is big
*/
#include<stdio.h>
#include<conio.h>
# define OS 3 // OPERATING SYSTEM VERSION
int main()
{
//clrscr();
#if OS==1
printf("\n Operating system version is 1");
#elif OS==2
printf("\n Operating system version is 2");
#elif OS==3
printf("\n Operating system version is 3");
#else
printf("\n Operating system version is not defined");
#endif
getch();
return 0;
}
/* output:
Operating system version is 3
*/
#include<stdio.h>
#include<conio.h>
void f1()
{
printf("\n\n IT IS F1 FUNCTION EXECUTES BEFORE MAIN START");
getch();
}
69
void f2()
{
printf("\n\n IT IS F2 FUNCTION EXECUTES BEFORE MAIN END");
getch();
}
#pragma startup f1
#pragma exit f2
int main()
{
//clrscr();
printf("\n IT IS MAIN FUNCTION");
getch();
return 0;
}
/* OUTPUT:
LOOPS
#include<stdio.h>
#include<conio.h>
int main( )
{
int i=0; //initialization
//clrscr( );
printf("FIRST 10 NATURAL NUMBERS ARE\n");
while(i<=10) //checking condition
{
printf("%5d",i);
i++; //increment i by 1
}
70
getch();
return 0;
}
/* OUTPUT:
FIRST 10 NATURAL NUMBERS ARE
0 1 2 3 4 5 6 7 8 9 10
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i=10; //initialization
//clrscr( );
printf("FIRST 10 NATURAL NUMBERS IN REVERSE ORDER ARE\n");
do
{
printf("%5d",i);
i--; //decrement i by 1
}while(i>=1); //checking condition
getch();
return 0;
}
/* OUTPUT:
FIRST 10 NATURAL NUMBERS IN REVERSE ORDER ARE
10 9 8 7 6 5 4 3 2 1
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,min,max;
//clrscr( );
71
printf("ENTER MINIMUM & MAXIMUM VALUE\n");
scanf("%d%d",&min,&max);
for(i=min;i<=max;i++)
printf("%5d",i);
getch();
return 0;
}
/* OUTPUT:
ENTER MINIMUM & MAXIMUM VALUE
10 16
10 11 12 13 14 15 16
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,sum=0;
float avg;
//clrscr( );
for(i=1;i<=10;i++)
sum+=i; //sum=sum+i;
avg=sum/10.0;
printf("Sum of first 10 natural numbers is %d\n",sum);
printf("Average of first 10 natural numbers is %f",avg);
getch();
return 0;
}
/* OUTPUT:
Sum of first 10 natural numbers is 55
Average of first 10 natural numbers is 5.500000
*/
#include<stdio.h>
#include<conio.h>
72
int main()
{
int i,n,fact=1;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter a number
5
Factorial of 5 is 120
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,n;
//clrscr( );
printf("ENTER n VALUE\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("\n %d is factor of %d",i,n);
}
getch();
return 0;
}
73
/* OUTPUT:
ENTER n VALUE
6
1 is factor of 6
2 is factor of 6
3 is factor of 6
6 is factor of 6
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=-1,j=1,k,n;
//clrscr();
printf("Enter n value\n");
scanf("%d",&n);
printf("\n Febanacci series numbers are \n");
k =i+j;
while(k<=n)
{ printf("%5d",k);
i =j;
j =k;
k =i+j;
}
getch();
return 0;
}
/* OUTPUT:
Enter n value
20
Febanacci series no.s are
0 1 1 2 3 5 8 13
*/
#include<stdio.h>
74
#include<conio.h>
int main()
{
int i, j, k;
printf("\n All combinations of 1, 2 and 3 are:\n");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
printf("\n%d%3d%3d",i,j,k);
}
}
}
getch();
return 0;
}
/* OUTPUT:
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
75
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
*/
int main( )
{
int x,y,i,ans=1;
//clrscr( );
for(i=1;i<=y;i++)
ans=ans*x;
getch();
return 0;
}
/* OUTPUT:
Enter base and power values
25
2 power 5 is 32
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
76
int i,sum=0;
//clrscr( );
for(i=1;i<=10;i++)
sum+=i; //sum=sum+i;
printf("Sum of first 10 natural numbers is %d",sum);
getch();
return 0;
}
/* OUTPUT:
Sum of first 10 natural numbers is 55
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,sum=0,temp,rem;
//clrscr( );
printf("Enter a number \n");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
printf("\n After adding 1 to %d is %d",rem,rem+1);
n=n/10;
}
getch();
return 0;
}
/* OUTPUT:
Enter a number
429
After adding 1 to 9 is 10
After adding 1 to 2 is 3
After adding 1 to 4 is 5
*/
77
// Program to add first and last digit of a three digit number
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,x,y;
//clrscr( );
printf("Enter three digit number \n");
scanf("%d",&n);
x=n%10;
y=n/100;
getch();
return 0;
}
/* OUTPUT:
Enter three digit number
456
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,sum=10,rem;
//clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(sum>=10)
{
sum=0;
while(n>0)
78
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
n=sum;
}
printf("Sum of digits = %d ",sum);
getch();
return 0;
}
/* OUTPUT:
Enter a number:167
Sum of digits = 5
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,sum=0,temp,rem;
//clrscr( );
printf("Enter a number \n");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("Sum of individuals of given number %d is %d",temp,sum);
getch();
return 0;
}
/* OUTPUT:
Enter a number
425
79
Sum of individuals of given number 425 is 11
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,sum=0,temp;
//clrscr( );
printf("Enter a number:");
scanf("%d",&n);
temp=n;
for(;n>0;sum+=n%10,n/=10);
getch();
return 0;
}
/* OUTPUT:
Enter a number:425
Sum of individuals of given number 425 is 11
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,min,max,sum=0;
//clrscr( );
printf("Enter minimum and maximum range\n");
scanf("%d%d",&min,&max);
for(i=min;i<=max;i++)
{
if(i%2==0)
sum=sum+i;
80
}
printf("Sum of even numbers between %d and %d is %d",min,max,sum);
getch();
return 0;
}
/* OUTPUT:
Enter minimum and maximum range
10 80
Sum of even numbers between 10 and 80 is 1620
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,rem,evensum,rev,count=1;
rev=evensum=0;
printf("Enter a number:");
scanf("%d", &n);
/* Reverse the number first. Because, we will
reaping digits from the LSB to MSB */
while(n>0)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}
n=rev;
81
n=n/10;
count++;
}
getch();
return 0;
}
/* OUTPUT:
Enter a number:12345
Sum of even digits is 6
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,n,sum=0;
//clrscr( );
printf("ENTER n VALUE\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
sum+=i;
getch();
return 0;
}
/* OUTPUT:
ENTER n VALUE
10
Sum of natural numbers upto 10 is 55
*/
82
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,min,max,sum=0;
//clrscr( );
printf("Enter minimum and maximum range\n");
scanf("%d%d",&min,&max);
for(i=min;i<=max;i++)
{
if(i%2==1)
sum=sum+i;
}
printf("Sum of odd numbers between %d and %d is %d",min,max,sum);
getch();
return 0;
}
/* OUTPUT:
Enter minimum and maximum range
10 20
Sum of odd numbers between 10 and 20 is 75
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,rem,oddsum,rev,count=1;
rev=oddsum=0;
printf("Enter a number:");
scanf("%d", &n);
/* Reverse the number first. Because, we will
reaping digits from the LSB to MSB */
while(n>0)
{
rem=n%10;
83
rev=(rev*10)+rem;
n=n/10;
}
n=rev;
getch();
return 0;
}
/* OUTPUT:
Enter a number:12345
Sum of odd digits is 9
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,k=0;
//clrscr();
printf("Enter a number \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
k++;
}
if(k==2)
printf("%d is a prime number",n);
else
84
printf("%d is not a prime number",n);
getch();
return 0;
}
/* OUTPUT:
Enter a number
9
9 is not a prime number
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,k,count=0;
//clrscr();
for(n=1;n<=500;n++)
{
k=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
k++;
}
if(k==2)
count++;
}
printf("Total number of prime numbers between 1-500=%d",count);
getch();
return 0;
}
/* OUTPUT:
Total number of prime numbers between 1-500=95
*/
#include<stdio.h>
#include<conio.h>
85
int main()
{
int n,i,k;
//clrscr();
printf("Prime numbers between 1-100 \n");
for(n=1;n<=100;n++)
{
k=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
k++;
}
if(k==2)
printf("\n%d is a prime number",n);
}
getch();
return 0;
}
/* OUTPUT:
2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
53 is a prime number
59 is a prime number
61 is a prime number
67 is a prime number
71 is a prime number
73 is a prime number
79 is a prime number
83 is a prime number
89 is a prime number
97 is a prime number
86
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,count=0;
//clrscr( );
for(i=1;i<=100;i++)
{
if(i%2!=0&&i%3!=0&&i%5!=0)
count++;
}
getch();
return 0;
}
/* OUTPUT:
Total numbers not divisible by 2,3 and 5 are 26
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i,count=0;
//clrscr( );
for(i=100;i<=200;i++)
{
if(i%2==0)
count++;
}
printf("Even numbers between 100 and 200 are %d",count);
getch();
87
return 0;
}
/* OUTPUT:
Even numbers between 100 and 200 are 51
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i;
//clrscr( );
printf("Even numbers between 100 and 200 are\n");
for(i=100;i<=200;i++)
{
if(i%2==0)
printf("%5d",i);
}
getch();
return 0;
}
/* OUTPUT:
Even numbers between 100 and 200 are
100 102 104 106 108 110 112 114 116 118 120 122 124 126
128 130
132 134 136 138 140 142 144 146 148 150 152 154 156 158
160 162
164 166 168 170 172 174 176 178 180 182 184 186 188 190
192 194
196 198 200
*/
#include<stdio.h>
#include<conio.h>
int main( )
88
{
int i,count=0;
//clrscr( );
for(i=100;i<=200;i++)
{
if(i%2==1)
count++;
}
printf("Odd numbers between 100 and 200 are %d",count);
getch();
return 0;
}
/* OUTPUT:
Odd numbers between 100 and 200 are 50
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int i;
//clrscr( );
printf("Odd numbers between 100 and 200 are\n");
for(i=100;i<=200;i++)
{
if(i%2==1)
printf("%5d",i);
}
getch();
return 0;
}
/* OUTPUT:
Odd numbers between 100 and 200 are
101 103 105 107 109 111 113 115 117 119 121 123 125 127
129 131
133 135 137 139 141 143 145 147 149 151 153 155 157 159
161 163
89
165 167 169 171 173 175 177 179 181 183 185 187 189 191
193 195
197 199
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sum=0;
//clrscr();
printf("Enter a no \n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("\n %d is a perfect number",n);
else
printf("\n %d is not a perfect number",n);
getch();
return 0;
}
/*OUTPUT:
Enter a no
6
6 is a perfect number
*/
#include<stdio.h>
#include<conio.h>
int main()
90
{
int i,n,sum,count=0;
//clrscr();
for(n=1;n<=1000;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
count++;
}
printf("Perfect numbers between 1-1000 are %d",count);
getch();
return 0;
}
/*OUTPUT:
Perfect numbers between 1-1000 are 3
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sum;
//clrscr();
91
printf("\n %d is a perfect number",n);
}
getch();
return 0;
}
/*OUTPUT:
Perfect numbers between 1-1000 are
6 is a perfect number
28 is a perfect number
496 is a perfect number
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,rev=0,temp,rem;
//clrscr( );
printf("Enter a number \n");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("Reverse of a given number %d is %d",temp,rev);
getch();
return 0;
}
/* OUTPUT:
Enter a number
257
Reverse of a given number 257 is 752
*/
92
//Program to check whether a given number is polyndrome or not
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,rev=0,temp,rem;
//clrscr( );
printf("Enter a number \n");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("Reverse of a given number %d is %d",temp,rev);
if(rev==temp)
printf("\n %d is a polyndrome number",temp);
else
printf("\n %d is not a polyndrome number",temp);
getch();
return 0;
}
/* OUTPUT:
Enter a number
353
Reverse of a given number 353 is 353
353 is a polyndrome number
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,sum=0,temp,rem;
93
//clrscr( );
printf("Enter a number:");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==temp)
printf("\n %d is a armstrong number",temp);
else
printf("\n %d is not a armstrong number",temp);
getch();
return 0;
}
/* OUTPUT:
Enter a number:153
153 is a armstrong number
*/
int main()
{
int n,t,rem,sum,i;
//clrscr();
94
t=t/10;
}
if(n==sum)
printf("%4d",n);
}
getch();
return 0;
}
/* OUTPUT:
Armstrong numbers between 1 and 500 are
1 370 371 407
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,x,y;
printf("\nEnter any two numbers:");
scanf("%d%d",&a,&b);
x=a,y=b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
printf("L.C.M=%d",x*y/a);
getch();
return 0;
}
/*OUTPUT:
95
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,f,s,t,r;
//clrscr();
printf("Enter a,b values:");
scanf("%d%d",&a,&b);
f=a;
s=b;
if(a<b)
{
t=a;
a=b;
b=t;
}
while(b>=0)
{
if(b>=1)
{
r=a%b;
a=b;
b=r;
}
else
{
printf("\n G.C.D(%d,%d)=%d\n",f,s,a);
break;
}
}
getch();
return 0;
}
/* OUTPUT:
Enter a,b values: 6 12
G.C.D(12,6)=6
*/
96
#include<stdio.h>
#include<conio.h>
int main( )
{
int n,count=0,temp,rem,digit;
//clrscr( );
printf("Enter a number \n");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
if(rem==digit)
{
count++;
}
n=n/10;
}
printf("\n %d is found %d times in a number %d",digit,count,temp);
getch();
return 0;
}
/* OUTPUT:
Enter a number
121341
Enter a digit
1
#include<stdio.h>
#include<conio.h>
int main( )
{
97
int n,sum=0,temp,rem;
//clrscr( );
printf("Enter a number:");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem);
n=n/10;
}
printf("\n Sum of square of each digit of number %d is %d",temp,sum);
getch();
return 0;
}
/* OUTPUT:
Enter a number:1234
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
//clrscr();
for(i=1;1<=4;i++)
{
printf("\n First loop");
if(i==3)
break;
for(j=1;j<=3;j++)
{
printf("\n Second loop");
}
}
98
getch();
return 0;
}
/* OUTPUT:
First loop
Second loop
Second loop
Second loop
First loop
Second loop
Second loop
Second loop
First loop
Out of loops
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n,x,res;
getch();
return 0;
}
/* OUTPUT:
99
How many numbers you want to enter: 3
Square root=2
Square root=3
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
//clrscr();
printf("Enter a number for multiplication table \n");
scanf("%d",&n);
for(i=1;i<=10;i++)
printf("\n %4d x %4d = %4d",n,i,n*i);
getch();
return 0;
}
/* OUTPUT:
Enter a number for muliplication table
5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
100
5x 8 = 40
5x 9 = 45
5x 10 = 50
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
//clrscr();
printf("Multiplication table for 1-4\n");
printf("----------------------------------\n");
for(n=1;n<=10;n++)
{
for(i=1;i<=4;i++)
{
printf("\t %d x %d = %d",i,n,n*i);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Multiplication table for 1-4
----------------------------------
1x1=1 2x1=2 3x1=3 4x1=4
1x2=2 2x2=4 3x2=6 4x2=8
1x3=3 2x3=6 3x3=9 4 x 3 = 12
1x4=4 2x4=8 3 x 4 = 12 4 x 4 = 16
1x5=5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20
1x6=6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24
1x7=7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28
1x8=8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32
1x9=9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40
*/
101
//Program to print all ASCII values and their equivalent
characters using a while loop
#include<stdio.h>
#include<conio.h>
int main( )
{
int i=0;
//clrscr( );
printf("ASCII VALUES AND EQUIVALENT CHARACTERS ARE\n");
printf("------------------------------------------\n");
while(i<=255)
{
printf("%c=%d\t",i,i);
i++;
}
getch();
return 0;
}
OUTPUT:
#include<stdio.h>
102
#include<conio.h>
#include<stdlib.h>
int main( )
{
int n,temp,rem,k=1;
//clrscr( );
printf("Enter a number \n");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
if(rem!=1&&rem!=0)
{
k=0;
printf("\n %d is not a binary number",temp);
getch();
exit(0);
}
else
k=1;
n=n/10;
}
if(k==1)
printf("\n %d is a binary number",temp);
getch();
return 0;
}
/* OUTPUT:
Enter a number
1011
1011 is a binary number
*/
#include<stdio.h>
#include<conio.h>
int main()
103
{
unsigned int num;
int count=0,temp;
temp=num;
while(num!=0)
{
if((num&1)==1)
count++;
num=num>>1;
}
printf("\n num of 1's in %d binary equivalent is:%d",temp,count);
getch();
return 0;
}
/* OUTPUT:
Enter the unsigned integer:15
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i=0,rem,sum=0;
printf("Enter a binary number\n");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
sum=sum+rem*pow(2,i);
n=n/10;
104
i++;
}
/* OUTPUT:
Enter a binary number 101
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int temp=1,r,num,sum=0,i,j=0,arr[10];
while(num>0)
{
r=num%2;
if(r>1)
{
printf("Invalid entry");
exit(0);
}
sum=sum+(r*temp);
temp=temp*2;
num=num/10;
}
while(sum>0)
{
r=sum%16;
arr[j]=r;
j++;
sum=sum/16;
}
105
printf("Hexadecimal equivalent= ");
for(i=j-1;i>=0;i--)
{
switch(arr[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d",arr[i]);
break;
}
}
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
106
int main()
{
int n,temp,octal,i,rem,pow2,pow8,count;
//clrscr();
printf("\nEnter the Binary number:");
scanf("%d",&n);
temp=n;
pow8=1;
octal=0;
while(temp>0)
{
i=0;
count=0;
pow2=1;
while(i<=2)
{
if(temp==0)
break;
rem=temp%10;
count+=rem*pow2;
i++;
pow2=pow2*2;
temp=temp/10;
}
octal+=count*pow8;
pow8=pow8*10;
}
getch();
return 0;
}
/* OUTPUT:
107
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
int i=0,rem=0,sum[30];
//clrscr();
printf("Enter the first binary number: ");
scanf("%d", &x);
printf("Enter the second binary number: ");
scanf("%ld", &y);
while(x!=0||y!=0)
{
sum[i]=(x%10+y%10+rem)%2;
rem=(x%10+y%10+rem)/2;
x=x/10;
y=y/10;
i++;
}
if(rem!=0)
sum[i++]=rem;
--i;
printf("Sum of two binary numbers: ");
while(i>=0)
{
printf("%d", sum[i]);
i--;
}
getch();
return 0;
}
/* OUTPUT:
Enter the first binary number: 10
Enter the second binary number: 01
Sum of two binary numbers: 11
*/
#include<stdio.h>
#include<conio.h>
108
int fun(int x, int y);
int main()
{
int x,y,mul=0;
int digit,f=1;
while(y!=0)
{
digit=y%10;
if(digit==1)
{
x=x*f;
mul=fun(x,mul);
}
else
x=x*f;
y=y/10;
f=10;
}
printf("Product of two binary numbers: %d", mul);
getch();
return 0;
}
while(x!=0||y!=0)
{
sum[i]=(x%10+y%10+rem)%2;
rem=(x%10+y%10+rem)/2;
x=x/10;
y=y/10;
i++;
}
if(rem!=0)
sum[i++]=rem;
--i;
109
while(i>=0)
temp=temp*10+sum[i--];
return temp;
}
/* OUTPUT:
Enter the first binary number: 11
Enter the second binary number: 11
Product of two binary numbers: 1001
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,count=0,temp;
//clrscr();
while(n>0)
{
n=n&(n-1);
count++;
}
printf("Number of 1's in a binary %d is %d",temp,count);
getch();
return 0;
}
/* OUTPUT:
Enter a number
15
Number of 1's in a binary 15 is 4
*/
110
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int rem,i=1,binary=0,n,temp;
temp=n;
while (n!=0)
{
rem=n%2;
n=n/2;
binary=binary+rem*i;
i=i*10;
}
printf("%d binary value is %d",temp,binary);
getch();
return 0;
}
/* OUTPUT:
Enter decimal number:15
15 binary value is 1111
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rem,quotient,i=1,k,temp;
char hex[100];
quotient=n;
111
while(quotient>0)
{
temp=quotient%16;
getch();
return 0;
}
/* OUTPUT:
Enter any decimal number: 1515
Hexadecimal value of decimal number 1515 is: 5EB
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rem,quotient,octalNumber[50],i=1,k;
while(quotient>0)
{
octalNumber[i]=quotient%8;
quotient=quotient/8;
i++;
112
}
getch();
return 0;
}
/* OUTPUT:
Enter the decimal number: 25
Equivalent octal value of decimal number 25 is: 31
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int n;
printf("Enter Deminal Number:");
scanf("%d", &n);
printf("\n Roman Number is:");
while(n>0)
{
if(n>=1000)
{
//M - 1000
printf("M");
n=n-1000;
}
else if(n>=500)
{
//D is 500. CM is 900
//CM = M - C = 1000 - 100 => 900
if(n>=900)
{
printf("CM");
n=n-900;
}
113
else
{
printf("D");
n=n-500;
}
}
else if(n>=100)
{
//C is 100. CD is 400
//CD = D - C = 500 - 100 => 400
if(n>=400)
{
printf("CD");
n=n-400;
}
else
{
printf("C");
n=n-100;
}
}
else if(n>=50)
{
//L is 50. XC is 90
//XC = C - X = 100 - 10 => 90
if(n>=90)
{
printf("XC");
n=n-90;
}
else
{
printf("L");
n=n-50;
}
}
else if(n>=9)
{
//XL is 40. IX is 9. X is 10
//XL = L - X = 50 - 10 = 40
//IX = X - I = 10 - 1 = 9
if(n>=40)
{
114
printf("XL");
n=n-40;
}
else if(n==9)
{
printf("IX");
n=n-9;
}
else
{
printf("X");
n = n - 10;
}
}
else if(n>=4)
{
//V is 5 and IV is 4
//IV = V - I = 5 - 1 => 4
if(n>=5)
{
printf("V");
n=n-5;
}
else
{
printf("IV");
n=n-4;
}
}
else
{
printf("I");
n=n-1;
}
}
printf("\n");
getch();
return 0;
}
/* OUTPUT:
Enter Deminal Number:24
115
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main()
{
char hex_num[25];
int i,j,num,len,k=0;
long int sum=0; //value can cross range of int
strupr(hex_num);
len=strlen(hex_num);
for(i=len-1;i>=0;i--)
{
if(hex_num[i]>='0'&&hex_num[i]<='9')
num=hex_num[i]-'0';
else
{
if(hex_num[i]>='A'&&hex_num[i]<='F')
num=hex_num[i]-55;
else
{
printf("The Entered number is not hexdecimal number");
getch();
return 1;
}
}
sum=sum+pow(16,k)*num;
k++;
}
printf("hexadecimal conversion of %s to decimal is: %ld",hex_num,sum);
getch();
return 0;
}
116
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,dec=0,i=0,temp;
while(n!=0)
{
dec=dec+(n%10)*pow(8,i++);
n=n/10;
}
printf("Equivalent decimal value of octal %d is %d",temp,dec);
getch();
return 0;
}
/*OUTPUT:
Enter any octal number:548
Equivalent decimal value of octal 0 is 360
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
117
int main()
{
char num[1000];
int i=0;
long int t=0;
while(num[i])
{
if(fun(num[i])<0)
{
printf("Invalid roman digit:%c",num[i]);
getch();
exit(1);
return 0;
}
if((strlen(num)-i)>2)
{
if(fun(num[i])<fun(num[i+2]))
{
printf("Invalid roman t");
return 0;
}
}
if(fun(num[i])>=fun(num[i+1]))
t=t+fun(num[i]);
else
{
t=t+(fun(num[i+1])-fun(num[i]));
i++;
}
i++;
}
printf("Decimal val is:%ld",t);
getch();
return 0;
}
int fun(char c)
{
int val=0;
118
switch(c)
{
case 'I':
val=1; break;
case 'V':
val=5; break;
case 'X':
val=10; break;
case 'L':
val=50; break;
case 'C':
val=100; break;
case 'D':
val=500; break;
case 'M':
val=1000; break;
case '\0':
val=0; break;
default:
val=-1;
}
return val;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main( )
{
int i;
//clrscr( );
119
{
printf("\n %d \t %d \t\t %0.4f \t %0.2f \t
%0.2f",i,i*i,pow(i,3),sqrt(i),1.0/i);
}
getch();
return 0;
}
/* OUTPUT:
N SQUARE CUBE SQUARE ROOT 1/n
------------------------------------------------------
#include<stdio.h>
#include<conio.h>
int main( )
{
int row,col;
//clrscr( );
for(col=1;col<=24;col++)
{
for(row=1;row<=80;row++)
printf("%c",1);
}
getch();
return 0;
}
/* OUTPUT:
120
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char ch1, ch2, ch3;
//clrscr();
for(ch1='X';ch1<='Z';ch1++)
{
for(ch2='X';ch2<='Z';ch2++)
for(ch3='X';ch3<='Z';ch3++)
printf("%c%c%c\t",ch1,ch2,ch3);
}
getch();
return 0;
}
/* OUTPUT:
XXX XXY XXZ XYX XYY XYZ XZX XZY XZZ YXX
YXY YXZ YYX YYY YYZ YZX YZY YZZ ZXX ZXY
ZXZ ZYX ZYY ZYZ ZZX ZZY ZZZ
*/
121
LOOP SERIES PROGRAMS
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sum=0;
//clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
{
sum+=i; //sum=sum+i
}
printf("\n Sum=%d",sum);
getch();
return 0;
}
/* OUTPUT:
Enter a number:10
Sum=55
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sum=0;
//clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i=i+2)
{
sum+=i; //sum=sum+i
}
printf("\n Sum=%d",sum);
122
getch();
return 0;
}
/* OUTPUT:
Enter a number:10
Sum=25
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sum=0;
//clrscr();
printf("Enter n value:");
scanf("%d",&n);
for(i=0;i<=n;i=i+2)
{
sum+=i; //sum=sum+i
}
printf("\n Sum=%d",sum);
getch();
return 0;
}
/* OUTPUT:
Enter a number:10
Sum=30
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n,sum=1,x;
//clrscr();
123
printf("Enter range and x value \n");
scanf("%d%d",&n,&x);
for(i=1;i<=n;i=i+1)
{
sum+=pow(x,i); //sum=sum+i
}
printf("\n Sum=%d",sum);
getch();
return 0;
}
/* OUTPUT:
Enter range and x value
51
Sum=6
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n;
double temp,sum=0;
//clrscr();
printf("Enter n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
temp=(i/fact(i));
printf("\n temp=%f",temp);
sum=sum+pow(-1,i)*temp;
}
printf("\n Sum=%f",sum);
getch();
124
return 0;
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n Factorial of %d is %d",n,f);
return f;
}
/* OUTPUT:
Enter n value:1
Factorial of 1 is 1
temp=1.000000
Sum=-1.000000
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n,x;
double temp,sum=1;
//clrscr();
for(i=1;i<=n;i++)
{
if(i%2==0)
{
125
temp=pow(x,2)/fact(i);
sum=sum+temp;
}
}
printf("\n Sum=%f",sum);
getch();
return 0;
}
int fact(int n)
{
int i,f=1;
if(n==0 || n==1)
return 1;
else
{
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n Factorial of %d is %d",n,f);
}
return f;
}
/* OUTPUT:
Enter n and x values
41
Factorial of 2 is 2
Factorial of 4 is 24
Sum=1.541667
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
126
int main()
{
int i,n,x;
double temp,sum=0;
//clrscr();
for(i=1;i<=n;i++)
{
if(i%2==1)
{
temp=pow(x,i)/fact(i);
sum=sum+temp;
}
}
printf("\n Sum=%f",sum);
getch();
return 0;
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n Factorial of %d is %d",n,f);
return f;
}
/* OUTPUT:
Enter n and x values
31
Factorial of 1 is 1
Factorial of 3 is 6
Sum=1.166667
*/
#include<stdio.h>
127
#include<conio.h>
#include<math.h>
int main()
{
int i,n;
double sum=0;
//clrscr();
getch();
return 0;
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n Factorial of %d is %d",n,f);
return f;
}
/* OUTPUT:
Enter n value
4
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial 4 is 24
Sum=1.708333
*/
128
/* PROGRAM TO FIND e^x SUM=1+x+x^2/2!/+x^3/3!+x^4/4!
+............+x^n/n! */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,n,x,f;
double sum=1;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter n and x values:3 1
Exponentional Sum=2.666667
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
129
int i,n,sum=0;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter a number 4
Sum=30
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n;
double sum=0;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter a number 5
130
Sum=225.000000
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n;
double sum=1;
//clrscr();
printf("Enter n value\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
sum=sum+pow(i,2);
}
printf("\n Sum=%f",sum);
getch();
return 0;
}
/* OUTPUT:
Enter n value 4
Sum=21
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n;
double sum=0;
//clrscr();
printf("Enter n value\n");
131
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==1)
sum=sum+pow(i,2);
}
printf("\n Sum=%f",sum);
getch();
return 0;
}
/* OUTPUT:
Enter n value 5
Sum=35
*/
/*x-x2/2!+x3/3!-x4/4!.........*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,f,n,x,k=-1;
float sum=0;
//clrscr();
printf("Enter range and x value\n");
scanf("%d%d",&n,&x);
//sum=x;
for(i=1;i<=n;i=i+1)
{
k=k*(-1);
f=1;
for(j=1;j<=i;j++)
f=f*j;
sum=sum+k*pow(x,i)/f;
}
printf("\n ANSWER=%f",sum);
getch();
}
/* OUTPUT:
Enter range and x value
5 1
132
ANSWER=0.633333
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n,x;
double sum=1;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter n and x values:4 1
Sum=3.083333
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n;
133
double sum=0;
//clrscr();
printf("Enter n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+1.0/pow(i,2);
}
printf("\n Sum=%f",sum);
getch();
return 0;
}
/* OUTPUT:
Enter n value:3
Sum=1.361111
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,i,j,n,f=1,k=2;
float sum=1;
//clrscr();
134
sum=sum-pow(x,i)/f;
}
else
{
for(j=1;j<=i;j++)
f=f*j;
sum=sum+pow(x,i)/f;
}
k++;
}
printf("\n SUM OF GIVEN SERIES=%f",sum);
getch();
return 0;
}
/* OUTPUT:
Enter n and x values:3 1
*/
COMPLETED
/* PROGRAM TO FIND SIN SERIES SUM=x-x^3/3!+x^5/5!-
x^7/7!+x^9/9!............ */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,n,k;
float x,temp,sum,r;
for(k=2;k<=n;k++)
{
temp=(temp*(-1)*r*r)/(i*(i+1));
sum=sum+temp;
i=i+2;
}
getch();
return 0;
}
/* OUTPUT:
Enter number of terms:2
*/
PATTERN PROGRAMS
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n;
//clrscr();
136
{
for(j=1;j<=i;j++)
{
printf("%4d",i);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter n value for number of lines
4
1
2 2
3 3 3
4 4 4 4
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n;
//clrscr();
137
getch();
return 0;
}
/* OUTPUT:
Enter n value for number of lines
4
1
1 2
1 2 3
1 2 3 4
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,c,k,sp=1; //sp is for space
sp=n-1;
for(k=1;k<=n;k++)
{
for(c=1;c<=sp;c++)
printf(" ");
sp--;
for(c=1;c<=2*k-1;c++)
printf("*");
printf("\n");
}
sp=1;
for(k=1;k<=n-1;k++)
{
for(c=1;c<=sp;c++)
printf(" ");
sp++;
for(c=1;c<=2*(n-k)-1;c++)
printf("*");
printf("\n");
}
138
getch();
return 0;
}
/* OUTPUT:
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n;
//clrscr();
139
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter n value for number of lines:4
4 3 2 1
4 3 2
4 3
4
*/
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,x,n,k;
//clrscr();
getch();
140
return 0;
}
/* OUTPUT:
Enter n value for number of lines:4
1
1 2
1 2 3
1 2 3 4
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter n value for number of lines:4
1 2 3 4
1 2 3
141
1 2
1
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,k=0;
//clrscr();
for(j=1;j<=i;j++)
{
k++;
printf("%4d",k);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter n value for number of lines:4
1
2 3
4 5 6
7 8 9 10
*/
142
*
* *
* * *
* * * *
* * * * *
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int line,star,n;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
Enter n value for number of lines
5
*
* *
* * *
* * * *
* * * * *
*/
*/
143
#include<stdio.h>
#include<conio.h>
int main()
{
int i,x,n,k;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
EnEnter n value for number of lines:5
1
2 2
3 3 3
4 4 4 4
*/
*/
#include<stdio.h>
144
#include<conio.h>
int main()
{
int i,x,y,n,k;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
EnEnter n value for number of lines:4
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
*/
#include<stdio.h>
#include<conio.h>
int main()
145
{
int i,j,zero=0,one=1,n,m;
//clrscr();
getch();
return 0;
}
/* OUTPUT:
EnEnter n value for number of lines:4
1
0 1
1 0 1
0 1 0 1
*/
146
#include<stdio.h>
#include<conio.h>
int main()
{
int rows,i,j,space;
printf("Enter number of rows:");
scanf("%d",&rows);
for(i=rows;i>=1;i--)
{
for(space=0;space<rows-i;++space)
printf(" ");
for(j=i;j<=2*i-1;++j)
printf("* ");
for(j=0;j<i-1;++j)
printf("* ");
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter number of rows:5
*********
*******
*****
***
*
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,r,q=0,b,s;
//clrscr();
printf("ENTER NUMBER OF ROWS:");
scanf("%d",&r);
for(q=0;q<r;q++)
147
{
for(s=30-3*q;s>0;s--) //s=spaces
printf(" ");
for(x=0;x<=q;x++)
{
if(x==0||q==0)
b=1;
else
b=(b*(q-x+1)/x);
printf("%6d",b);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
ENTER NUMBER OF ROWS:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
*/
/* start with "1" at the top & continue placing numbers below it in a
triangular pattern.
Each number is build just sum of above two number,
(except for the edge, which are all ‘1’ and all numbers outside the
Triangle are 0's).
0 row=1
1 row=adding the two numbers above them to the left and the right
=(0+1),(1+0)
=1,1
2 row=(0+1),(1+1),(1+0)
148
=1,2,1
3 row=(0+1),(1+2),(2+1),(1+0)
=1,3,3,1
4 row=(0+1),(1+3),(3+3),(3+1),(1+0)
= 1,4,6,4,1
*/
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int i,j,row,res;
getch();
return 0;
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
}
149
/* OUTPUT:
ENTER NUMBER OF ROWS
6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
*/
ARRAYS
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0,n,a[10];
//clrscr();
printf("Enter max range \n");
scanf("%d",&n);
printf("\n Enter %d numbers",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("\n Given 1-D array elements sum=%d",sum);
getch();
return 0;
}
/* OUTPUT:
Enter max range
3
Enter 3 numbers10 20 30
Given 1-D array elements sum=60
150
*/
int main()
{
int a[]={10,20,23,54,85,6,7,8,9,10};
int i;
// clrscr();
for(i=0;i<10;i++)
printf("\n Address of a[%d] is %u and value = %d",i,&a[i],a[i]);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,sum=0,row,col,a[4][4];
float avg;
//clrscr();
printf("Enter number of rows & coulums for matrix \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values\n",row,col);
151
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
sum=sum+a[i][j];
}
}
avg=(float)sum/(row*col);
printf("\n Given 2-D array elements sum=%d and average=
%f",sum,avg);
getch();
return 0;
}
/* OUTPUT:
Enter number of rows & coulums for matrix
33
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
int array[3][3][3]=
{
{
{10, 20, 30},
{21, 15, 14},
152
{17, 18, 19}
},
{
{41, 62, 23},
{44, 55, 26},
{27, 88, 29}
},
{
{71, 32, 43},
{84, 95, 36},
{37, 38, 69}
},
};
getch();
return 0;
}
/* OUTPUT:
3D Array Elements
10 20 30
21 15 14
17 18 19
41 62 23
44 55 26
27 88 29
71 32 43
84 95 36
37 38 69
*/
153
/* PROGRAM TO PRINT EVEN & ODD NUMBERS OF A GIVEN 1-D
ARRAY */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[20];
//clrscr();
printf("Enter n value for array elements:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
printf("\n %d is even",a[i]);
else
printf("\n %d is odd",a[i]);
}
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:4
Enter 4 numbers:25 14 20 98
25 is odd
14 is even
20 is even
98 is even
*/
154
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[20],evensum=0,oddsum=0;
//clrscr();
printf("Enter n value for array elements:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
evensum+=a[i];
else
oddsum+=a[i];
}
printf("\n Sum of even numbers=%d and odd numbers=
%d",evensum,oddsum);
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:5
Enter 5 numbers:2 46 3 15 10
Sum of even numbers=58 and odd numbers=18
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[20],evencount=0,oddcount=0,pcount=0,ncount=0;
//clrscr();
printf("Enter n value for array elements:");
155
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
evencount++;
if(a[i]%2==1)
oddcount++;
if(a[i]>0)
pcount++;
if(a[i]<0)
ncount++;
}
printf("\n Total number of even numbers=%d",evencount);
printf("\n Total number of odd numbers=%d",oddcount);
printf("\n Total number of positive numbers=%d",pcount);
printf("\n Total number of negative numbers=%d",ncount);
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:4
*/
#include<stdio.h>
#include<conio.h>
int main()
156
{
int i,n,a[20],evensum=0,oddsum=0;
//clrscr();
printf("Enter n value for array elements:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(i%2==0)
evensum+=a[i];
else
oddsum+=a[i];
}
printf("\n Sum of even place numbers=%d and odd place numbers=
%d",evensum,oddsum);
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:4
Enter 4 numbers:24 56 78 12
Sum of even place numbers=102 and odd place numbers=68
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0,n,a[10];
//clrscr();
printf("Enter total number of elements \n");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
157
scanf("%d",&a[i]);
}
getch();
return 0;
}
/* OUTPUT:
Enter total number of elements
4
Enter 4 numbers:10 20 30 40
#include<stdio.h>
#include<conio.h>
int main()
{
int a[6][6],i,j,big,rows,cols;
//clrscr();
printf("Enter number of rows and columns:");
scanf("%d%d",&rows,&cols);
for(i=1;i<=rows;i++)
{
for(j=1;j<=cols;j++)
{
if(a[i][j]>big)
158
big=a[i][j];
else
big=big;
}
}
/* OUTPUT:
Enter number of rows and columns:3 3
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int temp,x[10],i,n,j;
//clrscr();
printf("\n Enter total number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
printf("\n Numbers before sort:");
for(i=0;i<n;i++)
printf("%4d",x[i]);
printf("\n Numbers after sort:");
for(i=0;i<n;i++)
159
{
for(j=0;j<n-1;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
for(j=0;j<n;j++)
printf("%4d",x[j]);
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
int min,secondmin,x[10],i,n,j;
//clrscr();
printf("\n Enter total number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
160
for(i=0;i<n;i++)
scanf("%d",&x[i]);
min=x[0];
for(i=1;i<n;i++)
{
if(min>x[i])
{
min=x[i];
j=i;
}
}
secondmin=x[n-j-1];
for(i=1;i<n;i++)
{
if(secondmin>x[i]&&j!=i)
secondmin=x[i];
}
printf("\n Second smallest number=%d",secondmin);
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[10],b[10];
//clrscr();
printf("Enter maximum range for an array:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
161
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i,n,a[10],b[10],k=1;
//clrscr();
printf("Enter max range for an array:");
scanf("%d",&n);
printf("\n Enter %d numbers for array-A:",n);
for(i=0;i<n;i++)
{
162
scanf("%d",&a[i]);
}
// COMPARING
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
{
k=0;
}
else
k=1;
}
if(k==0)
{
printf("\n Given array elements are not same");
getch();
exit(1);
}
else
printf("\n Given array elements are same");
getch();
return 0;
}
/* OUTPUT:
Enter max range for an array:3
163
Enter 3 numbers for array-A:10 20 30
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0,n,a[10];
float average;
//clrscr();
printf("Enter max range \n");
scanf("%d",&n);
printf("\n Enter %d numbers",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
average=(float)sum/n;
printf("\n Given 1-D array elements average=%f",average);
getch();
return 0;
}
/* OUTPUT:
Enter max range
4
Enter 4 numbers10 20 30 40
164
Given 1-D array elements average=25.000000
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[20],j;
//clrscr();
printf("Enter max range of an array:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]==a[i])
{
printf("\n %d is similar to %d ",a[i],a[j]);
break; //Identify importance of break here
}
}
}
getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:5
Enter 5 numbers:4 6 2 4 6
4 is similar to 4
6 is similar to 6
*/
165
// PROGRAM TO SPLITTING ARRAY AND ADDING IT AT THE END
OF ARRAY
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30],i,n,j,pos;
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
for(i=0;i<pos;i++)
{
arr[n]=arr[0];
for(j=0;j<n;j++)
{
arr[j]=arr[j+1];
}
}
getch();
return 0;
}
/* OUTPUT:
Enter size of array:5
Enter 5 elements
10 20 30 40 50
Enter the position of the element to split the array:2
The resultant array is:
166
30 40 50 10 20
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k=0,n1,n2,f=0,array1[10],array2[10],c[30];
//clrscr();
printf("\n Enter range for set-1:");
scanf("%d",&n1);
for(i=0;i<n1;i++)
{
c[k]=array1[i];
k++;
}
for(i=0;i<n2;i++)
{
f=0;
for(j=0;j<n1;j++)
{
if(array2[i]==c[j])
{
f=1;
break;
}
}
if(f==0)
{
c[k]=array2[i];
167
k++;
}
}
getch();
return 0;
}
/* OUTPUT:
Enter 4 numbers:10 20 30 40
Enter 3 numbers:10 40 50
*/
/* PROGRAM TO MOVE ALL 0'S AND 1'S ONE SIDE IN A GIVEN 1-D
ARRAY */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i,count=0,n,a[20];
//clrscr();
printf("Enter max range of an array:");
scanf("%d",&n);
printf("\n Enter %d numbers(only 0's and 1's):",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
168
for(i=0;i<n;i++)
if(a[i]!=0)
a[count++]=a[i];
while(count<n)
a[count++]=0;
getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:6
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,min,max,a[10];
//clrscr();
printf("Enter max range \n");
scanf("%d",&n);
printf("\nEnter %d numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
min=a[0];
169
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]<min)
min=a[i];
else if(a[i]>max)
max=a[i];
}
Enter 5 numbers
10 20 30 40 50
Minimum value=10
Maximum value=50
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,row,col,min,max,a[10][10];
//clrscr();
printf("Enter number of rows & coulums for matrix \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values\n",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
170
min=a[0][0];
max=a[0][0];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(a[i][j]<min)
min=a[i][j];
else if(a[i][j]>max)
max=a[i][j];
}
}
Minimum value=1
Maximum value=9
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i,j,left,right,min_sum,sum,n,a[20];
//clrscr();
printf("Enter max range of an array:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
171
for(i=0;i<n;i++)
scanf("%d",&a[i]);
left=0;
right=1;
min_sum=a[0]+a[1];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
sum=a[i]+a[j];
if(abs(min_sum)>abs(sum))
{
min_sum=sum;
left=i;
right=j;
}
}
}
printf(" The two elements whose sum is minimum are %d and %d",
a[left], a[right]);
getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:5
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30],i,n,j,index,x;
172
printf("Enter size of array:");
scanf("%d",&n);
printf("\n Enter %d elements\n",n);
for(i=0;i<n;++i)
scanf("%d",&arr[i]);
if(fun(arr, n, x))
printf("The given no %d appears more than %d times in array",x,n/2);
else
printf("The given no %d does not appear more than %d times in
array",x,n/2);
getch();
return 0;
}
/* OUTPUT:
Enter size of array:4
Enter 4 elements
1112
*/
173
/* PROGRAM TO FIND FREQUENCY OF ELEMENTS IN A GIVEN 1-D
ARRAY */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,count=0,n,a[20],val;
//clrscr();
printf("Enter max range of an array:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
val=a[i];
count=0;
for(j=0;j<n;j++)
{
if(a[j]==val)
count++;
}
printf("\n %d repeated %d times",val,count);
}
getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:5
Enter 5 numbers:1 4 2 4 1
1 repeated 2 times
4 repeated 2 times
2 repeated 1 times
4 repeated 2 times
1 repeated 2 times
*/
174
// PROGRAM TO INSERTING AN ELEMENT IN THE ARRAY
#include<stdio.h>
#include<conio.h>
int main()
{
int x[20],i,n,pos,item,k;
//clrscr();
printf("Enter array size \n",n);
scanf("%d",&n);
printf("Enter array elements \n",i);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
printf("Enter position & value to insert\n");
scanf("%d%d",&pos,&item);
for(k=n;k>=pos;k--)
x[k]=x[k-1];
x[--pos]=item;
getch();
return 0;
}
/* OUTPUT:
Enter array size 5
Enter array elements
10 20 30 40 50
Enter position & value to insert
4 35
After inserting value the array elements are:
10 20 30 35 40 50
*/
#include<stdio.h>
#include<conio.h>
int main()
{
175
int i,j,k=0,n1,n2,f=0,array1[10],array2[10],c[30];
//clrscr();
printf("\n Enter range for set-1:");
scanf("%d",&n1);
for(i=0;i<n1;i++)
{
f=0;
for(j=0;j<n2;j++)
{
if(array1[i]==array2[j])
{
f=1;
break;
}
}
if(f==1)
{
c[k]=array1[i];
k++;
}
}
if(k==0)
{
printf("\n There are no common elements");
}
else
{
printf("\n Intersection of above two sets is:\n");
for(i=0;i<k;i++)
{
printf("\t%d",c[i]);
}
}
176
getch();
return 0;
}
/* OUTPUT:
Enter 4 numbers:10 20 30 40
Enter 3 numbers:10 40 60
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i,j,max_diff,n,a[20];
//clrscr();
printf("Enter max range of an array:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max_diff=a[1]-a[0];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((a[j]-a[i])>max_diff)
max_diff=a[j]-a[i];
}
177
}
printf(" \n Maximum differnce is %d", max_diff);
getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:5
Enter 5 numbers:10 20 30 40 50
Maximum differnce is 40
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[20];
//clrscr();
printf("Enter n value for array elements \n");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("\n\t%d\t\t%d\t%d",a[i],(a[i]*a[i]),(a[i]*a[i]*a[i]));
}
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements
178
4
Enter 4 numbers:10 20 30 40
#include<stdio.h>
#include<conio.h>
int main()
{
int x[20],i,n,pos;
//clrscr();
printf("Enter array size \n",n);
scanf("%d",&n);
printf("Enter array elements \n",i);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
printf("Enter position to delete\n");
scanf("%d",&pos);
if(pos>=n+1)
printf("Deletion not possible.\n");
else
{
for(i=pos-1;i<n-1;i++)
x[i]=x[i+1];
getch();
return 0;
}
/* OUTPUT:
Enter array size
179
4
Enter array elements
10 20 30 40
Enter position to delete
3
After deleting array elements are:
10 20 40
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,j=0,k=0,a[30],evenarray[20],oddarray[20];
//clrscr();
printf("Enter n value for array elements:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(i%2==0)
evenarray[j++]=a[i];
else
oddarray[k++]=a[i];
}
180
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:5
Enter 5 numbers:10 20 30 40 50
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,n,a[20];
//clrscr();
printf("Enter max range of an array:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
n--;
}
else
j++;
181
}
}
getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:5
Enter 5 numbers:21 63 58 21 63
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,temp,n,a[30];
//clrscr();
printf("Enter n value for array elements:");
scanf("%d",&n);
printf("\n Enter %d numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(j=0;j<=n-2;j=j+2)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
182
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:5
Enter 5 numbers:10 20 30 40 50
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[3][3],b[3][3];
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d A matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
183
printf("\n");
}
/* OUTPUT:
Enter number of rows and columns
3 3
184
THE GIVEN B MATRIX IS
----------------------
1 1 1
1 1 1
1 1 1
ADDITION OF A+B IS
----------------------
2 3 4
5 6 7
8 9 10
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[3][3],b[3][3];
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d A matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
185
{
printf("%4d",a[i][j]);
}
printf("\n");
}
/* OUTPUT:
Enter number of rows and columns
3 3
186
1 2 3
4 5 6
7 8 9
ADDITION OF A+B IS
----------------------
0 1 2
3 4 5
6 7 8
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int row1,col1,row2,col2,i,j,k,a[10][10],b[6][6],c[6][6];
//clrscr();
printf("Enter A MATRIX number of rows and columns \n");
scanf("%d%d",&row1,&col1);
printf("\n Enter %dX%d A matrix values\n",row1,col1);
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
scanf("%d",&a[i][j]);
}
if(col1!=row2)
{
printf("\n Matrix multiplication is not possible");
getch();
187
exit(0);
}
else
{
printf("\nEnter %dX%d B matrix values\n",row2,col2);
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<row1;i++)
188
{
for(j=0;j<col2;j++)
{
printf("%4d",c[i][j]);
}
printf("\n");
}
}
getch();
return 0;
}
/* OUTPUT:
Enter A MATRIX number of rows and columns
33
THE MULTIPLICATION IS
----------------------------
1 2 3
4 5 6
7 8 9
*/
189
// Program to sort all rows of the matrix in ascending order
//and all columns in descending order
#include<stdio.h>
#include<conio.h>
int main()
{
int a[4][4],b[4][4],i,j,row,col,k,temp;
190
}
}
}
for(i=0;i<row;++i)
{
for(j=0;j<col;++j)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
for(i=0;i<row;++i)
{
for(j=0;j<col;++j)
{
printf("%5d",b[i][j]);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns of the matrix:3 3
Enter 3X3 matrix elements:
191
123
572
638
The given matrix is:
1 2 3
5 7 2
6 3 8
After arranging rows in ascending order
1 2 3
2 5 7
3 6 8
After arranging the columns in descending order
6 7 8
5 3 3
1 2 2
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,m,n,a[10][10];
//clrscr();
printf("Enter number of Rows & Columns of a Matrix A:");
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
printf("Enter the A[%d][%d] location value:",i,j);
scanf("%d",&a[i][j]);
}
printf("\n");
192
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i<j)
printf("");
if(i>=j)
printf("%5d",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
193
{
int i,j,a[10][10],sum,m,n;
sum=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(i>j) // Condition for lower Triangle
sum=sum+a[i][j];
}
getch();
return 0;
}
/* OUTPUT:
Enter the number of Rows:3
*/
194
/* PROGRAM TO CHECK WHETHER A GIVEN MATRIX IS LOWER
TRIANGULAR OR NOT */
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[10][10],k=0;
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
//CHECKING
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if((a[i]<a[j])&&(a[i][j]==0))
{
k++;
}
}
}
if(k==3)
printf("\n Given matrix is lower triangular matrix");
else
printf("\n Given matrix is not a lower triangular matrix");
195
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns
33
*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
int i,j,r,c,a[10][10],rsum=0,csum=0;
//clrscr();
printf("Enter the number of rows & columns of matrix\n");
scanf("%d%d",&r,&c);
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("\n Enter %d row %d column value:",i,j);
scanf("%d",&a[i][j]);
}
printf("\n\n EACH ROW and COLUMN SUM IS \n\n");
for(i=0;i<r;++i)
{
196
rsum=0;
for(j=0;j<c;++j)
{
printf("%4d",a[i][j]);
rsum+=a[i][j];
}
printf(" =%3d",rsum);
printf("\n");
}
printf("---------------\n");
for(i=0;i<r;++i)
{
csum=0;
for(j=0;j<c;++j)
{
csum+=a[j][i];
}
printf("%5d",csum);
}
getch();
getch();
}
/* OUTPUT:
Enter the number of rows & columns of matrix
33
197
EACH ROW and COLUMN SUM IS
10 20 30 = 60
40 50 60 =150
70 80 90 =240
-----------------
120 150 180
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,k,x,a[3][3];
//clrscr();
printf("Enter number of rows and columns:");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix:",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
//SORTING ROWS
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
198
for(k=(j+1);k<col;k++)
{
if(a[i][j]>a[i][k])
{
x=a[i][j];
a[i][j]=a[i][k];
a[i][k]=x;
}
}
}
}
printf("\n AFTER SORTING ROWS THE GIVEN MATRIX IS \n");
printf("-------------------------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns:3 3
*/
199
/* PROGRAM TO PRINT SUM OF EACH ROW OF A GIVEN MATRIX
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[10][10],sum=0;
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns
3 3Enter number of rows and columns
33
200
---------------------------
0 row sum=6
1 row sum=15
2 row sum=7
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[6][6],i,j,row,col,counter=0;
if(counter>((row*col)/2))
{
printf("The given matrix is sparse matrix \n");
}
else
printf("The given matrix is not a sparse matrix \n");
printf ("There are %d number of zeros",counter);
getch();
return 0;
201
}
/* OUTPUT:
Enter number of rows:3
Enter number of columns:3
Enter 3X3 matrix
123
010
004
The given matrix is not a sparse matrix
There are 4 number of zeros
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int row,col,i,j,a[10][10],trans[10][10];
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
if(row!=col)
{
printf("\n Please enter equal number of rows & columns");
getch();
exit(0);
}
else
{
202
printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",trans[i][j]);
}
printf("\n");
}
//checking
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(trans[i][j]!=a[i][j])
{
printf("Given matrix is not a symmtric");
getch();
exit(0);
}
}
}
203
}
/* OUTPUT:
Enter number of rows and columns
22
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[10][10];
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
204
{
printf("%4d",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns
33
*/
#include<stdio.h>
#include<conio.h>
205
int main()
{
int i,j,row,col,a[10][10];
//clrscr();
printf("Enter number of Rows & Columns of a Matrix:");
scanf("%d%d",&row,&col);
206
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i,j,a[10][10],sum,m,n;
sum=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(i<j) // Condition for Upper Triangle
sum=sum+a[i][j];
}
getch();
return 0;
}
/* OUTPUT:
Enter the number of Rows:3
207
Enter 3X3 matrix values
123
456
789
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[10][10],k=0;
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
//CHECKING
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
208
if((a[i]>a[j])&&(a[i][j]==0))
{
k++;
}
}
}
if(k==3)
printf("\n Given matrix is upper triangular matrix");
else
printf("\n Given matrix is not a upper triangular matrix");
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns
33
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,k,x,a[3][3];
//clrscr();
printf("Enter number of rows and columns:");
scanf("%d%d",&row,&col);
209
printf("\n Enter %dX%d matrix:",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
//SORTING ROWS
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
for(k=(i+1);k<col;k++)
{
if(a[i][j]>a[k][j])
{
x=a[i][j];
a[i][j]=a[k][j];
a[k][j]=x;
}
}
}
}
printf("\n AFTER SORTING COLUMNS THE GIVEN MATRIX IS \n");
printf("-------------------------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
getch();
return 0;
210
}
/* OUTPUT:
Enter number of rows and columns:2 2
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[10][10],sum=0;
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
211
sum=sum+a[j][i];
}
printf("%\n %d column sum=%d",i,sum);
}
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns
33
0 column sum=12
1 column sum=15
2 column sum=18
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int row,col,i,j,a[3][3],b[3][3];
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d A matrix values:",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
212
printf("\n Enter %dX%d B matrix values:",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&b[i][j]);
}
printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(a[i][j]!=b[i][j])
{
printf("A and B matrices are not same");
getch();
exit(0);
}
}
}
getch();
return 0;
213
}
/* OUTPUT:
Enter number of rows and columns
22
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][2],i,j,det;
//clrscr();
printf("enter 2x2 matrix");
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
det=a[1][1]*a[2][2]-a[2][1]*a[1][2];
printf("\n DETERMINANT OF GIVEN MATRIX=%d",det);
getch();
214
return 0;
}
/* OUTPUT:
enter 2x2 matrix
12
57
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3],i,j;
int det=0;
for(i=0;i<3;i++)
det=det+(a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3]-a[1][(i+2)%3]*a[2]
[(i+1)%3]));
printf("\n DET is: %d",det);
getch();
return 0;
}
/* OUTPUT:
215
Enter the 3X3 matrix: 1 0 0 0 1 0 0 0 1
Given matrix is
1 0 0
0 1 0
0 0 1
DET is: 1
*/
int main()
{
int a[4][4],i,j,sum=0;
//clrscr();
216
}
printf("%d",sum);
getch();
return 0;
}
/* OUTPUT:
Enter elements for 3x3 matrix
10 5 4 98 25 30 2 56 11
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i,j,row,col,a[10][10],x;
//clrscr();
printf("Enter number of Rows & Columns of a Matrix:");
scanf("%d%d",&row,&col);
if(row!=col)
{
printf("\nGiven matrix is not a square matrix");
getch();
exit(0);
}
else
{
printf("Enter the %dX%d matrix:",row,col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
217
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%3d",a[i][j]);
printf("\n");
}
for (i=0;i<row;++i)
{
x=a[i][i];
a[i][i]=a[i][row-i-1];
a[i][row-i-1]=x;
}
getch();
return 0;
}
/* OUTPUT:
Given matrix is
10 20 30
40 50 60
70 80 90
218
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j,a[10][10],k=1;
//clrscr();
printf("Enter number of rows and columns \n");
scanf("%d%d",&row,&col);
printf("\n Enter %dX%d matrix values",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
//CHECKING
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if((a[i][i]!=1)||((i!=j)&&(a[i][j]!=0)))
{
k=0;
break;
}
}
}
if(k==1)
printf("\n Given matrix is identity matrix");
else
printf("\n Given matrix is not a identity matrix");
219
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns
22
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[3][3],i,j;
float det=0;
for(i=0;i<3;i++)
det=det+(arr[0][i]*(arr[1][(i+1)%3]*arr[2][(i+2)%3]-arr[1]
[(i+2)%3]*arr[2][(i+1)%3]));
220
printf("\nInverse matrix is: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%.2f\t",((arr[(i+1)%3][(j+1)%3]*arr[(i+2)%3][(j+2)%3])-
(arr[(i+1)%3][(j+2)%3]*arr[(i+2)%3][(j+1)%3]))/det);
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:
Enter 3X3 matrix: 4 5 6 1 2 3 1 5 4
Given matrix is
4 5 6
1 2 3
1 5 4
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int n,i,j,k,arr[10][10];
// clrscr() ;
printf("Enter size of the magic square:");
scanf("%d",&n);
if(n%2==0)
{
printf("\n Magic square is not possible");
getch();
221
exit(0);
}
printf("\n The magic square for %d x %d is :\n",n,n);
j=(n+1)/2;
i=1 ;
for(k=1;k<=n*n;k++)
{
arr[i][j]=k;
if(k%n==0)
{
i=(i+1);
goto loop;
}
if(i==1)
i=n;
else
i=i-1;
if(j==n)
j=1;
else
j=j+1;
loop : ;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n\n") ;
}
getch();
return 0;
}
/* OUTPUT:
Enter size of the magic square:3
3 5 7
222
4 9 2
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int size,arr[4][4],i,j,flag=0;
int sum,sumrow,sumcol;
//clrscr();
//sum of rows
for(i=0;i<3;i++)
223
{
sumrow=0;
{
for(j=0;j<3;j++)
sumrow=sumrow+arr[i][j];
}
if(sum==sumrow)
flag=1;
else
{
flag=0;
break;
}
}
//sum of colomns
for(i=0;i<3;i++)
{
sumcol=0;
for(j=0;j<3;j++)
{
sumcol=sumcol+arr[j][i];
}
if(sum==sumcol)
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
printf("\n Given matrix is Magic square");
else
printf("\n Given matrix is not a Magic square");
getch();
return 0;
}
/* OUTPUT:
Enter any 3X3 matrix:
816
357
492
Entered matrix is :
224
8 1 6
3 5 7
4 9 2
Given matrix is Magic square
*/
FUNCTIONS
#include<stdio.h>
#include<conio.h>
void add();
int main()
{
//clrscr();
printf("\n Calling method add");
add();
printf("\n End of main" );
getch();
return 0;
}
void add()
{
int a,b;
printf("\n Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("\n Addition of %d and %d is %d",a,b,a+b);
}
225
/* OUTPUT:
Calling method add
Addition of 10 and 20 is 30
End of main
*/
#include<stdio.h>
#include<conio.h>
int add();
int main()
{
int result;
//clrscr();
printf("\n Calling method add");
result=add();
printf("\n Addition=%d",result);
printf("\n End of main" );
getch();
return 0;
}
int add()
{
int a,b;
printf("\n Enter two numbers\n");
scanf("%d%d",&a,&b);
return(a+b);
}
/* OUTPUT:
Calling method add
Enter two numbers
10 20
Addition=30
End of main
*/
226
// PROGRAM TO ADD TWO NUMBERS WITHOUT ARGUMENTS AND
WITHOUT RETURN VALUES
#include<stdio.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
//clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("\n Calling method add");
add(a,b);
printf("\n End of main" );
getch();
return 0;
}
/* OUTPUT:
Enter two numbers
10 20
#include<stdio.h>
#include<conio.h>
int add(int,int);
int main()
227
{
int a,b,result;
//clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("\n Calling method add");
result=add(a,b);
printf("\n Addition of %d and %d is %d",a,b,result);
printf("\n End of main" );
getch();
return 0;
}
/* OUTPUT:
Enter two numbers
10 20
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
//clrscr();
printf("Enter a number:");
scanf("%d",&n);
printf("\n Factorial of a number %d is %d",n,fact(n));
getch();
228
return 0;
}
//implementing function
int fact(int x)
{
int i,fact=1;
for(i=1;i<=x;i=i+1)
{
fact=fact*i;
}
return fact;
}
/* OUTPUT:
Enter a number:5
Factorial of 5 is 120
*/
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,r;
float c;
//clrscr();
printf("Enter value for n and r:");
scanf("%d%d",&n,&r);
c=fact(n)/(fact(r)*(fact(n-r)));
printf("Ncr value=%f",c);
getch();
return 0;
}
int fact(int x)
{
int i,f=1;
229
for(i=x;i>=1;i--)
f=f*i;
return(f);
}
/* OUTPUT:
Enter value for n and r:4 2
Ncr value=6.000000
*/
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,r;
float c;
//clrscr();
printf("Enter value for n and p:");
scanf("%d%d",&n,&r);
c=fact(n)/((fact(n-r)));
printf("Npr value=%f",c);
getch();
return 0;
}
int fact(int x)
{
int i,f=1;
for(i=x;i>=1;i--)
f=f*i;
return(f);
}
/* OUTPUT:
Enter value for n and p:4 2
Npr value=12.000000
*/
230
// Program to convert given binary into gray code using function
#include<stdio.h>
#include<conio.h>
#include<math.h>
int convert(int);
int main()
{
int n,gray;
getch();
return 0;
}
while(bin!=0)
{
x=bin%10;
bin=bin/10;
y=bin%10;
if((x&&!y)||(!x&&y))
{
res=res+(int)pow(10,i);
}
i++;
}
return res;
}
/* OUTPUT:
231
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int d();
//clrscr();
printf("\n Address of function d=%u",d);
d();
getch();
return 0;
}
int d()
{
puts("\n It is function");
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int *p;
int *myfun();
p=myfun();
printf("p value=%d",*p);
getch();
return 0;
}
int *myfun()
232
{
int i=20;
return(&i); //return address of i
}
/* OUTPUT:
p value=20
*/
#include<stdio.h>
#include<conio.h>
void swap(int,int);
int main()
{
int a,b;
//clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("\n BEFORE SWAPPING a=%d and b=%d",a,b);
swap(a,b);
printf("\n AFTER SWAPPING in main a=%d and b=%d",a,b);
getch();
return 0;
}
233
/* OUTPUT:
Enter two numbers
10 5
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
//clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("\n BEFORE SWAPPING a=%d and b=%d",a,b);
swap(&a,&b);
printf("\n AFTER SWAPPING in main a=%d and b=%d",a,b);
getch();
return 0;
}
/* OUTPUT:
Enter two numbers
10 5
234
BEFORE SWAPPING a=10 and b=5
AFTER SWAPPING in swap function x=5 and y=10
AFTER SWAPPING in main a=5 and b=10
*/
#include<stdio.h>
#include<conio.h>
void swap(int,int);
int main()
{
int a,b;
swap(a,b);
getch();
return 0;
}
/* OUTPUT:
*/
235
// Program to find sum of digits of a given number using
recursion. [Ex: 123=1+2+3=6]
#include<stdio.h>
#include<conio.h>
int sum(int);
int main()
{
int n,res;
//clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
res=sum(n);
printf("Sum of the digits of a number %d is %d",n,res);
getch();
return 0;
}
int rem,s=0;
int sum(int n)
{
if(n)
{
rem=n%10;
s=s+rem;
sum(n/10);
}
return(s);
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int add(int);
236
int main()
{
int x,res;
//clrscr();
printf("\n Enter a number: ");
scanf("%d",&x);
res=add(x);
printf("\n Sum=%d",res);
getch();
return 0;
}
int add(int n)
{
if(n==1)
return 1;
else
return n+add(n-1);
}
/* OUTPUT:
Enter a number: 10
Sum=55
*/
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,temp;
//clrscr();
printf("Enter a number\n");
scanf("%d",&n);
temp=fact(n);
237
printf("\n Factorial of %d is %d",n,temp);
//printf("\n Factorial of %d is %d",n,fact(n));
getch();
return 0;
}
int fact(int n)
{
int i,f=1;
if(n==1)
return 1;
else
return(n*fact(n-1));
}
/* OUTPUT:
Enter a number
5
Factorial of 5 is 120
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,big,arr[20],i;
big=arr[0];
big=large(arr,n-1,big);
printf("\nThe big number in the arr is: %d\n", big);
getch();
238
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int convert(int);
int main()
{
int n,gray;
239
getch();
return 0;
}
if(!bin)
{
return 0;
}
else
{
x=bin%10;
bin=bin/10;
y=bin%10;
if((x&&!y)||(!x&&y))
{
return(1+10*convert(bin));
}
else
{
return(10*convert(bin));
}
}
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
240
int convert(int);
int main()
{
int n,bin;
getch();
return 0;
}
int convert(int n)
{
if(n==0)
{
return 0;
}
else
{
return(n%2)+10*convert(n/2);
}
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int fib(int);
int main()
{
int n,i=0,c;
241
printf("Enter maximum number:");
scanf("%d",&n);
printf("Fibonacci series:\n");
for(c=1;c<=n;c++)
{
printf("%5d",fib(i));
i++;
}
getch();
return 0;
}
int fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fib(n-1)+fib(n-2));
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,res;
int gcd(int,int);
//clrscr();
printf("ENTER TWO INTEGER VALUES:\n");
scanf("%d%d",&x,&y);
res=gcd(x,y);
printf("THE GCD OF %d and %d IS:%d",x,y,res);
242
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
float factorial(int n)
{
float f=1;
if(n>1)
{
f=n*factorial(n-1);
}
return f;
}
int main()
{
int n,r;
float res;
//clrscr();
printf("\n Enter n,r values:");
scanf("%d %d",&n,&r);
res = factorial(n) /( factorial(n-r) * factorial(r) );
printf("\n Result NCR=%f",res);
243
getch();
return 0;
}
/* OUTPUT:
Result NCR=6.000000
*/
#include<stdio.h>
#include<conio.h>
int prime(int,int);
int main()
{
int n,res;
//clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
res=prime(n,n/2);
if(res==1)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
getch();
return 0;
}
244
return(0);
else
prime(x,i-1);
}
}
/* OUTPUT:
Enter a number: 4
4 is not a prime number
*/
#include<stdio.h>
#include<conio.h>
int rev(int);
int main()
{
int n;
//clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);
getch();
return 0;
}
int rem,s=0;
int rev(int n)
{
if(n)
{
rem=n%10;
s=s*10+rem;
rev(n/10);
}
return(s);
}
/* OUTPUT:
245
Enter a number: 123
Reverse of a number 123 is 321
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char str[50];
puts("Enter any string:");
gets(str);
/* OUTPUT:
Enter any string:
strlen is a string function
String length=27
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
246
int main()
{
char str1[20], str2[20];
copy(str1,str2,0);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char str[50],result;
247
printf("Enter any string:");
gets(str);
result=caps_test(str);
if(result==0)
printf("No capital letter is present in string: %s", str);
else
printf("The first capital letter is:%c",result);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
248
void test(char [], int);
int main()
{
char str[20];
printf("Enter any string:\n");
scanf("%s",str);
test(str,0);
getch();
return 0;
}
/* OUTPUT:
Enter any string:
LIRIL
The given string is a palindrome
*/
#include<stdio.h>
#include<conio.h>
249
int main()
{
char str[50];
puts("Enter any string:");
gets(str);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
void static_test();
int main()
{
int j;
for(j=1;j<=10;j++)
static_test();
250
getch();
return 0;
}
void static_test()
{
static int i=5;
int x=5;// or auto int x=5;
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
register int i;
//clrscr();
for(i=1;i<=10;i++)
{
printf("%3d",i);
}
251
getch();
return 0;
}
/* OUTPUT:
1 2 3 4 5 6 7 8 9 10
*/
#include<stdio.h>
#include<conio.h>
void fun1();
void fun2();
void fun3();
int main()
{
fun1();
fun2();
fun3();
getch();
return 0;
}
void fun1()
{
x=x+5; //here x is not declared
printf("\n x value in function-1 is:%d",x);
}
void fun2()
{
x=x+10; //x value can be accessed throughout the program
printf("\n x value in function-2 is:%d",x);
}
void fun3()
{
252
x=x+20;
printf("\n x value in function-3 is:%d",x);
}
/* OUTPUT:
*/
POINTERS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int a,b,ch;
int *p=&a,*q=&b;
char choice;
do
{
printf("\n Enter value for a and b:");
scanf("%d%d",&a,&b);
printf("\n Enter choice 1-Add 2-Subtract 3-Product 4-Division 5-Exit:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Sum=%d",(*p+*q));
break;
case 2:printf("\n Difference=%d",(*p-*q));
break;
case 3:printf("\n Product=%d",(*p*(*q)));
break;
case 4:printf("\n Quotient=%d",(*p/(*q)));
break;
case 5:exit(1);
253
default: printf("\n Input is wrong");
break;
}
printf("\n Do you want to continue(y/n):");
choice=getche();
}while(choice=='y'||choice=='Y');
getch();
return 0;
}
/* OUTPUT:
Sum=30
Do you want to continue(y/n):y
Enter value for a and b:30 5
Product=150
Do you want to continue(y/n):
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=10;
int *p;
int **k;
//clrscr();
p=&i;
k=&p;
printf("\n Address of i=%u",&i);
printf("\n Address of i=%u",p);
254
printf("\n Address of p=%u",&p);
printf("\n Value of i=%d",i);
printf("\n Value of p=%d",p);
printf("\n Value of i=%d",*(&i));
printf("\n Value of i=%d",*p);
/* OUTPUT:
Address of i=2293572
Address of i=2293572
Address of p=2293568
Value of i=10
Value of p=2293572
Value of i=10
Value of i=10
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],*p,i;
p=a;
//p=&a[0];
//clrscr();
printf("Enter 5 values\n");
for(i=0;i<5;i++)
{
printf("\n Enter a[%d]: ",i);
scanf("%d",p++);
// scanf("%d",p+i);
// scanf("%d",&p[i]);
// scanf("%d",&a[i]);
}
255
printf("\n Values in reverse order");
for(i=4;i>=0;i--)
printf("%4d ",*(--p));
printf("\n Values in forward order");
for(i=0;i<5;i++)
printf("%4d ",*(p+i));
getch();
return 0;
}
/*
OUTPUT:
Enter 5 values
Enter a[0]: 10
Enter a[1]: 20
Enter a[2]: 30
Enter a[3]: 40
Enter a[4]: 50
*/
#include<stdio.h>
#include<conio.h>
//void read(int(*)[],int,int);
//void display(int(*)[],int,int);
256
for(i=0;i<u;i++)
for(j=0;j<v;j++)
printf("%5d",*(*(p+i)+j));
}
int main()
{
int a[3][3],m,n;
//clrscr();
printf("Enter number of rows and columns\n");
scanf("%d%d",&m,&n);
printf("Enter %d elements\n",m*n);
read(a,m,n);
printf("The array elements are \n");
display(a,m,n);
getch();
return 0;
}
/* OUTPUT:
Enter number of rows and columns
23
Enter 6 elements
10 20 410 5 68 18
The array elements are
10 20 410 5 68 18
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,j=0;
char *s1,*s2,*newstr;
puts("Enter first string:");
gets(s1);
puts("Enter second string:");
gets(s2);
257
puts(s2);
while(*s1)
{
newstr[i++]=*s1++;
}
while(*s2)
{
newstr[i++]=*s2++;
}
newstr[i]='\0';
printf("Concatenation string is:\n");
puts(newstr);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
void copystring(char*,char*);
int main()
{
char*str1,*str2;
//clrscr();
258
copystring(str2,str1);
printf("\n Copied string=%s",str2);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
int main()
{
int i,n;
int *value;
if(value==NULL)
{
printf("Memory is not allocated");
getch();
exit(1);
259
}
for(i=0;i<n;++i)
{
printf("Enter Number %d: ",i+1);
scanf("%d",value+i);
}
for(i=1;i<n;++i)
{
if(*value<*(value+i))
*value=*(value+i);
}
getch();
return 0;
}
/* OUTOUT:
Enter total number of elements(1 to 100): 3
Enter Number 1: 10
Enter Number 2: 20
Enter Number 3: 15
Largest element = 20.00
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char str[50],*p;
int len=0;
p=str;
while(*(p+len))
len++;
260
printf("Length of entered string=%d\n",len);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int main()
{
int i,n,*a,*b,*c;
c=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
261
*(c+i)=*(a+i)+*(b+i);
}
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,*p,*q,x,y,z=0;
//clrscr();
p=&a;
q=&b;
*p=2;
*q=4;
x=*p* *q;
y=*q/ *p;
z=z+*p+*q+y;
printf("x=%d\n",x);
printf("y=%d\n",y);
printf("z=%d\n",z);
262
getch();
return 0;
}
/* OUTPUT:
x=8
y=2
z=8
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100],*p;
int i,len;
len=strlen(str);
for(i=0;i<len;i++)
{
if(*p=='\0')
break;
p++;
}
p--;
printf("Reversed String: ");
for(i=len;i>0;i--)
{
printf("%c",*p--);
}
getch();
return 0;
}
263
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],i,sum=0,*ptr,n;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ptr=a; // a=&a[0]
for(i=0;i<n;i++)
{
sum=sum+*ptr; //*p=content pointed by 'ptr'
ptr++;
}
getch();
return 0;
}
/* OUTPUT:
*/
STRINGS
264
/* PROGRAM TO FIND LENGTH OF GIVEN STRING */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,length=0;
char str[50];
//clrscr();
printf("Enter string\n");
gets(str);
printf("\n Given string=%s",str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
printf("\n\n Length of string with spaces=%d",length);
getch();
return 0;
}
/*
Enter string
How are you
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
char str[20]={'R','A','V','I','N','D','E','R','\0'};
//clrscr();
printf("Given string=%s",str);
for(i=0;str[i]!='\0';i++)
265
printf("\n\n Address of %c is %u",str[i],&str[i]);
getch();
return 0;
}
/*
OUTPUT:
Given string=RAVINDER
Address of R is 2293520
Address of A is 2293521
Address of V is 2293522
Address of I is 2293523
Address of N is 2293524
Address of D is 2293525
Address of E is 2293526
Address of R is 2293527
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
//clrscr();
getch();
return 0;
266
}
/* OUTPUT:
Enter first string in upper case
IT IS C PROGRAM
Enter second string in lower case
c is a language
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
char str1[20],str2[20];
//clrscr();
printf("Enter string: \n");
gets(str1);
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
printf("\n Original string str1=%s\n Copied string in str2=%s",str1,str2);
getch();
return 0;
}
/* OUTPUT:
Enter string:
WELCOME
267
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,l,chars=0,words=1,lines=1;
char str[50],ch;
//clrscr();
fflush(stdin);
str[i]='\0';
printf("\n Given string=%s",str);
for(i=0;str[i]!='$';i++)
{
chars++;
if(str[i]==' '||str[i]=='\n')
words++;
if(str[i]=='\n')
lines++;
}
getch();
return 0;
}
/*
Enter string with $ at the end to terminate it
268
THIS IS RAGHU
HOW ARE YOU$
Number of characters=25
Number of words=6
Number of lines=2
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,l;
char str[20];
//clrscr();
printf("Enter string\n");
gets(str);
l=strlen(str);
printf("STRING IN REVERSE ORDER IS \n");
for(i=l-1;i>=0;i--)
printf("%c",str[i]);
getch();
return 0;
}
/*
OUTPUT:
Enter string
I AM AN INDIAN
STRING IN REVERSE ORDER IS
NAIDNI NA MA I
*/
269
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
char str1[40],str2[40];
//clrscr();
printf("Enter first string: \n");
gets(str1);
getch();
return 0;
}
/* OUTPUT:
Enter first string:
Hello!
Enter second string:
It is string concat program
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,l,j,f=0;
char str1[20] ,str2[20];
270
//clrscr();
printf("Enter string1:");
gets(str1);
printf("Enter string2:");
gets(str2);
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
break;
}
}
if((str2[i]=='\0') && (str1[i]=='\0'))
printf("\n Given strings %s and %s are equal",str1,str2);
else
printf("\n Given strings %s and %s are not equal",str1,str2);
getch();
return 0;
}
/* OUTPUT:
Enter string1:C STRINGS
Enter string2:C STRINGS
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,j,x;
char str1[50],str2[50];
//clrscr();
for(i=0,j=0;(str1[i]!='\0'||str2[i]!='\0');i++,j++)
271
{
if(str1[i]!=str2[i])
{
if(str1[i]>str2[i])
x=1;
else
x=-1;
break;
}
}
if(str1[i]=='\0'&&str2[i]=='\0')
x=0;
if(x==0)
printf("\n Given strings are equal");
else if(x==1)
printf("\n string1 is greater than string2");
else if(x==-1)
printf("\n string2 is greater than string1");
getch();
return 0;
}
/*
Enter first string
RAVINDER
Enter second string
RAGHU
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,t,len;
char str[50];
272
printf("Enter string:");
gets(str);
len=strlen(str);
str[len]=' ';
getch();
return 0;
}
/* OUTPUT:
Enter string:c program is easy
Words ending with letter 'm' are:
program
*/
#include<stdio.h>
#include<conio.h>
int main()
{
273
char ch[50];
int i,nv=0,nc=0,nw=1,nb=0,nd=0,ns=0;
//clrscr();
printf("Enter any string:\n");
gets(ch);
i=0;
while(ch[i]!='\0')
{
if((ch[i]>64&&ch[i]<91)||(ch[i]>96&&ch[i]<123))
{
if(ch[i]=='a'||ch[i]=='e'||ch[i]=='i'||ch[i]=='o'||ch[i]=='u')
nv++;
else
nc++;
}
else if(ch[i]==' ')
{
nb++;
nw++;
}
else if(ch[i]>45&&ch[i]<57)
nd++;
else
ns++;
i++;
}
printf("Number of vowels=%d\n Number of consonants=%d\n Number of
digits=%d\n Number of blanks spaces=%d\n Number of words=%d\n
Number of symbols=%d\n",nv,nc,nd,nb,nw,ns);
getch();
return 0;
}
/* OUTPUT:
Enter any string:
*i am an indian*
Number of vowels=6
Number of consonants=5
Number of digits=0
Number of blanks spaces=3
Number of words=4
Number of symbols=2
*/
274
/* PROGRAM TO PRINT VOWELS & STRINGS FROM THE GIVEN STRING */
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
char str[50];
//clrscr();
printf("Enter string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
printf("\n %c is vowel",str[i]);
else
printf("\n %c is consonant",str[i]);
}
getch();
return 0;
}
/*
OUTPUT:
Enter string
ravinder
r is consonant
a is vowel
v is consonant
i is vowel
n is consonant
d is consonant
e is vowel
r is consonant
*/
#include<stdio.h>
#include<conio.h>
275
int main()
{
int i;
char str[50];
//clrscr();
printf("Enter string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
str[i]='@';
}
getch();
return 0;
}
/*
OUTPUT:
Enter string
ravinder
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,len,n,j,index=0,s=0;
char str[50];
//clrscr();
276
printf("Enter string:\n");
gets(str);
printf("\n Given string=%s",str);
n=strlen(str);
printf("\n Enter sub string length:\n");
scanf("%d", &len);
for(i=1;i<=n;i++)
{
if(index-s+1==i)
{
if(i==len)
{
for(j=index;j<n;j++)
{
for(i=s;i<index;i++)
printf("%c",str[i]);
printf("%c\n", str[j]);
}
if(s!=i)
{
s++;
index=s;
}
}
else
{
index++;
}
}
}
getch();
return 0;
}
/*
Enter string:
ABCD
Given string=ABCD
Enter sub string length:
2
The subsets are
277
AB
AC
AD
*/
// Program to insert a sub string into given main string from a given
position
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[40],str2[40],newstr[100];
int i,j,k,pos;
for(i=0;i<pos;i++)
{
newstr[i]=str1[i];
}
for(j=0;str2[j]!='\0';i++,j++)
{
newstr[i]=str2[j];
}
for(k=pos;str1[k]!='\0';i++,k++)
{
newstr[i]=str1[k];
}
newstr[i]='\0';
printf("The resultant string is:\n");
printf("%s",newstr);
getch();
return 0;
278
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[30],result[10];
int i,j,pos,n,len;
printf("Enter any string:");
gets(str);
len=strlen(str);
printf("\n Enter position to delete characters:");
scanf("%d",&pos);
printf("\n Enter number of characters to be deleted:");
scanf("%d",&n);
for(j=pos-1;j<(pos+n-1);j++)
{
str[j]='\0';
}
for(j=pos-1,i=(pos+n-1);i<len;i++,j++)
{
str[j]=str[i];
}
str[j]='\0';
279
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100], word[30];
int i,j,len1=0,len2=0,flag;
while (str[len1]!='\0')
len1++;
while (word[len2]!='\0')
len2++;
for(i=0;i<=len1-len2;i++)
{
for(j=i;j<i+len2;j++)
{
flag=1;
if(str[j]!=word[j-i])
{
280
flag=0;
break;
}
}
if(flag==1)
break;
}
if(flag==1)
puts("Sub string is found");
else
puts("Sub string is not found");
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,len;
281
gets(str);
n=len;
printf("The subsets are:\n");
for(i=1;i<=n;i++)
fun_subset(0,0,i);
getch();
return 0;
}
else
{
for(j=index;j<n;j++)
{
for(i=start;i<index;i++)
printf("%c",str[i]);
printf("%c\n",str[j]);
}
if(start!=n-y)
fun_subset(start+1,start+1,y); //recursion
}
}
else
{
fun_subset(start,index+1,y); //recursion
}
}
/* OUTPUT:
282
J
A
V
A
JA
JV
JA
AV
AA
VA
JAV
JAA
AVA
JAVA
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,l,x=0;
char str[20];
//clrscr();
printf("Enter string\n");
gets(str);
l=strlen(str);
printf("STRING LENGTH=%d",l);
for(i=0,l=strlen(str)-1;l>=0;i++,l--)
{
printf("\n Original char=%c\t Reverse char=%c",str[l],str[i]);
if(str[i]!=str[l])
{
printf("\n %s is not a polyndrome",str);
x++;
break;
}
}
283
if(x==0)
printf("\n %s is polyndrome",str);
getch();
return 0;
}
/*
OUTPUT:
Enter string
MADAM
STRING LENGTH=5
Original char=M Reverse char=M
Original char=A Reverse char=A
Original char=D Reverse char=D
Original char=A Reverse char=A
Original char=M Reverse char=M
MADAM is polyndrome
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char str[100],big[50],small[50];
printf("Enter string:");
gets(str);
fun(str,big,small);
getch();
return 0;
}
284
{
char *p,tempmax[30],tempmin[30];
int i,max=0,min,pos=0;
if(isspace(arr[strlen(arr)-1]))
{
}
else
{
arr[strlen(arr)]=' ';
arr[strlen(arr)]='\0';
}
min=strlen(arr);
for(i=0;i<strlen(arr);i++)
{
p=&arr[i];
if(*p!=' ')
{
tempmax[pos]=*p;
tempmin[pos]=*p;
pos++;
}
else
{
tempmax[pos]='\0';
tempmin[pos]='\0';
if(strlen(tempmax)>max)
{
copy_fun(tempmax,str1);
max=strlen(tempmax);
}
if(strlen(tempmin)<min)
{
copy_fun(tempmin,str2);
min=strlen(tempmin);
}
pos=0;
}
}
}
285
{
*t=*s;
*s++;
*t++;
}
*t='\0';
}
/* OUTPUT:
*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
int t=1,len,i;
char str[200];
//clrscr();
printf("\n Enter the string:");
gets(str);
len=strlen(str);
for(i=1;i<len;i++)
{
int j;
for(j=0;j<t;j++)
{
if(str[i]==str[j])
break;
}
if(j==t)
{
str[t]=str[i];
++t;
286
}
}
str[t]=0;
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char str1[100],str2[20][20];
int i,j=0,k=0,n,m;
for(i=0;str1[i]!= '\0';i++)
{
if(str1[i]==' ')
{
str2[k][j]='\0';
k++;
j=0;
}
else
{
str2[k][j]=str1[i];
j++;
}
}
287
str2[k][j]='\0';
for(i=0;i<=k;i++)
{
for(j=i+1;j<=k;j++)
{
if(strcmp(str2[i],str2[j])==0)
{
for(m=j;m<=k;m++)
strcpy(str2[m],str2[m+1]);
k--;
}
}
}
printf("\n String after removing repeted words is:\n");
for(n=0;n<=k;n++)
{
printf("%s ", str2[n]);
}
getch();
return 0;
}
/* OUTPUT:
Enter any string:
C is a language. C is rich in operators
*/
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100],newstr[100];
int i=0,j=0;
printf("Enter string:\n");
gets(str);
288
while(str[i]!='\0')
{
if(!(str[i]==' '))
{
newstr[j]=str[i];
j++;
}
i++;
}
newstr[j]='\0';
printf("After removing blank spaces new string is:\n %s", newstr);
getch();
return 0;
}
/* OUTPUT:
Enter string:
IT IS C PROGRAMMING EXAMPLE
After removing blank spaces new string is:
ITISCPROGRAMMINGEXAMPLE
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j=0,k=0;
char str[50],searchword[20],newstr[10][20];
printf("Enter string:");
scanf("%[^\n]s",str);
for(i=0;str[i]!='\0';i++)
{
289
if(str[i]==' ')
{
newstr[k][j]='\0';
k++;
j=0;
}
else
{
newstr[k][j]=str[i];
j++;
}
}
newstr[k][j]='\0';
printf("Enter searching word:");
scanf("%s",searchword);
for(i=0;i<k+1;i++)
{
if(strcmp(newstr[i],searchword)==0)
{
for(j=i;j<k+1;j++)
strcpy(newstr[j],newstr[j+1]);
k--;
}
}
for(i=0;i<k+1;i++)
{
printf("%s",newstr[i]);
}
getch();
return 0;
}
/* OUTPUT:
Enter string:IT IS C. IT HAS 32 KEYWORDS
Enter searching word:IT
IS C. HAS 32 KEYWORDS
*/
290
// Program to find the number of occurences of each alphabet in a given
string
#include<stdio.h>
#include<conio.h>
int main()
{
char str[20],ch;
int i,c,j;
printf("Enter any string:");
gets(str);
i=0;
while(str[i]!='\0')
{
c=0;
ch=str[i];
j=i;
while(str[j]!='\0')
{
if(ch==str[j])
c++;
j++;
}
getch();
return 0;
}
/* OUTPUT:
I repeated times=2
T repeated times=1
repeated times=2
I repeated times=1
S repeated times=1
repeated times=1
291
C repeated times=1
*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char input[100],word[10],repword[10],str[10][10];
int i=0,j=0,k=0,x,len;
// clrscr();
for(k=0;k<len;k++)
{
if(input[k]!=' ')
{
str[i][j]=input[k];
j++;
}
else
{
str[i][j]='\0';
j=0;
i++;
}
}
str[i][j]='\0';
x=i;
292
for (i=0;i<=x;i++)
{
if(strcmp(str[i],word)==0)
strcpy(str[i],repword);
printf("%s ",str[i]);
}
getch();
return 0;
}
/* OUTPUT:
Enter any string:
Ravi how are you?
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j=0,k=0,x,len;
char str[50],temp,newstr[10][20];
printf("Enter string:");
scanf("%[^\n]s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
293
{
newstr[k][j]='\0';
k++;
j=0;
}
else
{
newstr[k][j]=str[i];
j++;
}
}
newstr[k][j]='\0';
for(i=0;i<=k;i++)
{
len=strlen(newstr[i]);
for(j=0,x=len-1;j<x;j++,x--)
{
temp=newstr[i][j];
newstr[i][j]=newstr[i][x];
newstr[i][x]=temp;
}
}
for(i=0;i<=k;i++)
{
printf("%s ",newstr[i]);
}
getch();
return 0;
}
/* OUTPUT:
Enter string:THIS IS RAVINDER BATHINI
SIHT SI REDNIVAR INIHTAB
*/
#include<stdio.h>
294
#include<conio.h>
int main()
{
int i,j;
char str[50],t;
//clrscr();
printf("Enter string:");
gets(str);
printf("\n Given string=%s",str);
for(i=0;str[i]!='\0';i++)
{
for(j=i+1;str[j]!='\0';j++)
{
if(str[i]>str[j])
{
t=str[i];
str[i]=str[j];
str[j]=t;
}
}
}
printf("\n The alphabet string is: %s",str);
getch();
return 0;
}
/*
Enter string:
PROGRAM
Given string=PROGRAM
The alphabet string is: AGMOPRR
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
295
{
int i,j,n;
char str[30][30],temp[20];
printf("Enter number of words to be sorted:");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string:\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();
return 0;
}
/*OUTPUT:
Enter number of words to be sorted:4
Slate
Apple
Book
Pen
Apple
Book
Pen
Slate
*/
#include<stdio.h>
#include<conio.h>
296
#include<string.h>
#include<ctype.h>
int main()
{
char str[100],big[50],small[50];
printf("Enter string:");
gets(str);
fun(str,big,small);
getch();
return 0;
}
if(isspace(arr[strlen(arr)-1]))
{
}
else
{
arr[strlen(arr)]=' ';
arr[strlen(arr)]='\0';
}
min=strlen(arr);
for(i=0;i<strlen(arr);i++)
{
p=&arr[i];
if(*p!=' ')
{
tempmax[pos]=*p;
tempmin[pos]=*p;
pos++;
}
297
else
{
tempmax[pos]='\0';
tempmin[pos]='\0';
if(strlen(tempmax)>max)
{
copy_fun(tempmax,str1);
max=strlen(tempmax);
}
if(strlen(tempmin)<min)
{
copy_fun(tempmin,str2);
min=strlen(tempmin);
}
pos=0;
}
}
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
298
int main()
{
char str[100], word[30];
int i=0,c=0;
printf("\n Enter a string:");
gets(str);
while(str[i]!='\0')
{
if(str[i]==word[c]&&word[c]!='\0'&&str[i]!=' ')
c++;
else
c=0;
i++;
}
if(c==strlen(word))
printf("\nWord found");
else
printf("\nWord not found");
getch();
return 0;
}
/* OUTPUT:
Word found
*/
#include<stdio.h>
#include<conio.h>
int main()
{
299
char str[100];
int k,count=0,n=0,i,s,space;
while(str[count]!='\0')
{
count++;
}
for(k=0;k<=count-2;k++)
{
i=(str[k]=='i'||str[k]=='I');
s=(str[k+1]=='s'||str[k+1]=='S');
space=(str[k+2]==' '||str[k+2]=='\0');
if((i&&s&&space)==1)
n++;
}
printf("Frequency of the word 'is'= %d\n", n);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i,j,count=0,pos,k=0;
300
char str1[100],str2[50],str3[100];
char *ptr1,*ptr2,*ptr3;
ptr1=str1;
ptr3=str3;
for(i=0,j=0;*ptr1!='\0';ptr1++,i++,j++,ptr3++)
{
str3[j]=str1[i];
if(*ptr1==' '&&k!=1)
++count;
if(k!=1&&count==pos-1)
{
k=1;
for(ptr2=str2;*ptr2!='\0';ptr2++)
{
str3[++j]=*ptr2;
ptr3++;
}
str3[++j]=' ';
ptr3++;
}
}
str3[j]='\0';
printf("\n The string after modification is:\n%s",str3);
getch();
return 0;
}
/* OUTPUT:
301
Enter the position you like to insert:4
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
char str[50];
//clrscr();
printf("Enter string\n");
gets(str);
printf("\n Given string=%s",str);
getch();
return 0;
}
/*
Enter string
RAVINDER
Given string=RAVINDER
Encoded string:UDYLQGHU
*/
302
#include<stdio.h>
#include<conio.h>
int main()
{
char str[40];
int i,nd=0,total=0;
for(i=0;str[i]!='\0';i++)
{
if((str[i]>='0')&&(str[i]<='9'))
{
nd+=1;
total+=(str[i]-'0');
}
}
printf("\n Number of Digits in the string=%d\n",nd);
printf("\n Total of all digits=%d\n",total);
getch();
return 0;
}
/* OUTPUT:
Enter the string containing both digits and alphabet:
Ra12vi7
*/
#include<stdio.h>
#include<conio.h>
303
#include <ctype.h>
int main()
{
char ch;
if(isalpha(ch))
printf("\n Given character %c is an alphabet",ch);
if(isalnum(ch))
printf("\n Given character %c is a alpha-numeric",ch);
if(isupper(ch))
printf("\n Given character %c is a uppercase letter",ch);
if(islower(ch))
printf("\n Given character %c is a lowercase letter",ch);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0;
char str[40];
//clrscr();
printf("Enter string:");
gets(str);
for(i=0;str[i]!='\0';i++)
304
printf("\n ASCII value of %c is %d",str[i],str[i]);
getch();
return 0;
}
/* OUTPUT:
Enter string:Ravinder
ASCII value of R is 82
ASCII value of a is 97
ASCII value of v is 118
ASCII value of i is 105
ASCII value of n is 110
ASCII value of d is 100
ASCII value of e is 101
ASCII value of r is 114
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
char str[100];
int i=0;
while(str[i]!='\0')
{
if(isupper(str[i]))
{
printf("Firt capital letter %c is found at location %d",str[i],i+1);
getch();
exit(0);
}
i++;
}
305
printf("\n There is no capital letter in the given string");
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0;
char str[40];
//clrscr();
printf("Enter string:");
gets(str);
printf("\n Given string =%s",str);
//conversion
while(str[i]!='\0')
{
if((str[i]>='A'&&str[i]<='Z'))
str[i]=str[i]+32;
else if((str[i]>='a'&&str[i]<='z'))
str[i]=str[i]-32;
i++;
}
str[i]='\0';
printf("\n After conversion each letter to opposite case string is:%s",str);
getch();
return 0;
}
306
/* OUTPUT:
Enter string:It is C Program
*/
int main()
{
int i=0;
char str[40];
//clrscr();
printf("Enter string in upper case:\n");
gets(str);
printf("Given string =%s",str);
while(str[i]!='\0')
{
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]+32;
i++;
}
str[i]='\0';
printf("\n Now in lowercase=%s",str);
//lower to upper
printf("\n\n Enter string in lower case:\n");
gets(str);
printf("Given string =%s",str);
i=0;
while(str[i]!='\0')
{
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
i++;
}
str[i]='\0';
printf("\n Now in uppercase=%s",str);
307
getch();
return 0;
}
/* OUTPUT:
Enter string in upper case:
SINGLE LINE COMMENTS IN C SPECIFIED USING //
Given string =SINGLE LINE COMMENTS IN C SPECIFIED USING //
Now in lowercase=single line comments in c specified using //
/*Two words are anagrams if the letters from one word can be
rearranged to form the other word. */
int test(char[],char[]);
#include<stdio.h>
#include<conio.h>
int main()
{
char str1[40], str2[40];
int temp;
temp=test(str1,str2);
if(temp==1)
printf("\"%s\" and \"%s\" are anagrams.\n", str1, str2);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", str1, str2);
getch();
308
return 0;
}
while(a[k]!='\0')
{
first[a[k]-'a']++;
k++;
}
k=0;
while(b[k]!='\0')
{
second[b[k]-'a']++;
k++;
}
for(k=0;k<26;k++)
{
if(first[k]!=second[k])
return 0;
}
return 1;
}
/* OUTPUT:
*/
STRUCTURES,UNIONS
#include<stdio.h>
#include<conio.h>
struct stu
{
309
int rno,m1,m2,m3,tot;
char name[10];
float avg;
}s;
//struct stu s1;
int main()
{
int i,j;
//clrscr();
printf("\n Enter student name \n");
//gets(s.name);
scanf("%s",s.name);
printf("\n Enter student roll no,3 subject marks \n");
scanf("%d%d%d%d",&s.rno,&s.m1,&s.m2,&s.m3);
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3.0;
printf("\n Student name=%s \n roll no=%d",i,s.name,s.rno);
printf("\n Student total marks=%d \n average=%f",i,s.tot,s.avg);
getch();
return 0;
}
/* OOUTPUT:
Student name=RAGHU
roll no=1
Student total marks=287
average=95.666664
*/
#include<stdio.h>
#include<conio.h>
struct copy
{
int x,y;
310
};
//}s1,s2;
struct copy s1;
int main()
{
struct copy s2;
//clrscr();
printf("\n Enter two values \n");
scanf("%d%d",&s1.x,&s1.y);
printf("\n Sum of x and y=%d",s1.x+s1.y);
s2=s1;
printf("\n Multiplication of x and y=%d",s2.x*s2.y);
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
//clrscr();
for(i=1;i<=2;i++)
{
printf("\n Enter %d student name \n",i);
//gets(s[i].name);
311
scanf("%s",s[i].name);
printf("\n Enter %d student roll no,3 subject marks \n",i);
scanf("%d%d%d%d",&s[i].rno,&s[i].m1,&s[i].m2,&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3.0;
printf("\n %d student name=%s \n roll no=%d",i,s[i].name,s[i].rno);
printf("\n %d student total marks=%d \n average=
%f",i,s[i].tot,s[i].avg);
// we can copy one structer to another like s2=s1;
}
getch();
return 0;
}
/* OOUTPUT:
1 student name=RAGHU
roll no=1
1 student total marks=287
average=95.666664
Enter 2 student name
VIJAY
2 student name=VIJAY
roll no=2
2 student total marks=179
average=59.666668
*/
#include<stdio.h>
#include<conio.h>
struct add
{
unsigned int a:2;
unsigned int b:4;
312
unsigned int c:6;
}c;
int main()
{
//clrscr();
c.a=2;
c.b=4;
c.c=c.a+c.b;
printf("\n Size of structure=%d bytes",sizeof(c));
printf(" \n c value %d",c.c);
getch();
return 0;
}
/* OUTPUT:
Size of structure=2 bytes
c value 6
*/
#include "stdio.h"
#include "conio.h"
struct adress
{
char ct[10];
char dist[10],state[5];
};
struct emp
{
char name[10];
int age,sal;
struct adress a; //nested structure
};
main()
{
struct emp e[2];
int i;
//clrscr();
for(i=0;i<=1;i++)
{
313
printf("Enter %d Employee's Name,Age,salary", i+1);
scanf("%s%d%d",e[i].name,&e[i].age,&e[i].sal);
printf("\nEnter city, district & state");
scanf("%s%s%s",e[i].a.ct,e[i].a.dist,e[i].a.state);
}
for(i=0;i<=1;i++)
{
printf("\n%d Employee's Name %s",i+1,e[i].name);
printf("\n%d Employee's Age %d ",i+1,e[i].age);
printf("\n%d Employee's Salary %d",i+1,e[i].sal);
printf("\n%d Employee's City %s ",i+1,e[i].a.ct);
printf("\n%d Employee's District %s",i+1,e[i].a.dist);
printf("\n%d Employee's State %s",i+1,e[i].a.state);
}
getch();
return 0;
}
/* OUTPUT:
314
// Program to sending an entire structure as a parameter to a
function
#include<stdio.h>
#include<conio.h>
struct s
{
int a;
float b;
};
int main()
{
struct s s1={10,7.4};
fun(s1);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
315
struct stu
{
int rno,m1,m2,m3;
char name[40];
};
struct stu *s1;
int main()
{
int i,j;
//clrscr();
printf("\n Enter student name \n");
gets(s1->name);
printf("\n Enter roll no,3 subject marks \n");
scanf("%d%d%d%d",&s1->rno,&s1->m1,&s1->m2,&s1->m3);
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
316
void fun(struct num *);
struct num
{
int x;
float y;
}n1;
int main()
{
struct num n2={100,3.3};
//clrscr();
printf("\n sum=%f",n2.x+n2.y);
fun(&n2);
getch();
return 0;
}
/* OUTPUT:
sum=103.300000
mul=329.999995
*/
#include<stdio.h>
#include<conio.h>
struct example
{
int a;
float b;
};
int main()
{
struct example s={10,45.62};
317
void myfun(struct example *);
printf("\n The structure members before modification are :");
printf("a=%d b=%f",s.a,s.b);
myfun(&s);
printf(" \n The structure members after modification are :");
printf("a=%d b=%f",s.a,s.b);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
union add
{
int x;
float y;
}u1;
int main()
{
//clrscr();
printf("Enter x value:");
scanf("%d",&u1.x);
318
printf("\n Given x value=%d",u1.x);
getch();
return 0;
}
/* OUTPUT:
Enter x value:10
Given x value=10
Enter y value:20.45
Given y value=20.450000
*/
#include<stdio.h>
#include<conio.h>
struct x
{
int a;
float b;
};
union y
{
int a;
float b;
};
int main()
319
{
struct x x1;
union y y1;
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
{
struct stu
{
char name[30];
char branch[10];
int rollno;
};
union details
{
struct stu st;
};
union details det;
//clrscr();
printf("Enter name: ");
scanf("%s", det.st.name);
320
printf("\nEnter rollno: ");
scanf("%d", &det.st.rollno);
//flushall();
getch();
return 0;
}
/* OUTPUT:
Enter rollno: 01
*/
#include<stdio.h>
#include<conio.h>
int main()
{
typedef char* COLOR;
typedef unsigned long int WEIGHT;
321
COLOR board,door;
WEIGHT he,she;
board="black";
door="yellow";
printf("board color=%s and door color=%s",board,door);
he=60;
she=120;
printf("\n Weight of he=%d and she=%d",he,she);
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
enum month{JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
for(i=JAN;i<=DEC;i++)
printf("%5d",i);
getch();
return 0;
}
/* OUTPUT:
322
Example on enum data type
0 1 2 3 4 5 6 7 8 9 10
*/
#include<stdio.h>
#include<conio.h>
enum gen{MALE,FEMALE};
int main()
{
int gender;
if(gender==MALE)
{
printf("Gender is male");
}
if(gender==FEMALE)
{
printf("Gender is female");
}
getch();
return 0;
}
/* OUTPUT:
*/
FILES
// Program to read data from file
323
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch, fname[20];
FILE *fp;
if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
324
{
char str[100], fname[20];
FILE *fp;
if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int main()
325
{
FILE *fp;
char fname[20];
int size=0;
if(fp==NULL)
printf("\nFile unable to open");
else
printf("\nFile opened successfully");
fseek(fp,0,2); /* file pointer goes to end of file */
size=ftell(fp);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include <sys\stat.h>
int main()
{
struct stat s;
char fname[20];
printf("Enter file name:");
scanf("%s",fname);
stat(fname,&s);
326
if(s.st_mode&S_IFDIR)
printf("It is directory \n");
if(s.st_mode&S_IFREG)
printf("It is regular file");
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stu
{
char name[20];
char branch[10];
int rno;
};
int main()
{
FILE *fp;
char ch='y';
fp=fopen("student.txt","w");
if(fp==NULL)
{
printf("Unable to create file");
getch();
exit(1);
}
struct stu s;
327
do
{
printf("\n Enter student name:");
scanf("%s",s.name);
printf("\n Enter student branch:");
scanf("%s",s.branch);
printf("\n Enter student roll number:");
scanf("%d",&s.rno);
fprintf(fp,"%s\t\t%s\t%d",s.name,s.branch,s.rno);
fclose(fp);
fp=fopen("student.txt","r");
if(fp==NULL)
{
printf("Unable to open file");
getch();
exit(1);
}
// RETRIEVING DATA
printf("\n STUDENT NAME\t BRANCH \t ROLL NUMBER");
printf("\n -----------------------------------------\n");
while(fscanf(fp,"%s%s%d",s.name,s.branch,&s.rno)!=EOF)
printf("\n %s\t%s\t%d",s.name,s.branch,s.rno);
fclose(fp);
getch();
return 0;
}
/* OUTPUT:
do u want to continue(y/n):y
328
Enter student name:DURGA
Enter student branch:ECE
Enter student roll number:02
do u want to continue(y/n):y
do u want to continue(y/n):n
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch, fname[20];
FILE *fp1,*fp2,*fp3;
fp1=fopen(fname,"r");
fp2=fopen("VOWEL.TXT","w");
fp3=fopen("CONSONANT.TXT","w");
329
}
rewind(fp1);
fclose(fp1);
fclose(fp2);
fclose(fp3);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
330
int main()
{
char fname[20],newname[20];
FILE *fp;
int status;
fp=fopen(fname,"r");
if(fp==NULL)
{
perror("File does not exist\n");
exit(1);
}
fclose(fp);
status=rename(fname,newname);
if(status==0)
printf("\n File is renamed");
else
printf("\n Unable to rename file");
getch();
return 0;
}
/* OUTPUT:
File is renamed
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
331
int main()
{
FILE *fp;
char ch,fname[20];
int lineno=1;
if(fp==NULL)
{
puts("unable to open the file:");
getch();
exit(1);
}
printf("\n %d:",lineno);
while((ch=getc(fp))!=EOF)
{
if(ch=='\n')
{
lineno++;
printf("\n %d:",lineno);
}
else
{
printf("%c",ch);
}
}
fclose(fp);
/* OUTPUT:
332
2:RAGHU
3:RAVINDER
4:DURGA PRASAD
5:APPLE
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char fname[20];
FILE *fp;
int status;
if(fp==NULL)
{
perror("File does not exist\n");
exit(1);
}
fclose(fp);
status=remove(fname);
if(status==0)
printf("\n File is deleted");
else
printf("\n Unable to delete file");
getch();
return 0;
}
/* OUTPUT:
333
File is deleted
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch, sfname[20],dfname[20];
FILE *fp1,*fp2;
if(fp1==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}
if(fp2==NULL)
{
perror("Error while creating the file.\n");
exit(1);
}
while((ch=fgetc(fp1))!=EOF)
fprintf(fp2,"%c",ch);
fclose(fp1);
fclose(fp2);
334
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2;
int ch1,ch2;
char fname1[40],fname2[40];
fp1=fopen(fname1,"r" );
fp2=fopen(fname2,"r");
if(fp1==NULL||fp2==NULL)
{
printf("Cannot open the spefied file(s)");
exit(1);
}
else
{
335
ch1=getc(fp1);
ch2=getc(fp2);
while((ch1!=EOF)&&(ch2!=EOF)&&(ch1==ch2))
{
ch1=getc(fp1);
ch2=getc(fp2) ;
}
if(ch1==ch2)
printf("Data in the two is same");
else if(ch1!=ch2)
printf("Data in the two is different");
fclose(fp1);
fclose(fp2);
}
getch();
return 0;
}
/* OUTPUT:
Enter name of first file:file1.txt
Enter name of second file:file2.txt
Data in the two is different
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp1, *fp2;
char fname[20],ch;
int del_line,temp=1;
336
scanf("%s",fname);
if(fp1==NULL)
{
printf("\n Unable to open file");
getch();
exit(1);
}
fp1=fopen(fname,"r");
ch=getc(fp1);
rewind(fp1);
printf(" \n Enter line number of the line to be deleted:");
scanf("%d",&del_line);
fp2=fopen("newfile.txt","w");
ch=getc(fp1);
while(ch!=EOF)
{
ch=getc(fp1);
if(ch=='\n')
temp++;
fp1=fopen(fname,"r");
337
ch=getc(fp1);
while(ch!=EOF)
{
printf("%c",ch);
ch=getc(fp1);
}
fclose(fp1);
getch();
return 0;
}
/* OUTPUT:
Enter file name:x.txt
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch, fname[20];
FILE *fp;
int line=1;
338
printf("Enter the name of file to read from it:\n");
gets(fname);
if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include <dirent.h>
339
int main()
{
DIR *d;
struct dirent *dir;
d=opendir("c:\\mydir\\");
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char ch,file1[20],file2[20],newfile[20];
340
printf("Enter name of first file:");
gets(file1);
fp1=fopen(file1,"r");
fp2=fopen(file2,"r");
if(fp1==NULL||fp2==NULL)
{
perror("Unable to read files");
printf("Press any key to exit...\n");
getch();
exit(1);
}
fp3=fopen(newfile,"w");
if(fp3==NULL)
{
perror("unable to create file ");
printf("Press any key to exit...\n");
exit(1);
}
while((ch=fgetc(fp1))!=EOF)
fputc(ch,fp3);
while(( ch=fgetc(fp2))!=EOF)
fputc(ch,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
getch();
return 0;
}
341
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct emp
{
char name[20];
char dept[10];
int id;
};
int main()
{
FILE *fp;
int i,n;
char ch='y';
fp=fopen("employee.txt","w");
if(fp==NULL)
{
printf("Unable to create file");
getch();
exit(1);
}
342
for(i=1;i<=n;i++)
{
printf("\n Enter %d-employee name:",i);
scanf("%s",e[i].name);
printf("\n Enter %d-employee department:",i);
scanf("%s",e[i].dept);
printf("\n Enter %d-employee id:",i);
scanf("%d",&e[i].id);
fp=fopen("employee.txt","r");
if(fp==NULL)
{
printf("Unable to open file");
getch();
exit(1);
}
// RETRIEVING DATA
printf("\n Employee NAME\t DEPARTMENT \t ID");
printf("\n -----------------------------------------\n");
for(i=1;i<=n;i++)
{
fscanf(fp,"%s%s%d",e[i].name,e[i].dept,&e[i].id);
printf("\n %s\t\t%s\t%d",e[i].name,e[i].dept,e[i].id);
}
fclose(fp);
getch();
return 0;
}
/* OUTPUT:
343
Enter 1-employee id:10
RAVI HR 10
VIJAY FINANCE 11
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch,str[100],fname[20];
FILE *fp;
if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}
fprintf(fp,"%s",str);
344
printf("\n Data inserted in file\n");
printf("--------------------------------\n");
printf("File content after appending is:\n");
while((ch=fgetc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char ch, sfname[20],dfname[20];
FILE *fp1,*fp2;
345
printf("Enter source file name to read from it:");
gets(sfname);
fp1=fopen(sfname,"r"); // read mode
if(fp1==NULL)
{
perror("\n Error while opening the file.\n");
exit(1);
}
if(fp2==NULL)
{
perror("Error while creating the file.\n");
exit(1);
}
// APPEND PROCESS
while((ch=fgetc(fp1))!=EOF)
fprintf(fp2,"%c",ch);
rewind(fp2);
fclose(fp1);
fclose(fp2);
346
getch();
return 0;
}
/* OUTPUT:
---------------------------------
it is second file
it is first file
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int n;
char c;
fp=fopen("FILE1.TXT", "w");
if(fp==NULL)
{
347
printf("Unable to create file");
getch();
exit(1);
}
fp = fopen("FILE1.TXT","r");
if(fp==NULL)
{
printf("Unable to open file");
getch();
exit(1);
}
n=0L;
while(feof(fp)==0)
{
fseek(fp,n,SEEK_SET);
printf("\n Position of %c is %d\n", getc(fp),ftell(fp));
n=n+1L;
}
putchar('\n');
getch();
return 0;
}
/* OUTPUT:
Enter data and end with $ symbol:
IT IS C$
348
Total number of characters in a file=7
Position of I is 0
Position of T is 1
Position of is 2
Position of I is 3
Position of S is 4
Position of is 5
Position of C is 6
Position of is 7
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2;
int ch1,ch2;
char fname1[40],fname2[40];
fp1=fopen(fname1,"r" );
fp2=fopen(fname2,"r");
349
if(fp1==NULL||fp2==NULL)
{
printf("Cannot open the spefied file(s)");
exit(1);
}
else
{
ch1=getc(fp1);
ch2=getc(fp2);
while((ch1!=EOF)&&(ch2!=EOF)&&(ch1==ch2))
{
ch1=getc(fp1);
ch2=getc(fp2) ;
}
if(ch1==ch2)
printf("Data in the two is same");
else if(ch1!=ch2)
printf("Data in the two is different");
fclose(fp1);
fclose(fp2);
}
getch();
return 0;
}
/* OUTPUT:
Enter name of first file:file1.txt
Enter name of second file:file2.txt
Data in the two is different
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
350
{
int i;
int sum=0;
printf("Number of arguments=%d",argc);
for(i=0;i<argc;i++)
{
printf("\n argv[%d] is %s",i,argv[i]);
sum=sum+atoi(argv[i]);
}
printf("\n Sum of command line arguments=%d",sum);
getch();
return 0;
}
/* OUTPUT:
Goto command-prompt where you set the output directory
To set output directory in Turboc2/c3 goto Options menu---> select
directories option-->type path for output directory
Now type
c:\tc\bin> FILE-NAME.EXE 10 20 30 40
Number of arguments=5
argv[0] is C:\TC\BIN\CMD.EXE
argv[1] is 10
argv[2] is 20
argv[3] is 30
argv[4] is 40
Sum of command line arguments=100
*/
SEARCHING
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int x[10],key,i,n;
//clrscr();
printf("Enter how many elements do you want to enter");
scanf("%d",&n);
printf("Enter %d elements",n);
351
for(i=0;i<=n-1;i++)
{
scanf("%d",&x[i]);
}
getch();
return 0;
}
/* OUTPUT:
Second run:
enter how many elements do you want to enter 3
enter the elements
4
12
52
*/
#include<stdio.h>
352
#include<conio.h>
#include<stdlib.h>
int main()
{
int x[10],i,key,low,high,n,midpos;
//clrscr();
printf("\n Enter the total number of elements");
scanf("%d",&n);
printf("Enter the elements in ascending order");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
while(low<=high)
{
midpos=(low+high)/2;
if(key==x[midpos])
{
printf("%d Element found at location %d",key,midpos+1);
//break;
getch();
exit(0);
}
else if(key<x[midpos])
high=midpos-1;
else
low=midpos+1;
}
printf("%d is not found",key);
//printf("\n END OF PROGRAM");
getch();
return 0;
}
/* OUTPUT:
353
89
SORTING
#include<stdio.h>
#include<conio.h>
int main()
{
int temp,x[10],i,n,j;
//clrscr();
printf("\n Enter the total number of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
printf("\n Numbers before sort");
for(i=0;i<n;i++)
printf("%4d",x[i]);
printf("\n Numbers after sort");
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
for(j=0;j<n;j++)
printf("%4d",x[j]);
getch();
return 0;
}
354
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
int main()
{
void inssort(int [],int );
int a[10],i,n;
//clrscr();
printf("\n Enter number of Elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
// CALLING INSERTION SORT
inssort(a,n);
getch();
return 0;
355
}
if(a[i]<a[j])
{
temp=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[j]=temp;
break;
}
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 500
int main()
{
int i;
//clrscr();
printf("\nHow many arr you want to sort: ");
356
scanf("%d",&n);
printf("\nEnter the values one by one: ");
for (i = 0; i<n; i++)
{
printf ("\nEnter element %i :",i);
scanf("%d",&arr[i]);
}
getch();
return 0;
}
/* OUTPUT:
357
Enter the values one by one:
Enter element 0 :10
Enter element 1 :23
Enter element 2 :5
Enter element 3 :687
Enter element 4 :58
#include<stdio.h>
#include<conio.h>
int main()
{
int temp,x[10],i,n,l,r;
//clrscr();
printf("\n Enter the total number of elements");
scanf("%d",&n);
printf("\n Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
qsort(x,l,r);
printf("\n Numbers after sort");
for(i=0;i<n;i++)
printf("%4d",x[i]);
getch();
return 0;
}
358
void qsort(int b[],int left,int right) //lb=lower bound ub=upper bound
{
int i,j,p,temp,flag;
if(right>left)
{
i=left;
j=right;
p=b[left];
flag=0;
while(!flag)
{
do
{
i++;
}
while((b[i]<=p)&&(i<=right));
while((b[j]>=p)&&(j>left))
{
--j;
}
if(j<i)
flag=1;
else
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
temp=b[left];
b[left]=b[j];
b[j]=temp;
qsort(b,left,j-1);
qsort(b,i,right);
}
}
/* OUTPUT:
359
*/
#include<stdio.h>
#include<conio.h>
int a[50];
void merge(int,int,int);
void merge_sort(int,int);
int main()
{
int num,i;
//clrscr();
printf("\t\t\t\tMERGE SORT\n");
printf("\nEnter the total numbers: ");
scanf("%d",&num);
printf("\nEnter %d numbers: \n",num);
for(i=1;i<=num;i++)
{
scanf("%d",&a[i]);
}
merge_sort(1,num);
printf("\nSORTED ORDER: ");
for(i=1;i<=num;i++)
printf("\t%d",a[i]);
getch();
return 0;
}
360
// MERGE METHOD DEFINITION
/* OUTPUT:
MERGE SORT
361
Enter the total numbers: 5
Enter 5 numbers:
10
2
53
698
12
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],b[10],c[20],i,j,k,f,s;
//clrscr();
i=j=k=0;
while(i<f&&j<s)
{
362
if(a[i]<b[j])
c[k++]=a[i++];
else
c[k++]=b[j++];
}
while(i<f)
c[k++]=a[i++];
while(j<s)
c[k++]=b[j++];
getch();
return 0;
}
/* OUTPUT:
#include<stdio.h>
#include<conio.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
363
int main()
{
int n,i,arr[20];
heapsort(arr,n);
getch();
return 0;
}
for(i=n-1;i>0;i--)
{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
adjust(arr,i);
}
}
while((i>0)&&(item>arr[j]))
364
{
arr[i]=arr[j];
i=j;
j=(i-1)/2;
}
arr[i]=item;
}
}
j=0;
item=arr[j];
i=2*j+1;
while(i<=n-1)
{
if(i+1<=n-1)
if(arr[i]<arr[i+1])
i++;
if(item<arr[i])
{
arr[j]=arr[i];
j=i;
i=2*j+1;
}
else
break;
}
arr[j]=item;
}
/* OUTPUT:
*/
365
DATA STRUCTURES
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
void push();
void pop();
void display();
struct stack
{
int top;
int items[MAX];
};
struct stack s;
int main()
{
int choice;
char option;
//clrscr();
s.top=-1;
do
{
printf("\n ENTER CHOICE");
printf("\n 1)PUSH 2)POP 3)DISPLAY 4)EXIT \n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("ENTER CORRECT CHOICE");
break;
}
printf("\n DO YOU WANT TO CONTINUE(Y/N):");
366
//getch();
fflush(stdin);
scanf("%c",&option);
}
while(option=='y');
getch();
return 0;
}
void push()
{
int x;
if(s.top>=MAX)
{
printf("\n OVER FLOW");
exit(0);
}
else
{
printf("ENTER ELEMENT TO INSERT INTO STACK");
scanf("%d",&x);
s.top++;
s.items[s.top]=x;
}
}
void pop()
{
int x;
if(s.top<0)
{
printf("\nSTACK UNDERFLOW");
getch();
exit(0);
}
else
{
x=s.items[s.top];
s.top--;
printf("\nPOPPED ELEMENT=%d",x);
//s.top--;
}
}
void display()
{
367
int x,i;
printf("\n ELEMENTS IN STACK ARE\n");
for(x=0;x<=s.top;x++)
printf("%3d...",s.items[x]);
}
/* OUTPUT:
ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
1
ENTER ELEMENT TO INSERT INTO STACK 23
ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
1
ENTER ELEMENT TO INSERT INTO STACK 65
ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
1
ENTER ELEMENT TO INSERT INTO STACK 85
ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
3
ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
2
POPPED ELEMENT=85
DO YOU WANT TO CONTINUE(Y/N): n
*/
368
// Program to implement stack using pointers
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int data;
struct stack *next;
}*top=NULL,*temp,*n;
void push();
void pop();
void display();
int main()
{
int ch;
//clrscr();
do
{
printf("\n\n MENU OPTIONS ARE:\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.display\n");
printf("4.EXIT\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("INVALID CHOICE\n");
}
}
while(ch<=4);
getch();
return 0;
369
}
//DISPLAYING ELEMENTS
void display()
{
temp=top;
printf("THE ELEMENTS IN THE STACK ARE..\n");
while(temp!=NULL)
{
printf("%d-",temp->data);
temp=temp->next;
}
}
/* OUTPUT:
370
MENU OPTIONS ARE:
1.PUSH
2.POP
3.display
4.EXIT
ENTER YOUR CHOICE:1
ENTER THE ELEMENT TO BE INSERTED
20
*/
371
/* Program to implement linear queue using arrays */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
int q[10],front=0,rear=-1;
int main()
{
int ch;
void insert();
void delet();
void display();
//clrscr();
printf("\nQueue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Invalid option\n");
}
}
getch();
return 0;
}
void insert()
{
int x;
if(rear==max-1)
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
372
scanf("%d",&x);
q[++rear]=x;
}
}
void delet()
{
int del;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit(0);
}
del=q[front++];
printf("Deleted element is:%d\n",del);
if(front>rear)
{
front=0;
rear=-1;
}
}
void display()
{
int i;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
exit(0);
}
for(i=front;i<=rear;i++)
printf("\t%d",q[i]);
printf("\n");
}
/* OUTPUT:
Queue operations
1.insert
2.delete
3.display
4.exit
Enter your choice:1
Enter element to be insert:25
Enter your choice:1
373
Enter element to be insert:63
Enter your choice:1
Enter element to be insert:45
Enter your choice:3
25 63 45
Enter your choice:2
Deleted element is:25
Enter your choice:3
63 45
Enter your choice:2
Deleted element is:63
Enter your choice:3
45
Enter your choice:2
Deleted element is:45
Enter your choice:3
Queue is underflow
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
int q[max];
int rear = -1, front = -1;
void insert();
void delet();
void display();
main()
{
char ch;
int choice;
do
{
//clrscr();
printf("\n1 -> Insert");
printf("\n2 -> Delete");
printf("\n3 -> Display");
374
printf("\n4 -> Exit");
void insert()
{
int data;
if ((rear == max - 1 && front == 0) || rear == front - 1)
{
printf("\nSorry! Q is Full");
}
else
{
printf("\nEnter data You want to insert ->");
scanf("%d", &data);
if (front == -1)
{
front++;
rear++;
}
else if (rear == max - 1)
{
rear = 0;
}
else
375
{
rear++;
}
q[rear] = data;
printf("\n\nData inserted successfully");
}
}
void delet()
{
if (front == -1)
{
printf("\n\nSorry! Q is Empty");
}
else
{
if (front == rear)
{
front = rear = -1;
}
else if (front == max - 1)
{
front = 0;
}
else
{
front++;
}
printf("\nElement deleted Successfully");
}
}
void display()
{
int i;
if (front == -1)
{
printf("\n\nSorry! Q is Empty");
}
else
{
printf("\n\n:: Queue Elements are ::\n");
if (front <= rear)
{
for (i = front; i <= rear; i++)
{
printf("\n%d", q[i]);
}
376
}
else
{
for (i = front; i < max; i++)
{
printf("\n%d", q[i]);
}
for (i = 0; i <= rear; i++)
{
printf("\n%d", q[i]);
}
}
}
}
/* OUTPUT:
1 -> Insert
2 -> Delete
3 -> Display
4 -> Exit
377
4 -> Exit
10
20
20
*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
378
{
int data;
struct node *next;
}*head,*temp;
void createnode();
void insertbefore();
void insertafter();
void deletenode();
void search();
void display();
int main()
{
int choice;
//clrscr();
do
{
printf("\n MENU OPTIONS\n");
printf("1-CREATE A NODE \n");
printf("2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE\n");
printf("3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE\n");
printf("4-DELETE A NODE\n");
printf("5-SEARCH FOR A SPECIFIED ELEMENT\n");
printf("6-DISPLAY THE ELEMENTS IN THE LIST\n");
printf("7-EXIT\n");
printf("ENTER YOUR CHOICE\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
createnode();
break;
case 2:insertbefore();
break;
case 3:insertafter();
break;
case 4:deletenode();
break;
case 5:search();
break;
case 6:display();
break;
case 7:exit(0);
break;
default:printf("INVALID CHOICE\n");
379
}
}while(choice<=7);
getch();
return 0;
}
void createnode()
{
int x;
int ans;
struct node *n,*temp=head;
n=(struct node*)malloc(sizeof(struct node));
printf("ENTER ELEMENT TO INSERT:");
scanf("%d",&x);
n->data=x;
n->next=NULL;
if(head==NULL)
head=n;
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=n;
}
}
void insertbefore()
{
struct node *n,*temp=head;
int pos,i=1;
printf("ENTER POSITION TO INSERT BEFORE NODE:\n");
scanf("%d",&pos);
if(pos==1)
{
printf("ENTER DATA:");
scanf("%d->",&n->data);
n->next=head;
head=n;
}
else
{
while((temp->next!=NULL)&&(i<(pos-1)))
{
temp=temp->next;
i++;
}
if(temp->next!=NULL)
380
{
n=(struct node*)malloc(sizeof(struct node));
printf("ENTER DATA:\n");
scanf("%d",&n->data);
n->next=temp->next;
temp->next=n;
}
else
printf("INVALID CHOICE\n");
}
}
void insertafter()
{
struct node *n,*temp=head;
int pos,i=1;
printf("ENTER THE POSITION TO INSERT AFTER THE NODE:");
scanf("%d",&pos);
while((temp!=NULL)&&(i<pos))
{
temp=temp->next;
i++;
}
if(temp!=NULL)
{
n=(struct node*)malloc(sizeof(struct node));
printf("ENTER DATA:");
scanf("%d",&n->data);
n->next=temp->next;
temp->next=n;
}
else
printf("INVALID POSITION\n");
}
void deletenode()
{
struct node *temp=head;
int pos,i=1;
printf("ENTER THE POSITION TO DELETE:");
scanf("%d",&pos);
if(pos==1)
head=head->next;
else
{
while((temp->next!=NULL)&&(i<pos-1))
{
381
temp=temp->next;
i++;
}
if(temp->next!=NULL)
{
temp->next=temp->next->next;
}
}
}
void search()
{
struct node *temp=head;
int x,i=1;
printf("ENTER THE ELEMENT TO BE SEARCHED\n");
scanf("%d",&x);
while((temp!=NULL)&&(temp->data!=x))
{
temp=temp->next;
i++;
}
if(temp!=NULL)
printf("%d is FOUND AT POSITION %d\n",temp->data,i);
else
printf("ELEMENT NOT FOUND\n");
}
void display()
{
struct node *temp=head;
printf("THE ELEMENTS IN THE LIST ARE..\n");
for(temp=head;temp!=NULL;)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
/* OUTPUT:
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
382
7-EXIT
ENTER YOUR CHOICE
1
ENTER ELEMENT TO INSERT:10
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
1
ENTER ELEMENT TO INSERT:20
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
6
THE ELEMENTS IN THE LIST ARE..
10->20->
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
2
ENTER POSITION TO INSERT BEFORE NODE:
2
ENTER DATA:
15
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
383
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
6
THE ELEMENTS IN THE LIST ARE..
10->15->20->
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
5
5
ENTER THE ELEMENT TO BE SEARCHED
20
20 is FOUND AT POSITION 3
*/
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int data;
struct queue *next;
}*front=NULL,*rear=NULL;
int main()
{
void insert();
void delet();
void display();
int ch;
//clrscr();
384
printf("\n\tQUEUE IMPLEMENTATION USING LINKED LIST\n");
printf("\n\t1: INSERT\n\t2: DELETE\n\t3: DISPLAY\n\t4: EXIT");
while(1)
{
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: delet(); break;
case 3: display(); break;
case 4: exit(1); break;
default:printf("\nInvalid option");
}
}
return 0;
}
// insert method
void insert()
{
struct queue *new1;
new1=(struct queue*)malloc(sizeof(struct queue));
printf("Enter data to insert into queue:");
scanf("%d",&new1->data);
new1->next=NULL;
if(front==NULL)
front=new1;
else rear->next=new1;
rear=new1;
}
// delete method
void delet()
{
struct queue *temp,*prev;
if(front==NULL)
printf("\nQueue is underflow");
else
{
temp=front;
printf("\nDeleted element is :%d",temp->data);
front=temp->next;
if(front==NULL)
rear=NULL;
free(temp);
}
385
}
// display method
void display()
{
struct queue *temp;
temp=front;
printf("\n\n");
printf("Queue elements are:");
while(temp!=NULL)
{
printf(" \t%d",temp->data);
temp=temp->next;
}
}
/* OUTPUT:
1: INSERT
2: DELETE
3: DISPLAY
4: EXIT
386
Deleted element is :20
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*temp;
void createnode();
void insertbefore();
void insertafter();
void deletenode();
void search();
void display();
int main()
{
int choice;
//clrscr();
do
{
printf("\n MENU OPTIONS\n");
printf("1-CREATE A NODE \n");
printf("2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE\n");
printf("3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE\n");
printf("4-DELETE A NODE\n");
printf("5-SEARCH FOR A SPECIFIED ELEMENT\n");
printf("6-DISPLAY THE ELEMENTS IN THE LIST\n");
printf("7-EXIT\n");
printf("ENTER YOUR CHOICE\n");
387
scanf("%d",&choice);
switch(choice)
{
case 1:
createnode();
break;
case 2:insertbefore();
break;
case 3:insertafter();
break;
case 4:deletenode();
break;
case 5:search();
break;
case 6:display();
break;
case 7:exit(0);
break;
default:printf("INVALID CHOICE\n");
}
}while(choice<=7);
getch();
return 0;
}
void createnode()
{
int x;
int ans;
struct node *n,*temp=head;
n=(struct node*)malloc(sizeof(struct node));
printf("ENTER ELEMENT TO INSERT:");
scanf("%d",&x);
n->data=x;
n->next=NULL;
if(head==NULL)
head=n;
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=n;
}
}
void insertbefore()
{
388
struct node *n,*temp=head;
int pos,i=1;
printf("ENTER POSITION TO INSERT BEFORE NODE:\n");
scanf("%d",&pos);
if(pos==1)
{
printf("ENTER DATA:");
scanf("%d->",&n->data);
n->next=head;
head=n;
}
else
{
while((temp->next!=NULL)&&(i<(pos-1)))
{
temp=temp->next;
i++;
}
if(temp->next!=NULL)
{
n=(struct node*)malloc(sizeof(struct node));
printf("ENTER DATA:\n");
scanf("%d",&n->data);
n->next=temp->next;
temp->next=n;
}
else
printf("INVALID CHOICE\n");
}
}
void insertafter()
{
struct node *n,*temp=head;
int pos,i=1;
printf("ENTER THE POSITION TO INSERT AFTER THE NODE:");
scanf("%d",&pos);
while((temp!=NULL)&&(i<pos))
{
temp=temp->next;
i++;
}
if(temp!=NULL)
{
n=(struct node*)malloc(sizeof(struct node));
printf("ENTER DATA:");
scanf("%d",&n->data);
389
n->next=temp->next;
temp->next=n;
}
else
printf("INVALID POSITION\n");
}
void deletenode()
{
struct node *temp=head;
int pos,i=1;
printf("ENTER THE POSITION TO DELETE:");
scanf("%d",&pos);
if(pos==1)
head=head->next;
else
{
while((temp->next!=NULL)&&(i<pos-1))
{
temp=temp->next;
i++;
}
if(temp->next!=NULL)
{
temp->next=temp->next->next;
}
}
}
void search()
{
struct node *temp=head;
int x,i=1;
printf("ENTER THE ELEMENT TO BE SEARCHED\n");
scanf("%d",&x);
while((temp!=NULL)&&(temp->data!=x))
{
temp=temp->next;
i++;
}
if(temp!=NULL)
printf("%d is FOUND AT POSITION %d\n",temp->data,i);
else
printf("ELEMENT NOT FOUND\n");
}
void display()
390
{
struct node *temp=head;
printf("THE ELEMENTS IN THE LIST ARE..\n");
for(temp=head;temp!=NULL;)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
/* OUTPUT:
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
1
ENTER ELEMENT TO INSERT:10
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
1
ENTER ELEMENT TO INSERT:20
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
6
THE ELEMENTS IN THE LIST ARE..
391
10->20->
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
2
ENTER POSITION TO INSERT BEFORE NODE:
2
ENTER DATA:
15
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
6
THE ELEMENTS IN THE LIST ARE..
10->15->20->
MENU OPTIONS
1-CREATE A NODE
2-INSERT AN ELEMENT BEFORE A SPECIFIED NODE
3-INSERT AN ELEMEMT AFTER A SPECIFIED NODE
4-DELETE A NODE
5-SEARCH FOR A SPECIFIED ELEMENT
6-DISPLAY THE ELEMENTS IN THE LIST
7-EXIT
ENTER YOUR CHOICE
5
5
ENTER THE ELEMENT TO BE SEARCHED
20
20 is FOUND AT POSITION 3
*/
392
/* PROGRAM TO IMPLEMENT QUEUE USING SINGLE LINKED LIST
*/
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int data;
struct queue *next;
}*front=NULL,*rear=NULL;
int main()
{
void insert();
void delet();
void display();
int ch;
//clrscr();
while(1)
{
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: delet(); break;
case 3: display(); break;
case 4: exit(1); break;
default:printf("\nInvalid option");
}
}
return 0;
}
// insert method
void insert()
{
struct queue *new1;
new1=(struct queue*)malloc(sizeof(struct queue));
printf("Enter data to insert into queue:");
scanf("%d",&new1->data);
new1->next=NULL;
393
if(front==NULL)
front=new1;
else rear->next=new1;
rear=new1;
}
// delete method
void delet()
{
struct queue *temp,*prev;
if(front==NULL)
printf("\nQueue is underflow");
else
{
temp=front;
printf("\nDeleted element is :%d",temp->data);
front=temp->next;
if(front==NULL)
rear=NULL;
free(temp);
}
}
// display method
void display()
{
struct queue *temp;
temp=front;
printf("\n\n");
printf("Queue elements are:");
while(temp!=NULL)
{
printf(" \t%d",temp->data);
temp=temp->next;
}
}
/* OUTPUT:
1: INSERT
2: DELETE
3: DISPLAY
4: EXIT
394
Enter your choice: 1
Enter data to insert into queue:20
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
int s[20];
//char s[20];
int top=-1;
int main()
{
char str[20];
char ch;
int i;
395
char pop();
void push(char);
int precd(char);
s[top]='#';
//clrscr();
printf("\n\t Enter infix expression:");
gets(str);
printf("\n\t Postfix expression is:");
for(i=0;str[i]!='\0';i++)
{
if(isalpha(str[i]))
{
printf("%c",str[i]);
continue;
}
if(str[i]==')')
{
while((ch=pop())!='(')
printf("%c",ch);
}
else if(str[i]=='(')
push(str[i]);
396
st=s[top--];
return(st);
}
// precedence function definition
int precd(char s)
{
switch(s)
{
case '$':return(4);
case '*':
case '/': return(3);
case '+':
case '-': return(2);
case '(': return(1);
case '#': return(-1);
}
return(0);
}
/* OUTPUT:
second run:
#define SIZE 40
#include<string.h>
#include <ctype.h>
#include<stdio.h>
#include<conio.h>
void push(char );
char pop();
int priority(char );
char s[SIZE];
int top=-1;
397
int main()
{
char infix[50],prefix[50],ch,elem;
int i=0,k=0;
398
char pop()
{
return(s[top--]);
}
/* OUTPUT:
*/
#include <ctype.h>
#include <stdio.h>
#include <conio.h>
#define SIZE 50
int s[SIZE];
int top=-1;
void push(int n)
{
s[++top]=n;
}
399
int pop()
{
return(s[top--]);
}
main()
{
char postfix[50],ch;
int i=0,op1,op2;
while((ch=postfix[i++])!='\0')
{
if(isdigit(ch))
push(ch-'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}
/* OUTPUT:
400
*/
#include<stdio.h>
#include<conio.h>
int arr[10][10],p[30],visited[20],n,i,j,f=0,k=-1;
void bfsearch(int v)
{
for(i=1;i<=n;i++)
if(arr[v][i]&&!visited[i])
p[++k]=i;
if(f<=k)
{
visited[p[f]]=1;
bfsearch(p[f++]);
}
}
int main()
{
int v;
// clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i]=0;
visited[i]=0;
}
401
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n BFS is not possible");
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
int arr[10][10],reach[20],n;
402
}
}
int main()
{
int i,j,k=0;
//clrscr();
printf("\n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
arr[i][j]=0;
}
dfsearch(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
k++;
}
if(k==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();
}
/* OUTPUT:
Enter number of vertices:6
403
011000
1->2
2->5
2->6
6->3
1->4
Graph is connected
*/
OTHERS
#include<windows.h>
#include<stdlib.h>
int main()
{
system("explorer http://www.google.com");
return 0;
}
#include<stdio.h>
#include<conio.h>
/* OUTPUT:
OS=Windows_NT
404
Path=C:\Dev-Cpp\Bin;C:\Dev-
Cpp\Bin;C:\Perl\site\bin;C:\Perl\bin;C:\Windows\syste
m32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\
WindowsPowerShell\v1
.0\;C:\Program Files\Java\jdk1.7.0_45\bin;C:\Program Files\Microsoft
SQL Server\
100\Tools\Binn\;C:\Program Files\Microsoft SQL
Server\100\DTS\Binn\;;C:\Program
Files\Java\jdk1.7.0_45\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 42 Stepping 7,
GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=2a07
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\RAGHU\AppData\Local\Temp
TMP=C:\Users\RAGHU\AppData\Local\Temp
USERDOMAIN=RAGHU-PC
USERNAME=RAGHU
*/
#include<stdio.h>
#define MAX 10000
void fact(int);
void mul(int);
int length = 0;
int f[MAX];
int main()
{
int n,i;
printf("\nEnter a number:");
scanf("%d",&n);
405
f[0]=1;
fact(n);
getch();
return 0;
}
void fact(int n)
{
int i;
for(i=2;i<=n;i++)
{
mul(i);
}
}
void mul(int n)
{
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++)
{
arr[i]=f[i];
}
for(i=0;i<=length;i++)
{
f[i]=(arr[i]*n+r)%10;
r=(arr[i]*n+r)/10;
}
if(r!=0)
{
while(r!=0)
{
f[i]=r%10;
r= r/10;
i++;
}
}
406
length= i-1;
}
/* OUTPUT:
Enter a number:20
Factorial of 20 is :
2432902008176640000
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int x=10,y=20,result;
result=add(x,y);
printf("Addition of %d and %d=%d",x,y,result);
getch();
return 0;
}
/* OUTPUT:
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
system("C:\\Windows\\System32\\ipconfig");
407
getch();
return 0;
}
/* OUTPUT:
Windows IP Configuration
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,x,n,k;
//clrscr();
printf("Enter a value:");
scanf("%d",&n);
printf("1's complement of %d is %d",n,~n);
getch();
return 0;
}
/* OUTPUT:
Enter a value:5
1's complement of 5 is -6
408
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char bin[16];
int i;
// clrscr();
printf("Enter any binary number:");
gets(bin);
for(i=0;bin[i]!='\0';i++)
{
if(bin[i]!='0'&&bin[i]!='1')
{
printf("The number entered is not a binary number. Enter the correct
number");
exit(0);
}
}
convert(bin);
getch();
return 0;
}
char x[20];
len=strlen(bin);
for(i=len-1;i>=0;i--)
{
409
if(bin[i]=='0')
x[i]='1';
else
x[i]='0';
}
for(i=len-1;i>=0;i--)
{
if(i==len-1)
{
if(x[i]=='0')
x[i]='1';
else
{
x[i]='0';
c=1;
}
}
else
{
if(c==1&&x[i]=='0')
{
x[i]='1';
c=0;
}
else if(c==1&&x[i]=='1')
{
x[i]='0';
c=1;
}
}
}
x[len]='\0';
printf("The 2's complement is: %s",x);
}
/* OUTPUT:
*/
410
/* Program to display relationship between two persons
(FLAMES) */
int main()
{
char str1[20],str2[20],str3[20]={'F','L','A','M','E','S'};
int len,l1,l2,i,j,count=0,str,scount,ch;
// clrscr();
l1=strlen(str1);
l2=strlen(str2);
len=l1+l2;
switch(ch)
{
case 1:
case 7:
case 13:
case 19:printf("\n FRIENDSHIP");
break;
case 2:
411
case 8:
case 14:
case 20:printf("\n LOVE");
break;
case 3:
case 9:
case 15:
case 21:printf("\n AFFECTION");
break;
case 4:
case 10:
case 16:
case 22:printf("\n MARRIAGE");
break;
case 5:
case 11:
case 17:
case 23:printf("\n ENEMIES");
break;
case 6:
case 12:
case 18:
case 24:printf("\n SISTER");
break;
}
getch();
return 0;
}
/* OUTPUT:
Enter first string:RAGHU
Enter second string:RAJANI
FRIENDSHIP
*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
412
printf("Do you want to shutdown your computer now (y/n):");
scanf("%c",&ch);
if (ch=='y'||ch=='Y')
system("C:\\WINDOWS\\System32\\shutdown -s");
getch();
return 0;
}
/* OUTPUT:
Do you want to shutdown your computer now (y/n):y
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char input[20];
//gets the user command
puts("Enter any system command ex:dir or copy etc.,blank to exit");
gets(input);
if(input[0]=='\0')
exit(0);
//execute command
system(input);
getch();
return 0;
}
/* OUTPUT:
413
*/
#include<stdio.h>
#include<conio.h>
void hanoi(int,char,char,char);
int main()
{
int nvalue;
char snvalue='L',invalue='C',dnvalue='R';
//clrscr();
printf("ENTER THE NUMBER OF DISKS");
scanf("%d",&nvalue);
printf("\n\n TOWER OF HANOI PROBLEM WITH %d DISKS \n",nvalue);
hanoi(nvalue,snvalue,invalue,dnvalue);
getch();
return 0;
}
/* OUTPUT:
ENTER THE NUMBER OF DISKS 3
414
MOVE DISK 1 from R to C
MOVE DISK 3 from L to R
MOVE DISK 1 from C to L
MOVE DISK 2 from C to R
MOVE DISK 1 from L to R
*/
#include<stdio.h>
#include<time.h>
int main()
{
time_t t;
int z;
clrscr();
time(&t);
z=ctime(&t);
z=z+10;
printf("Current time: %s\n", z);
getch();
return 0;
}
/* OUTPUT:
*/
GRAPHICS
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
int main()
{
/* request auto detection */
415
int gdriver=DETECT,gmode,errorcode;
char msg[80];
line(10,10,100,200);
//Drawing a line
lineto(300,380);
sprintf(msg,"(%d,%d)", getx(),gety());
outtext(msg);
moveto(200,200);
sprintf(msg,"(%d,%d)", getx(),gety());
outtextxy(200,200,msg);
//Drawing a line
linerel(50,50);
sprintf(msg,"(%d,%d)", getx(),gety());
outtextxy(250,250,msg);
getch();
closegraph();
return 0;
}
416
// Program to create animation for drawing circles
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm,x,y,i,radius=300;
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
for(i=1;i<=10;i++)
{
setcolor(i);
delay(300);
circle(x,y,radius);
radius=radius-40;
}
getch();
closegraph();
return 0;
}
#include<graphics.h>
#include<conio.h>
#include<dos.h>
main()
{
int gd=DETECT,gm,angle=0;
struct arccoordstype p;
417
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
delay(2000);
while(angle<=360)
{
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
setcolor(BLUE);
getarccoords(&p);
circle(p.xstart,p.ystart,20);
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
getarccoords(&p);
setcolor(GREEN);
circle(p.xstart,p.ystart,20);
angle=angle+5;
delay(80);
}
getch();
closegraph();
return 0;
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
/* request auto detection */
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
setcolor(YELLOW); //sets edge color of circle
circle(100,100,30);
418
//Drwaing a circle at the middle
x=getmaxx()/2;
y=getmaxy()/2;
setcolor(RED);
circle(x,y,40);
getch();
closegraph();
return 0;
}
#include<graphics.h>
#include<conio.h>
main()
{
int
gd=DETECT,gm,left=100,top=100,right=200,bottom=200,x=
300,y=150,radius=50;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
rectangle(left,top,right,bottom);
circle(x,y,radius);
bar(left+300,top,right+300,bottom);
line(left-10,top+150,left+410,top+150);
ellipse(x,y+200,0,360,100,50);
arc(120,160,100,200,100);
outtextxy(left+100,top+325,"C Program for Diagrams");
getch();
419
closegraph();
return 0;
}
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int gd=DETECT,gm,area,temp1,temp2,left=25,top=75;
void *p;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(YELLOW);
circle(50,100,25);
setfillstyle(SOLID_FILL,BLUE);
floodfill(50,100,YELLOW);
setcolor(RED);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(44,85,2,6);
fillellipse(56,85,2,6);
ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);
area=imagesize(left,top,left+50,top+50);
p=malloc(area);
setcolor(RED);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(165,451,"Animation Example");
setcolor(BLUE);
rectangle(0,0,639,449);
420
while(!kbhit())
{
temp1=1+random(588);
temp2=1+random(380);
getimage(left,top,left+50,top+50,p);
putimage(left,top,p,XOR_PUT);
putimage(temp1,temp2,p,XOR_PUT);
delay(120);
left=temp1;
top=temp2;
}
getch();
closegraph();
return 0;
}
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i,x,y;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\BGI ");
x=10;
y=50;
settextstyle(DEFAULT_FONT,0,3); //0-left to right
outtextxy(x,y,"Raghu varma");
x=x+120;
y+=20;
x=10;
y=150;
421
settextstyle(GOTHIC_FONT,1,3); //1-bottom to top 3-char
size
outtextxy(x,y,"RAVINDER GOUD");
x=x+100;
y+=30;
getch();
closegraph();
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i=0;i<=15;i++)
{
textbackground(i);
cprintf(" It is a C graphics program ");
delay(1000);
}
getch();
return 0;
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
main()
422
{
int gd=DETECT,gm,i;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
getch();
return 0;
}
#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart Example");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
423
line(590,430,600,420);
outtextxy(95,35,"Y-AXIS");
outtextxy(610,405,"X-AXIS");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,YELLOW);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,AQUA,LIGHTRED);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);
getch();
return 0;
}
#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm,midx,midy;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(MAGENTA);
rectangle(0,40,640,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(YELLOW);
424
outtextxy(275,10,"Pie Chart Example");
midx=getmaxx()/2;
midy=getmaxy()/2;
setfillstyle(LINE_FILL,BLUE);
pieslice(midx,midy,0,75,100);
outtextxy(midx+100,midy-75,"21.73%");
setfillstyle(XHATCH_FILL,RED);
pieslice(midx,midy,75,225,100);
outtextxy(midx-175,midy-75,"39.67%");
setfillstyle(WIDE_DOT_FILL,GREEN);
pieslice(midx,midy,225,360,100);
outtextxy(midx+75,midy+75,"38.60%");
getch();
return 0;
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int
gd=DETECT,gm,coords[]={100,100,200,200,100,400,100,100
};
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
setcolor(RED);
drawpoly(4,coords);
getch();
closegraph();
return 0;
425
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm,x,y,i,radius=200,left,right,top,bottom;
initgraph(&gd,&gm,"c:\\turboc3\\BGI");
left=getmaxx()/2-100;
top=getmaxy()/2-100 ;
right=getmaxx()/2+200;
bottom=getmaxy()/2+200 ;
for(i=1;i<=10;i++)
{
setcolor(i);
/* draw a rectangle */
rectangle(left,top,right,bottom);
left=left+20;
top=top+20;
right=right-20;
bottom=bottom-20;
delay(200);
}
getch();
closegraph();
return 0;
}
main()
{
int i;
clrscr();
for(i=0;i<=15;i++)
{
textcolor(i+BLINK);
delay(1000);
cprintf(" C is a programming language");
}
getch();
return 0;
}
#include<graphics.h>
#include<dos.h>
#include<conio.h>
main()
{
int i,j=0,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving car");
getch();
setviewport(0,0,639,440,1);
for(i=0;i<=420;i=i+10,j++)
{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
427
circle(75+i,410,12);
circle(175+i,410,12);
setcolor(j);
delay(120);
if(i==420)
break;
clearviewport();
}
getch();
closegraph();
return 0;
}
428