[go: up one dir, main page]

0% found this document useful (0 votes)
4K views428 pages

500 C Programs For BOOK Print

The document contains C code examples demonstrating various functions of the C programming language like printf(), scanf(), escape characters, mathematical operations etc. It includes programs to print output formats using printf(), take input using scanf(), evaluate mathematical expressions, find data type ranges and more. Each program is accompanied by its output for demonstration purposes. The document serves as a reference for basic C programming concepts.

Uploaded by

Sanjana Pulapa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views428 pages

500 C Programs For BOOK Print

The document contains C code examples demonstrating various functions of the C programming language like printf(), scanf(), escape characters, mathematical operations etc. It includes programs to print output formats using printf(), take input using scanf(), evaluate mathematical expressions, find data type ranges and more. Each program is accompanied by its output for demonstration purposes. The document serves as a reference for basic C programming concepts.

Uploaded by

Sanjana Pulapa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 428

/* WELCOME PROGRAM */

#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

*/

// PROGRAM ON PRINTF() FUNCTION

#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%d",a); //prints a normally


printf("\n%10d",a); // prints a value after 10 column spaces
printf("\n%-10d",a); // a left align to 10 spaces

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
*/

/* PROGRAM ON PRINTF() FUNCTION */

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

// out put formats

int main()
{
int i=1234;
float f=12345.676747;
char c='A';
char str[]="C IS A PROGRAMMING LANGUAGE";

2
// clrscr();

printf("\n str right : %30s",str);


printf("\n str left : %-30s",str);
printf("\n first 4 of str right: %30.4s",str);
printf("\n first 8 of str left : %-30.8s",str);
printf("\n first 7 of str right: %*.*s",25,7,str);

printf("\n char right : %10c",c);


printf("\n char left : %-10c",c);

printf("\n int right : %10d",i);


printf("\n int left : %-10d",i);

printf("\n float right : %20f",f);


printf("\n float left : %-20f",f);
printf("\n float right round to 2 digits : %10.2f",f);
printf("\n float left round to 4 digits : %-10.4f",f);

getch();
return 0;
}

/* OUTPUT:

str right : C IS A PROGRAMMING LANGUAGE


str left : C IS A PROGRAMMING LANGUAGE
first 4 of str right: C IS
first 8 of str left : C IS A P
first 7 of str right: C IS A
char right : A
char left : A
int right : 1234
int left : 1234
float right : 12345.676758
float left : 12345.676758
float right round to 2 digits : 12345.68
float left round to 4 digits : 12345.6768

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

printf("\n Enter YOUR address:");


fflush(stdin);
scanf("%10[^\n]s",str); // reads only 10 chars
printf("\n Address=%s",str);

printf("\n Enter tel no:");


fflush(stdin);
scanf("%[0-9]10s",str);//reads only numbers
printf("\n Telephone no=%s",str);

printf("\n Enter any string in capital alphabets:");


fflush(stdin);
scanf("%10[A-Z]s",str);//reads only 10 capital letters
printf("\n String in caps=%s",str);

printf("\n Enter any string in small alphabets:");


fflush(stdin);
scanf("%10[a-z][^\n]s",str); //reads only 10 small letters
printf("\n String in small=%s",str);

printf("\n Enter any string in capital and small alhabpets:");


fflush(stdin);
scanf("%10[a-z,A-Z]s",str);//reads both small & caps letters
printf("\n String in small and capitals=%s",str);

getch();
return 0;
}

/* OUTPUT:

Enter 4 digit number:1234


given no=1234.000000

Enter YOUR address:Karimnagar


Address=Karimnagar

4
Enter tel no:9490230057
Telephone no=9490230057

Enter any string in capital alphabets:RAVINDER


String in caps=RAVINDER

Enter any string in small alphabets:ravi


String in small=ravi

Enter any string in capital and small alhabpets:Ravinder


String in small and capitals=Ravinder
*/

// Program to write a C program without main function

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

#define begin main //here #define is preprocessor directive

int begin()
{
printf("Hello....Welcome to C");

getch();
return 0;
}

/* OUTPUT:

Hello....Welcome to C

*/

/* PROGRAM TO KNOW THE DATA TYPES RANGE */


#include<stdio.h>
#include<conio.h>
#include<limits.h> //Header file for all data type constants

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

printf("\n\n MINIMUM VALUE OF INTEGER=%d",INT_MIN);


printf("\n MAXIMUM VALUE OF INTEGER=%d",INT_MAX);
printf("\n MAXIMUM VALUE OF UNSIGNED INTEGER=%u",UINT_MAX);

printf("\n\n MINIMUM VALUE OF LONG=%ld",LONG_MIN);


printf("\n MAXIMUM VALUE OF LONG=%ld",LONG_MAX);

getch();
return 0;
}

/* OUTPUT:

MINUMUM VALUE OF CHAR=-128


MAXIMUM VALUE OF CHAR=127
NUMBER OF BITS IN A BYTE=8
MAXIMUM VALUE OF SIGNED CHAR=255

MINIMUM VALUE OF SHORT INTEGER=-32768


MAXIMUM VALUE OF SHORT INTEGER=32767
MAXIMUM VALUE OF UNSIGNED SHORT INTEGER=65535

MINIMUM VALUE OF INTEGER=-32768


MAXIMUM VALUE OF INTEGER=32767
MAXIMUM VALUE OF UNSIGNED INTEGER=65535

MINIMUM VALUE OF LONG=-2147483648


MAXIMUM VALUE OF LONG=2147483647
*/

/* PROGRAM ON ESCAPE CHARACTERS */

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

int main()
{
//clrscr();

printf("ESCAPE CHARACTERS EXAMPLE");


printf("\n -------------------------"); // \n PRINTS NEW LINE

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;

printf("\n Addition of %d and %d is %d",x,y,sum);


printf("\n Multiplication of %d and %d is %d",x,y,mul);
printf("\n Subtraction of %d and %d is %d",x,y,sub);
printf("\n Division of %d and %d is %d",x,y,div);

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

*/

/* PROGRAM TO PRINT ADDITION,MULTIPLICATION,DIVISION,


SUBTRACTION,REMAINDER OF GIVEN 2 NUMBERS WITHOUT
TEMPORARY VARIABLES*/

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

int main()
{
int x,y;
clrscr();
printf(" Enter any 2 numbers \n");
scanf("%d%d",&x,&y);

printf("\n Addition of %d and %d is %d",x,y,x+y);


printf("\n Multiplication of %d and %d is %d",x,y,x*y);
printf("\n Subtraction of %d and %d is %d",x,y,x-y);
printf("\n Division of %d and %d is %d",x,y,x/y);
printf("\n Remainder of %d and %d is %d",x,y,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

*/

/* PROGRAM TO EVALUATE ax+b/ax-b */

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

int main()
{
int a,b,x;
float result;
clrscr();

printf("Enter a,b,x values \n");


scanf("%d%d%d",&a,&b,&x);
result=(a*x+b)/(a*x-b);

printf("\n Result=%f",result);
getch();
return 0;
}

/* OUTPUT:
Enter a,b,x values
123
Result=5.000000
*/

/* PROGRAM TO EVALUATE 2.5logx+cos32+x2-y2+2xy */

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

/* PROGRAM TO EVALUATE (2X+3Y)*(4X+5Y)/(9/3)*(6X+3Y) */

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

int main()
{
int x,y;
float result;
clrscr();

printf("Enter x,y values \n");


scanf("%d%d",&x,&y);
result=((2*x+3*y)*(4*x+5*y))/((9.0/3)*(6*x+3*y));

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("Enter x,y,m,n values \n");


scanf("%d%d%d%d",&x,&y,&x,&y);
result=(pow(x,3)+pow(x,2))/((y*y*y)+(y*y)+(m/n)+72.5);

printf("\n Result=%f",result);
getch();
return 0;
}

/* OUTPUT:
Enter x,y,m,n values
10 5 12 4
Result=12.195439
*/

/* PROGRAM TO CONVERT FROM KILO METERS TO METERS &


VICE-VERSA */

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

printf("\n Enter number meters \n");


scanf("%d",&m);
k=m/1000;
printf("Value in kilo meters=%d \n",k);

getch();
return 0;
}

11
/* OUTPUT:
Enter number of kilo meters
5
Value in meters=5000

Enter number of meters


3000
Value in kilo meters=3

*/

/* PROGRAM TO SUBTRACT TWO NUMBERS WITHOUT USING '-'


OPERATOR */

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

/* PROGRAM TO PRINT % and %d SYMBOL */

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

/* PROGRAM TO PRINT ; SYMBOL WITHOUT USING if */

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

int main()
{
clrscr();
if(printf("%c",59))
{
}

getch();
return 0;
}

/* OUTPUT:
;
*/

/* PROGRAM TO PRINT HELLO WORLD WITHOUT USING printf


DIRECTLY */

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

int main()
{
clrscr();
if(printf("HELLO WORLD"))
{
}

getch();
return 0;
}

/* OUTPUT:

13
HELLO WORLD
*/

/* PROGRAM TO CONVERT GIVEN CENTIGRADE TO IT'S


EQUIVALENT FAHRENHEIT & VICE-VERSA */

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

int main()
{
float f,c;
//clrscr();

printf("Enter fahrenheit value:");


scanf("%f",&f);
c=5.0/9*(f-32);
printf("\n Value in centigrade=%f",c);

printf("\n Enter centigrade value:");


scanf("%f",&c);
f=(9*c)/5+32;
printf("\n Value in fahrenheit=%f",f);

getch();
return 0;
}

/* OUTPUT:
Enter fahrenheit value:50
Value in centigrade=10.000000

Enter centigrade value:60


Value in fahrenheit=140.000000
*/

/* PROGRAM TO DISPLAY SQUARE & CUBE OF A GIVEN NUMBER


*/

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

int main()
{
float i;
clrscr();

14
printf("Enter any value \n");
scanf("%f",&x);

printf("\n Square of a given number=%f",i*i);


printf("\n Cube of a given number=%f",i*i*i);

printf("\n\n Square of a given number=%f",pow(i,2));


printf("\n Cube of a given number=%f",pow(i,3));
getch();
return 0;
}

/* OUTPUT:
Enter any value
4

Square of a given number=16.000000


Cube of a given number=64.000000

Square of a given number=16.000000


Cube of a given number=64.000000

*/

/* PROGRAM TO FIND VOLUME OF A CYLINDER */

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

int main()
{
int l,b,h,v;
clrscr();

printf(" Enter the values length, breadth and height: \n");


scanf("%d%d%d",&l,&b,&h);
v=l*b*h;
printf("\n Cylinder Volume=%d",v);

getch();
return 0;
}

/* OUTPUT:
Enter the values length, breadth and height:
4 8 6
Cylinder Volume=192

15
*/

/* PROGRAM TO FIND AREA OF TRIANGLE */

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

int main()
{
int s,a,b,c;
float area;
clrscr();

printf(" Enter s,a,b,c values: \n");


scanf("%d%d%d%d",&s,&a,&b,&c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n Area of Circle=%f",area);

getch();
return 0;
}

/* OUTPUT:
Enter s,a,b,c values:
20 2 5 6

Area of Circle=274.954529
*/

/* PROGRAM TO CALCULATE AREA,PERIMETER OF


SQUARE,RECTANGLE,
TRIANGLE AND CIRCLE */

#include<stdio.h>
#include<conio.h>
#define pi 3.14

int main()
{
int x,l,b,r;
//clrscr();

printf(" Enter side for square \n");


scanf("%d",&x);
printf("\n Square Area=%d and Perimeter=%d",x*x,4*x);

printf("\n Enter length,bredth for rectangle \n");

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

printf("\n Enter radius for circle \n");


scanf("%d",&r);
printf("\n Circle Area=%f and Perimeter=%f",pi*r*r,2*pi*r);

getch();
return 0;
}

/* OUTPUT:
Enter side for square
4

Square Area=16 and Perimeter=16


Enter length,bredth for rectangle
26

Rectangle Area=12 and Perimeter=16


Triangle Area=6.000000
Enter radius for circle
7

Circle Area=153.860000 and Perimeter=43.960000

*/

/* PROGRAM TO CALCULATE SIMPLE INTEREST */

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

printf("Enter p,n,r values \n");


scanf("%f%d%f",&p,&n,&r);

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

printf("Enter p,n,r values \n");


scanf("%f%d%f",&p,&n,&r);

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
*/

// PROGRAM ON SIZEOF OPERATOR

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

/* PROGRAM TO ILLUSTRATE SHORTHAND OPERATORS */

#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

*/

/* PROGRAM TO CONVERT GIVEN DAYS INTO EQUIVALENT


YEARS,MONTHS and WEEKS*/

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

printf("Years= %d \n Months= %d \n Weeks =%d \n Remaining days =


%d",years,months,weeks,rdays);

getch();
return 0;
}
/* OUTPUT:
Enter number of days
800
Years= 2
Months= 26
Weeks =114
Remaining days = 20
*/

/* PROGRAM TO DISPLAY CURRENT DATE & TIME */

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

int main()
{
time_t t;
time(&t);

20
//clrscr();

printf("Today's date and time : %s",ctime(&t));


getch();
return 0;
}

/* OUTPUT:
Today's date and time : Sat Nov 23 10:52:05 2013
*/

/* PROGRAM TO DISPLAY ASCII VALUE OF A GIVEN CHARACTER &


VICE-VERSA */

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

int main()
{
int x;
char ch;
clrscr();

printf(" Enter a character: \n");


scanf("%c",&ch);
printf("\n ASCII value of %c is %d",ch,ch);

printf("\n Enter a ASCII value: \n");


scanf("%d",&x);
printf("\n Charcater of %d is %c",x,x);

getch();
return 0;
}

/* OUTPUT:
Enter a character:A
ASCII value of A is 65
Enter a ASCII value: 68
Charcater of 68 is D
*/

/* PROGRAM ON BIT-WISE OPERATORS */

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

/* PROGRAM TO FIND TOTAL & AVERAGE OF 3 SUBJECTS */

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

Enter 3 subject marks


50 80 60

Total marks=190 and


Average=63.333332

*/

/* PROGRAM TO ILLUSTRATE ALL MATHEMATICAL FUNCTIONS */

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

// PROGRAM ON UNARY OPERATORS

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

int main()
{
int a=10;
// clrscr();

printf("a++ = %d\n",a++); //10


printf("a = %d\n",a); //11
printf("++a = %d\n",++a); //12

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
*/

/* PROGRAM TO SWAP GIVEN 2 NUMBERS USING THIRD


VARIABLE */

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

int main()
{
int a,b,c;
//clrscr();

printf(" Enter two numbers: \n");


scanf("%d%d",&a,&b);
printf("\n Values before swap a=%d and b=%d",a,b);
c=a;
a=b;
b=c;
printf("\n Values after swap a=%d and b=%d",a,b);

getch();
return 0;
}

25
/* OUTPUT:
Enter two numbers:
10 15

Values before swap a=10 and b=15


Values after swap a=15 and b=10

*/

/* PROGRAM TO SWAP GIVEN 2 NUMBERS WITHOUT USING


THIRD VARIABLE */

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

int main()
{
int a,b;
//clrscr();

printf(" Enter two numbers: \n");


scanf("%d%d",&a,&b);
printf("\n Values before swap a=%d and b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n Values after swap a=%d and b=%d",a,b);

getch();
return 0;
}

/* OUTPUT:
Enter two numbers:
10 15

Values before swap a=10 and b=15


Values after swap a=15 and b=10

*/

/* ANOTHER METHOD:
b=a+b-(a=b);
*/

/* PROGRAM TO SWAP GIVEN 2 NUMBERS USING XOR */

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

int main()
{
int a,b;
//clrscr();

printf(" Enter two numbers: \n");


scanf("%d%d",&a,&b);
printf("\n Values before swap a=%d and b=%d",a,b);

a=a^b;
b=a^b;
a=a^b;

printf("\n Values after swap a=%d and b=%d",a,b);

getch();
return 0;
}

/* OUTPUT:
Enter two numbers:
10 15

Values before swap a=10 and b=15


Values after swap a=15 and b=10

*/

CONDITIONAL CONSTROL STATEMENTS


/* PROGRAM TO FIND MAXIMUM NUMBER FROM THE GIVEN TWO
NUMBERS */

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

Enter x,y values


20 5

20 is big
*/

/* PROGRAM TO FIND MAXIMUM NUMBER FROM THE GIVEN


THREE NUMBERS */

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

Enter x,y,z values


20 45 33

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
*/

/* PROGRAM TO CHECK WHETHER A GIVEN CHARACTER IS A


VOWEL OR CONSONANT USING ELSE-IF LADDER */

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

/* PROGRAM TO CHECK WHETHER A GIVEN CHARACTER IS A


VOWEL OR CONSONANT USING LOGICAL OPERATOR */

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

int main()
{
char ch;
//clrscr();

printf("\n Enter a character\n");


scanf("%c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("\n %c is a vowel",ch);
else
printf("\n %c is a consonant",ch);

getch();
return 0;
}

/* OUTPUT:

30
Enter a character z
z is a consonant
*/

/* PROGRAM TO FIND WETHER A TRIANGLE IS


ISOSCELES,EQUILATERAL OR SCALENE */

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

int main()
{
int x,y,z;
//clrscr();

printf("Enter 3 sides of triangle:");


scanf("%d%d%d",&x,&y,&z);

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

Two sides are same so it is iscoceles triangle

*/

/* PROGRAM TO FIND WETHER A TRIANGLE IS ACUTE,OBTUSE OR


RIGHT ANGLE */

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

int main()
{
int x,y,z;

31
//clrscr();

printf("Enter 3 Angles of triangle:");


scanf("%d%d%d",&x,&y,&z);

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

Three angles are less than 90 So it is acute triangle

*/

/*Program to determine whether the character entered is a


capital, a small, a digit or a special symbol using the following
ASCII values
Character ASCII values
---------------------
A-Z 65-90
a-z 97-122
0-9 48-57
Special symbols 0-47, 58-64, 91-96,123-127 */

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

/* PROGRAM TO FIND MAXIMUM NUMBER FROM THE GIVEN TWO


NUMBERS USING CONDITIONAL OPERATORS */

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

Enter x,y values


30 45

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:

Enter x,y,z values


10 5 46

46 is big
*/

/* PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS EVEN OR


ODD */

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

/* PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS EVEN OR


ODD USING CONDITIONAL OPERATORS */

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

/* Program to display opposite case of a given alphabet (small to


capital and vice-versa) */

#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

You entered upper case=A


Character in lower case=a
*/

/* PROGRAM TO PRINT GIVEN 3 NUMBERS IN ASCENDING ORDER


*/

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

printf("\n Ascending order is: %d %5d %5d",x,y,z);

getch();
return 0;
}
/* OUTPUT:

Enter x,y,z values:


30 10 20

Ascending order is: 10 20 30

*/

// PROGRAM TO EVALUATE ((c*d)-(a*b)/(a+b))/(c-d) WHEN (c-


d) IS NOT EQUAL TO ZERO

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

main()
{
int a,b,c,d;
float result;

printf("Enter a,b,c,d values");


scanf("%d%d%d%d",&a,&b,&c,&d);

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
*/

// PROGRAM TO EVALUATE FUNCTION y=4x-8 if x>1 , y=2x+3 if


x=0

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

main()
{
int x,y;

printf("Enter x,y values");


scanf("%d%d",&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
*/

/* Progam to evaluate the function using compound assignment


operators
If k=3 increase x by 2
If k=10 increase x by 3
If k=15 increase x by 4 */

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

/* PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS POSITIVE


OR NEGATIVE OR ZERO */

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

/* PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS POSITIVE


OR NEGATIVE USING CONDITIONAL OPERATORS */

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

-24 is negative number


*/

/* Program to accept a single number and check whether


the given number is divisible by 4 and 9 or not */

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

// PROGRAM TO PRINT 10 NUMBERS USING GOTO

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

// PROGRAM TO FIND SUM OF FIRST 10 NATURAL NUMBERS


USING GOTO

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
*/

/* PROGRAM TO CHECK WHETHER A GIVEN YEAR IS LEAP YEAR


OR NOT */

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

/* PROGRAM TO CHECK WHETHER A GIVEN YEAR IS LEAP YEAR


OR NOT USING CONDITIONAL(TERNARY) OPERATORS */

#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

1998 is not a leap year


*/

/* PROGRAM TO ILLUSTRATE LOGICAL OPERATORS */

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

/* PROGRAM TO ILLUSTRATE RELATIONAL OPERATORS */

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

int main()
{
int a=5,b=3;
//clrscr();

printf("\n %d>%d Result is %d",a,b,a>b);


printf("\n %d>=%d Result is %d",a,b,a>=b);
printf("\n %d<%d Result is %d",a,b,a<b);
printf("\n %d<=%d Result is %d",a,b,a<=b);
printf("\n %d==%d Result is %d",a,b,a==b);
printf("\n %d!=%d Result is %d",a,b,a!=b);

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

Roots are -0.219224,-2.28077

*/

// PROGRAM TO CALCULATE EMPLOYEE SALARY

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

/* Super market gives discount as


Customer purchase amount Discount
>2000 20%
>1000 15%
>500 10%
Calculate Net amount payable. */

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

Enter purhase amount

48
10000
Total net to be paid is 8000.000000
*/

/* PROGRAM TO FIND TOTAL,AVERAGE AND GRADE OF THREE


SUBJECTS */

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

printf("\n Total marks=%d and \n Average=%f", sum, avg);

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:

Enter 3 subject marks

49
40
40
40

Total marks=120 and


Average=40.000000
Third Division

*/

SWITCH STATEMENT

/* PROGRAM TO PRINT SUM,SUB,MUL,DIV,REM OF GIVEN TWO


NUMBERS
DEPENDING ON THE GIVEN DIGIT (1-add 2-sub etc.) */

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

/* PROGRAM TO PRINT SUM,SUB,MUL,DIV,REM OF GIVEN TWO


NUMBERS
DEPENDING ON THE GIVEN OPERATOR (+-add *-mul etc.) */

#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

*/

//PROGRAM TO CHECK WHETHER A CHARACTER IS VOWEL OR


CONSONANT

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

//PROGRAM TO PRINT GIVEN DIGIT(0-9) IN WORDS

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

//Program to print corresponding number month in words. (Ex:1-


January, 2-february etc.

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

Enter a number between 1 and 12


5
MAY
End of main
*/

//PROGRAM TO CONVERT YEAR INTO


MONTHS,DAYS,HOURS,MINUTES

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

int main()
{

int ch,min,hours,days,months,years,sec;
//clrscr();

printf("\n l-MINUTES \n 2-HOURS \n 3-DAYS \n 4-MONTHS \n 5-


SECONDS \n 6-EXIT");
printf("\n Enter Your Choice:");
scanf("%d",&ch);

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

*/

//PROGRAM TO PRINT DAY OF THE MONTH

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

int leaptest(int date, int month,int year);


int find_dayofweek(int date, int month, int year);

//------------------------------------------
int main()
{
int date,month,year;
//clrscr();

printf("Enter the year:");


scanf("%d",&year);

printf("Enter the month:");


scanf("%d",&month);

printf("Enter the date:");


scanf("%d",&date);

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;

printf("\n Given Date:%d/%d/%d \n",date,month,year);

57
dayofweek=1.25*yy+leaptest(date,month,year)+date-2*(century%4);
//function of DAY for Gregorian

dayofweek=dayofweek%7; //remainder on division by 7

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;

//leap function 1 for leap & 0 for non-leap


if((year%100==0)&&(year%400!=0))
leap=0;
else if(year%4==0)
leap=1;
else
leap=0;

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

*/

// PROGRAM TO CONVERT GIVEN NUMBER INTO WORDS

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

Enter any number: 54874


Fifty Four Thousand Eight Hundred Seventy Four
*/

// PROGRAM FOR ATM TRANSACTIONS

#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

Do you want another transaction(y/n)?:y


-------------------------------------------
Choose your Option
1-Inquiry Balance
2-Withdraw
3-Deposit
4-Exit:
------------------------------------------
3

Your Balance: 0

Enter Amount to Deposit: 2000

Your Current Balance 2000


Do you want another transaction(y/n)?:y
-------------------------------------------
Choose your Option
1-Inquiry Balance
2-Withdraw
3-Deposit
4-Exit:
------------------------------------------
2

65
Your Balance: 2000

Enter Amount to Withdraw: 500

Your Current Balance 1500

Do you want another transaction(y/n)?:y


-------------------------------------------
Choose your Option
1-Inquiry Balance
2-Withdraw
3-Deposit
4-Exit:
------------------------------------------
1

Your balance: 1500

Do you want another transaction(y/n)?:y


-------------------------------------------
Choose your Option
1-Inquiry Balance
2-Withdraw
3-Deposit
4-Exit:
------------------------------------------
4

Do you Want to Exit(y/n)?:y

Thank You & Come Again

*/

PREPROCESSOR DIRECTIVES

// PROGRAM ON PREDEFINED MACROS

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

Current time: 12:19:10


Current date: Oct 30 2014

*/

/*PROGRAM ON #IFDEF PREPROCESSOR DIRECTIVE */

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

/*PROGRAM ON #DEFINE PREPROCESSOR DIRECTIVE */

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

ENTER RADIUS VALUE 5

circle area=78.500000
circle perimeter=31.400000
*/

/* PROGRAM ON #DEFINE PREPROCESSOR DIRECTIVE */


#include<stdio.h>
#include<conio.h>
# define AND &&
#define OR ||
#define X printf("\n welcome to c");
#define y b>a AND b>c

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
*/

/*PROGRAM ON #IF,#ELSE PREPROCESSOR DIRECTIVE */

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

/* PROGRAM ON #PRAGMA PREPROCESSOR DIRECTIVE */

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

IT IS F1 FUNCTION EXECUTES BEFORE MAIN START


IT IS MAIN FUNCTION

IT IS F2 FUNCTION EXECUTES BEFORE MAIN END


*/

LOOPS

//PROGRAM TO PRINT FIRST 10 NATURAL NUMBERS USING


WHILE LOOP

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

//PROGRAM TO PRINT FIRST 10 NATURAL NUMBERS IN REVERSE


ORDER USING DO-WHILE LOOP

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

//PROGRAM TO PRINT GIVEN RANGE OF NUMBERS USING FOR


LOOP

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

//PROGRAM TO FIND AVERAGE OF FIRST 10 NATURAL NUMBERS

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

/* PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER


*/

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

72
int main()
{
int i,n,fact=1;
//clrscr();

printf("Enter a number \n");


scanf("%d",&n);
for(i=1;i<=n;i=i+1)
{
fact=fact*i;
}
printf("\n Factorial of %d is %d",n,fact);

getch();
return 0;
}

/* OUTPUT:
Enter a number
5
Factorial of 5 is 120
*/

//PROGRAM TO PRINT ALL FACTORS OF A GIVEN NUMBER

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

/* PROGRAM TO GENERATE FIBONACCI SERIES */

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

// Program to generate all combinations of 1, 2 and 3 using for


loop

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

All combinations of 1, 2 and 3 are:

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

*/

//Program to find the value of one number raise to the power of


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

int main( )
{
int x,y,i,ans=1;
//clrscr( );

printf("Enter base and power values\n");


scanf("%d%d",&x,&y);

for(i=1;i<=y;i++)
ans=ans*x;

printf("\n %d power %d is %d",x,y,ans);

getch();
return 0;
}

/* OUTPUT:
Enter base and power values
25

2 power 5 is 32
*/

//PROGRAM TO FIND SUM OF FIRST 10 NATURAL NUMBERS

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

// Program to add 1 to every digit of a given number

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

printf("\n Sum of first and last digit in a number %d is %d",n,x+y);

getch();
return 0;
}

/* OUTPUT:
Enter three digit number
456

Sum of first and last digit in a number 456 is 10

*/

// Program to condense given number into a single digit


//EX:167=1+6+7=14=1+4=5

#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

*/

//Program to find sum of the individual digits of a given number

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

//Program to find sum of the individual digits of a given number


in a single statement

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

printf("Sum of individuals of given number %d is %d",temp,sum);

getch();
return 0;
}

/* OUTPUT:
Enter a number:425
Sum of individuals of given number 425 is 11
*/

//PROGRAM TO FIND SUM OF EVEN NUMBERS WITHIN GIVEN


RANGE

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

// Program to find sum of the even position digits in a given


number
//Ex:4781= 7+1=8

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

/*extract digits from LSB to MSB and add even digits*/


while(n>0)
{
rem=n%10;
if(count%2==0)
{
evensum=evensum+rem;
}

81
n=n/10;
count++;
}

printf("Sum of even digits is %d\n", evensum);

getch();
return 0;
}

/* OUTPUT:
Enter a number:12345
Sum of even digits is 6
*/

//PROGRAM TO FIND SUM OF 1+2+3+......+n

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

printf("Sum of natural numbers upto %d is %d",n,sum);

getch();
return 0;
}

/* OUTPUT:
ENTER n VALUE
10
Sum of natural numbers upto 10 is 55
*/

//PROGRAM TO FIND SUM OF ODD NUMBERS WITHIN GIVEN


RANGE

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
*/

// Program to find sum of the odd position digits in a given


number
//Ex:4781= 4+8=12

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

/*extract digits from LSB to MSB and add even digits*/


while(n>0)
{
rem=n%10;
if(count%2==1)
{
oddsum=oddsum+rem;
}
n=n/10;
count++;
}
printf("Sum of odd digits is %d\n", oddsum);

getch();
return 0;
}

/* OUTPUT:
Enter a number:12345
Sum of odd digits is 9
*/

/* PROGRAM TO TEST WHETEHER A GIVEN NO. IS PRIME OR NOT


*/

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

/* PROGRAM TO COUNT TOTAL NUMBER OF PRIME NUMBERS


BETWEEN 1-500 */

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

/* PROGRAM TO PRINT ALL PRIME NUMBERS BETWEEN 1-100 */

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

//Program to count numbers between 1 to 100 not divisible by 2,


3 and 5

#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++;
}

printf("Total numbers not divisible by 2,3 and 5 are %d",count);

getch();
return 0;
}

/* OUTPUT:
Total numbers not divisible by 2,3 and 5 are 26
*/

//PROGRAM TO COUNT EVEN NUMBERS BETWEEN 100 AND 200

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

//PROGRAM TO PRINT EVEN NUMBERS BETWEEN 100 AND 200

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

//PROGRAM TO COUNT ODD NUMBERS BETWEEN 100 AND 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
*/

//PROGRAM TO PRINT ODD NUMBERS BETWEEN 100 AND 200

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

/* PROGRAM TO CHECK WHETHER A GIVEN NO. IS PERFECT OR


NOT
Ex: 6=1+2+3=6*/

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

/* PROGRAM TO COUNT ALL PERFECT NUMBERS BETWEEN 1-1000


Ex: 6=1+2+3=6*/

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

/* PROGRAM TO PRINT ALL PERFECT NUMBERS BETWEEN 1-1000


Ex: 6=1+2+3=6*/

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

int main()
{
int i,n,sum;
//clrscr();

printf("Perfect numbers between 1-1000 are\n");


for(n=1;n<=1000;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)

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
*/

//Program to print the reverse of a given 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
*/

//Program to check whether the given number is armstrong or


not

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

/* PROGRAM TO FIND ARMSTRONG NUMBERS BETWEEN 1 and


500 */
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int n,t,rem,sum,i;
//clrscr();

printf("Armstrong numbers between 1 and 500 are \n ");


for(n=1;n<=500;n++)
{
t=n;
sum=0;
while(t>0)
{ rem=t%10;
sum=sum+pow(rem,3);

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
*/

// PROGRAM TO FIND LCM OF TWO NUMBERS

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

Enter any two numbers:8 16


L.C.M=16
*/

// PROGRAM TO FIND GCD OF GIVEN TWO NUMBERS

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

*/

//Program to check how many times a digit is repeated in a


number

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

printf("Enter a digit \n");


scanf("%d",&digit);
temp=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

1 is found 3 times in a number 121341


*/

// PROGRAM TO FIND SUM OF SQUARE OF EACH DIGIT OF A


NUMBER

#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

Sum of square of each digit of number 1234 is 30

*/

// PROGRAM TO ILLUSTRATE 'break' STATEMENT

#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");
}
}

printf("\n Out of loops");

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

*/

// PROGRAM TO ILLUSTRATE 'continue' STATEMENT

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

int main()
{
int i,n,x,res;

printf("\n How many numbers you want to enter: ");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter only positive number:");
scanf("%d",&x);
if(x<0)
continue;
res=sqrt(x);
printf("\n Square root=%d\n",res);
}

getch();
return 0;
}

/* OUTPUT:

99
How many numbers you want to enter: 3

Enter only positive number:-6

Enter only positive number:4

Square root=2

Enter only positive number:9

Square root=3

*/

/*PROGRAM TO PRINT MULTIPLICATION TABLE */

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

/*PROGRAM TO PRINT MULTIPLICATION TABLE FOR 1-5 */

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

//Program to check whether the given number is binary or not

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

// Program to print number of 1's in a decimal equivalent binary


number

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

int main()

103
{
unsigned int num;
int count=0,temp;

printf("Enter the unsigned integer:");


scanf("%d",&num);

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

num of 1's in 15 binary equivalent is:4

*/

// PROGRAM TO CONVERT BINARY NUMBER TO DECIMAL

#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++;
}

printf("\n Decimal equal value=%d",sum);


getch();
return 0;
}

/* OUTPUT:
Enter a binary number 101

Decimal equal value=5


*/

// PROGRAM TO CONVERT BINARY NUMBER TO HEXADECIMAL

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

int main()
{
int temp=1,r,num,sum=0,i,j=0,arr[10];

printf("Enter the Binary number:");


scanf("%d",&num);

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:

Enter the Binary number:11111010


Hexadecimal equivalent= FA

*/

// PROGRAM TO CONVERT BINARY NUMBER TO OCTAL

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

printf ("\noctalal Equivalent for %d is %d",n,octal);

getch();
return 0;
}

/* OUTPUT:

Enter the Binary number:101111

octalal Equivalent for 101111 is 57


*/

/* PROGRAM TO ADD TWO BINARY NUMBERS */

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

*/

/* PROGRAM TO MULTIPLY TWO BINARY NUMBERS */

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

108
int fun(int x, int y);
int main()
{
int x,y,mul=0;
int digit,f=1;

printf("Enter the first binary number: ");


scanf("%d", &x);
printf("Enter the second binary number: ");
scanf("%d", &y);

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

int fun(int x, int y)


{
int i=0,rem=0,sum[30];
int temp=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

*/

/* PROGRAM TO FIND NUMBER OF 1'S IN A GIVEN NUMBER FROM


ITS BINARY */

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

int main()
{
int n,count=0,temp;
//clrscr();

printf("Enter a number \n");


scanf("%d",&n);
temp=n;

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

*/

// PROGRAM TO CONVERT DECIMAL NUMBER TO BINARY

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

int main()
{
int rem,i=1,binary=0,n,temp;

printf("Enter decimal number:");


scanf("%d",&n);

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
*/

/* PROGRAM TO CONVERT DECIMAL TO HEXADECIMAL NUMBER


*/

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

int main()
{
int n,rem,quotient,i=1,k,temp;
char hex[100];

printf("Enter any decimal number: ");


scanf("%d",&n);

quotient=n;

111
while(quotient>0)
{
temp=quotient%16;

// convert integer into character


if(temp<10)
temp=temp+48;
else
temp=temp+55;
hex[i]=temp;
quotient=quotient/16;
i++;
}

printf("Hexadecimal value of decimal number %d is: ",n);


for(k=i-1;k>0;k--)
printf("%c",hex[k]);

getch();
return 0;
}

/* OUTPUT:
Enter any decimal number: 1515
Hexadecimal value of decimal number 1515 is: 5EB
*/

/* PROGRAM TO CONVERT DECIMAL TO OCTAL NUMBER */

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

int main()
{
int n,rem,quotient,octalNumber[50],i=1,k;

printf("Enter the decimal number: ");


scanf("%d",&n);
quotient=n;

while(quotient>0)
{
octalNumber[i]=quotient%8;
quotient=quotient/8;
i++;

112
}

printf("Equivalent octal value of decimal number %d is: ",n);


for(k=i-1;k>0;k--)
printf("%d", octalNumber[k]);

getch();
return 0;
}

/* OUTPUT:
Enter the decimal number: 25
Equivalent octal value of decimal number 25 is: 31

*/

/* C Program to convert Decimal number to Roman */

#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

Roman Number is: XXIV

115
*/

// PROGRAM TO CONVERT HEXADECIMAL NUMBER TO DECIMAL

#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

printf("\nEnter the hexadecimal number: ");


gets(hex_num);

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:

Enter the hexadecimal number: FA


hexadecimal conversion of FA to decimal is: 250

*/

// PROGRAM TO CONVERT OCTAL NUMBER TO DECIMAL

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

int main()
{
int n,dec=0,i=0,temp;

printf("Enter any octal number:");


scanf("%d",&n);
temp=n;

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

*/

// PROGRAM TO CONVERT ROMAN NUMBER TO DECIMAL

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

int fun(char c);

117
int main()
{
char num[1000];
int i=0;
long int t=0;

printf("Enter any roman number(Valid digits are I,V,X,L,C,D,M):");


scanf("%s",num);

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:

Enter any roman number(Valid digits are I, V, X, L, C, D, M):XIV


Decimal val is:14

*/

//PROGRAM TO PRINT SQUARE,CUBE,SQUARE ROOT,1/n VALUES


FOR 1-10

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

int main( )
{
int i;
//clrscr( );

printf("N \t SQUARE \t CUBE \t SQUARE ROOT \t 1/n \n");


printf("------------------------------------------------------\n");
for(i=1;i<=10;i++)

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

1 1 1.0000 1.00 1.00


2 4 8.0000 1.41 0.50
3 9 27.0000 1.73 0.33
4 16 64.0000 2.00 0.25
5 25 125.0000 2.24 0.20
6 36 216.0000 2.45 0.17
7 49 343.0000 2.65 0.14
8 64 512.0000 2.83 0.13
9 81 729.0000 3.00 0.11
10 100 1000.0000 3.16 0.10
*/

//PROGRAM TO FIILL ENTIRE SCREEN WITH SMILING FACE

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

/* PROGRAM TO PRINT COMBINATIONS OF X,Y AND Z */

#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

/* PROGRAM TO FIND SUM=1+2+3+............+n */

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

/* PROGRAM TO FIND SUM=1+3+5............+n */

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

/* PROGRAM TO FIND SUM=1+2+4............+n */

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

/* PROGRAM TO FIND SUM=1+x+x2+x3+............+n */

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

/* PROGRAM TO FIND SUM=-1/1!+2/2!-3/3!............ */

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

int fact(int n);

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

*/

/* PROGRAM TO FIND SUM=1+x^2/2!+x^2/4!............+x^2/n!


where n is even */

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

int fact(int n);

int main()
{
int i,n,x;
double temp,sum=1;
//clrscr();

printf("Enter n and x values \n");


scanf("%d%d",&n,&x);

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
*/

/* PROGRAM TO FIND SUM=1+x^3/3!+x^5/5!............+x^n/n!


where n is even */

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

int fact(int n);

126
int main()
{
int i,n,x;
double temp,sum=0;
//clrscr();

printf("Enter n and x values \n");


scanf("%d%d",&n,&x);

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
*/

/* PROGRAM TO FIND SUM=1+1/2!+1/3!............+1/n! */

#include<stdio.h>

127
#include<conio.h>
#include<math.h>

int fact(int n);

int main()
{
int i,n;
double sum=0;
//clrscr();

printf("Enter n value \n");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1.0/fact(i));
}
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 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();

printf("Enter n and x values:");


scanf("%d%d",&n,&x);
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
sum=sum+pow(x,i)/f;
}
printf("\n Exponentional Sum=%f",sum);

getch();
return 0;
}

/* OUTPUT:
Enter n and x values:3 1

Exponentional Sum=2.666667
*/

/* PROGRAM TO FIND SUM=1^2+2^2+3^2+4^2+............+n^2


*/

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

int main()
{

129
int i,n,sum=0;
//clrscr();

printf("Enter a number \n");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+pow(i,2);
}
printf("\n Sum=%d",sum);

getch();
return 0;
}

/* OUTPUT:
Enter a number 4
Sum=30
*/

/* PROGRAM TO FIND SUM=1^3+2^3+3^3+4^3+............+n^3


*/

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

int main()
{
int i,n;
double sum=0;
//clrscr();

printf("Enter a number \n");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+pow(i,3);
}
printf("\n Sum=%f",sum);

getch();
return 0;
}

/* OUTPUT:
Enter a number 5

130
Sum=225.000000
*/

/* PROGRAM TO FIND SUM=1+2^2+4^2+6^2+............ */

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

/* PROGRAM TO FIND SUM=1+3^2+5^2+7^2+............ */

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

/* PROGRAM TO FIND 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,n,x;
double sum=1;
//clrscr();

printf("Enter n and x values:");


scanf("%d%d",&n,&x);
for(i=1;i<=n;i++)
{
sum=sum+pow(x,i)/i;
}
printf("\n Sum=%f",sum);

getch();
return 0;
}

/* OUTPUT:
Enter n and x values:4 1

Sum=3.083333

*/

/* PROGRAM TO FIND SUM=1+1/4+1/9+1/16+............ */

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

/* PROGRAM TO FIND COS SERIES SUM=1-x^2/2!+x^4/4!-


x^6/6!+x^8/8!............ */

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

int main()
{
int x,i,j,n,f=1,k=2;
float sum=1;

//clrscr();

printf("Enter n and x values:");


scanf("%d%d",&n,&x);
for(i=2;i<=n;i+=2)
{
f=1;
if(k%2==0)
{
for(j=1;j<=i;j++)
f=f*j;

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

SUM OF GIVEN SERIES=0.500000

*/

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;

printf("Enter number of terms");


scanf("%d",&n);
printf("\nEnter the value of x:");
scanf("%f",&x);
r=((x*3.1415)/180);
temp=r;
sum=r;
135
i=2;

for(k=2;k<=n;k++)
{
temp=(temp*(-1)*r*r)/(i*(i+1));
sum=sum+temp;
i=i+2;
}

printf("\nSUM OF THE SINE SERIES=%5f",sum);

getch();
return 0;
}

/* OUTPUT:
Enter number of terms:2

Enter the value of x:1

SUM OF THE SINE SERIES=0.017452

*/

PATTERN PROGRAMS

/* PROGRAM TO PRINT FOLLOWING PATTERN


1
22
333
4444
*/

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

int main()
{
int i,j,n;
//clrscr();

printf("Enter n value for number of lines \n");


scanf("%d",&n);
for(i=1;i<=n;i++)

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
*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


1
1 2
1 2 3
1 2 3 4
*/

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

int main()
{
int i,j,n;
//clrscr();

printf("Enter n value for number of lines \n");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%4d",j);
}
printf("\n");
}

137
getch();
return 0;
}

/* OUTPUT:
Enter n value for number of lines
4
1
1 2
1 2 3
1 2 3 4
*/

// RPOGRAM TO PRINT DIAMOND PATTERN

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

int main()
{
int n,c,k,sp=1; //sp is for space

printf("Enter number of lines:");


scanf("%d",&n);

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:

Enter number of lines:7

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

*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


4 3 2 1
4 3 2
4 3
4
*/

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

int main()
{
int i,j,n;
//clrscr();

printf("Enter n value for number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=4;j>=i;j--)
{
printf("%4d",j);

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
*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


1
1 2
1 2 3
1 2 3 4

*/

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

int main()
{
int i,x,n,k;
//clrscr();

printf("Enter n value for number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=30-i;k++)
printf(" ");
for(x=1;x<=i;x++)
{
printf(" %3d",x);
}
printf("\n");
}

getch();

140
return 0;
}

/* OUTPUT:
Enter n value for number of lines:4
1
1 2
1 2 3
1 2 3 4

*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


1 2 3 4
1 2 3
1 2
1
*/

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

int main()
{
int i,j,n;
//clrscr();

printf("Enter n value for number of lines:");


scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%4d",j);
}
printf("\n");
}

getch();
return 0;
}

/* OUTPUT:
Enter n value for number of lines:4

1 2 3 4
1 2 3

141
1 2
1
*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


1
2 3
4 5 6
7 8 9 10
*/

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

int main()
{
int i,j,n,k=0;
//clrscr();

printf("Enter n value for number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{

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
*/

/* PROGRAM TO PRINT FOLLOWING PATTERN

142
*
* *
* * *
* * * *
* * * * *
*/

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

int main()
{
int line,star,n;
//clrscr();

printf("Enter n value for number of lines \n");


scanf("%d",&n);
for(line=1;line<=n;line++)
{
for(star=1;star<=line;star++)
{
printf("\t*");
}
printf("\n");
}

getch();
return 0;
}

/* OUTPUT:
Enter n value for number of lines
5
*
* *
* * *
* * * *
* * * * *
*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


1
2 2
3 3 3
4 4 4 4

*/

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

int main()
{
int i,x,n,k;
//clrscr();

printf("Enter n value for number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=30-i;k++)
printf(" "); //double space
for(x=1;x<=i;x++)
{
printf(" %3d",i);
}
printf("\n");
}

getch();
return 0;
}

/* OUTPUT:
EnEnter n value for number of lines:5
1
2 2
3 3 3
4 4 4 4

*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4

*/

#include<stdio.h>

144
#include<conio.h>

int main()
{
int i,x,y,n,k;
//clrscr();

printf("Enter n value for number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=30-i;k++)
printf(" "); //double space
for(x=i;x<=2*i-1;x++)
printf("%3d",x);
for(y=2*i-2;y>=i;y--)
printf("%3d",y);
printf("\n");
}

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

*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


1
0 1
1 0 1
0 1 0 1
*/

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

int main()

145
{
int i,j,zero=0,one=1,n,m;
//clrscr();

printf("Enter n value for number of lines:");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
for(j=1;j<=i/2;j++)
printf("%2d%2d",zero,one);
}
else
{
for(m=1;m<=i;m++)
{
if(m%2!=0)
printf("%2d",one);
else
printf("%2d",zero);
}
}
printf("\n");
}

getch();
return 0;
}

/* OUTPUT:
EnEnter n value for number of lines:4
1
0 1
1 0 1
0 1 0 1

*/

/* PROGRAM TO PRINT FOLLOWING PATTERN


*********
*******
*****
***
*
*/

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

*/

/*PROGRAM TO PRINT PASCAL TRIANGLE */

#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

*/

/*PROGRAM TO PRINT PASCAL TRIANGLE USING FACTORIAL*/

/* 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;

printf("ENTER NUMBER OF ROWS:");


scanf("%d", &row);
for(i=0;i<row;i++)
{
for(j=0;j<=(row-i-1);j++)
printf(" ");
for(j=0;j<=i;j++)
{
res=fact(i)/(fact(j)*fact(i-j));
printf("%d ",res); //take single space
}
printf("\n");
}

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

/* PROGRAM TO CALCULATE THE SUM OF ELEMENTS OF A GIVEN


1-D ARRAY */

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

// Program to print the array elements and their addresses


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

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:

Address of a[0] is 65506 and value = 10


Address of a[1] is 65508 and value = 20
Address of a[2] is 65510 and value = 23
Address of a[3] is 65512 and value = 54
Address of a[4] is 65514 and value = 85
Address of a[5] is 65516 and value = 6
Address of a[6] is 65518 and value = 7
Address of a[7] is 65520 and value = 8
Address of a[8] is 65522 and value = 9
Address of a[9] is 65524 and value = 10

*/

/* PROGRAM TO CALCULATE THE SUM AND AVERAGE OF


ELEMENTS OF A GIVEN 2-D ARRAY */

#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

Enter 3X3 matrix values


100
001
123

Given 2-D array elements sum=8 and average=0.888889*/

/* PROGRAM TO ILLUSTRATE 3D ARRAYS */

#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}
},
};

printf("3D Array Elements \n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf("%d\t",array[i][j][k]);
}
printf("\n");
}
printf("\n");
}

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

*/

/* PROGRAM TO FIND SUM OF EVEN & ODD NUMBERS OF A GIVEN


1-D ARRAY ELEMENTS */

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

*/

/* PROGRAM TO COUNT POSITIVE,NEGATIVE, EVEN & ODD


NUMBERS OF A GIVEN 1-D ARRAY ELEMENTS */

#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

Enter 4 numbers:25 -56 -48 74

Total number of even numbers=3


Total number of odd numbers=1
Total number of positive numbers=2
Total number of negative numbers=2

*/

/* PROGRAM TO FIND SUM OF EVEN & ODD PLACE NUMBERS OF A


GIVEN 1-D ARRAY */

#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

*/

/* PROGRAM TO PRINT ELEMENTS IN REVERSE ORDER OF A


GIVEN 1-D ARRAY */

#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]);
}

printf("\n Given 1-D array elements in reverse order is\n");


for(i=n-1;i>=0;i--)
printf("%5d",a[i]);

getch();
return 0;
}
/* OUTPUT:
Enter total number of elements
4

Enter 4 numbers:10 20 30 40

Given 1-D array elements in reverse order is


40 30 20 10
*/

// Program to find out biggest number in an array

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

printf("\n Enter %dX%d Matrix:\n",rows,cols);


for(i=1;i<=rows;i++)
{
for(j=1;j<=cols;j++)
{
scanf("%d",&a[i][j]);
}
}

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

printf("\n Biggest number in the above matrix=%d",big);


getch();
return 0;
}

/* OUTPUT:
Enter number of rows and columns:3 3

Enter 3X3 Matrix:


123
456
789

Biggest number in the above matrix=9

*/

/*PROGRAM TO FIND SECOND SMALLEST & LARGEST NUMBER IN


A 1-D ARRAY */

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

printf("\n Second largest number=%d",x[n-2]);


printf("\n Second smallest number=%d",x[1]);

getch();
return 0;
}
/* OUTPUT:

Enter total number of elements:5


Enter the elements:15 32 96 87 50

Numbers before sort: 15 32 96 87 50


Numbers after sort: 15 32 50 87 96
Second largest number=50
Second smallest number=87
*/

/* PROGRAM TO FIND SECOND SMALLEST NUMBER WITHOUT


SORTING */

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

Enter total number of elements:5


Enter the elements:6 9 4 3 7

Second smallest number=4


*/

/* PROGRAM TO COPY ONE ARRAY ELEMENTS TO ANOTHER


ARRAY */

#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]);
}

printf("Array A values are:");


for(i=0;i<n;i++)
{
printf("%5d",a[i]);
}

printf("\n After copy Array B values are:");


for(i=0;i<n;i++)
{
b[i]=a[i];
printf("%5d",b[i]);
}
getch();
return 0;
}
/* OUTPUT:
Enter maximum range for an array:4

Enter 4 numbers: 50 87 465 10


Array A values are: 50 87 465 10
Aftercopy Array B values are: 50 87 465 10

*/

/* PROGRAM TO COMPARE TWO ONE-DIMENSIONAL ARRAYS */

#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]);
}

printf("\n Enter %d numbers for array-B:",n);


for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}

printf("\n Array A values are:\n");


for(i=0;i<n;i++)
{
printf("%5d",a[i]);
}

printf("\n Array B values are:\n");


for(i=0;i<n;i++)
{
printf("%5d",b[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

Enter 3 numbers for array-B:10 20 30

Array A values are:


10 20 30
Array B values are:
10 20 30
Given array elements are same

*/

/* PROGRAM TO CALCULATE THE AVERAGE OF ELEMENTS OF A


GIVEN 1-D ARRAY */

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

/* PROGRAM TO DISPLAY SIMILAR ELEMENTS OF A GIVEN 1-D


ARRAY */

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

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

printf("Enter the position of the element to split the array:");


scanf("%d",&pos);

for(i=0;i<pos;i++)
{
arr[n]=arr[0];
for(j=0;j<n;j++)
{
arr[j]=arr[j+1];
}
}

printf("The resultant array is:\n");


for(i=0;i<n;++i)
{
printf("%5d", arr[i]);
}

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

*/

// PROGRAM TO PRINT UNION OF GIVEN TWO SETS

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

printf("\n Enter %d numbers:",n1);


for(i=0;i<n1;i++)
scanf("%d",&array1[i]);

printf("\n Enter range for set-2:");


scanf("%d",&n2);

printf("\n Enter %d numbers:",n2);


for(i=0;i<n2;i++)
scanf("%d",&array2[i]);

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++;
}
}

printf("\n Union of above two sets is:\n");


for(i=0;i<k;i++)
{
printf("\t%d",c[i]);
}

getch();
return 0;
}

/* OUTPUT:

Enter range for set-1:4

Enter 4 numbers:10 20 30 40

Enter range for set-2:3

Enter 3 numbers:10 40 50

Union of above two sets is:


10 20 30 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;

printf("\n After grouping all 0's and 1's output is:");


for(i=0;i<n;i++)
printf("%3d", a[i]);

getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:6

Enter 6 numbers(only 0's and 1's):


101110

After grouping all 0's and 1's output is:


1 1 1 1 0 0

*/

/* PROGRAM TO FIND MINIMUM,MAXIMUM NUMBER FROM A


GIVEN 1-D ARRAY */

#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];
}

printf("\n Minimum value=%d \n Maximum value=%d",min,max);


getch();
return 0;
}
/* OUTPUT:
Enter max range
5

Enter 5 numbers
10 20 30 40 50

Minimum value=10
Maximum value=50
*/

/* PROGRAM TO FIND MINIMUM,MAXIMUM NUMBER FROM A


GIVEN 2-D ARRAY */

#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];
}
}

printf("\n Minimum value=%d \n Maximum value=%d",min,max);


getch();
return 0;
}
/* OUTPUT:
Enter number of rows & coulums for matrix
33

Enter 3X3 matrix values


123
456
789

Minimum value=1
Maximum value=9
*/

/* PROGRAM TO PRINT MINIMUM SUM ELEMENTS IN A GIVEN 1-D


ARRAY */

#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

Enter 5 numbers:10 20 -30 40 50


The two elements whose sum is minimum are 20 and -30

*/

// Program to find if a given number X appears more than N/2


times in array

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

int fun(int arr[],int n,int x);

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

printf("\n Enter searching number:");


scanf("%d",&x);

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

int fun(int arr[],int n,int x)


{
int i,index;
index=n%2?n/2:(n/2+1);
for(i=0;i<index;i++)
{
/* check if x is presents more than n/2 times */
if(arr[i]==x&&arr[i+n/2]==x)
return 1;
}
return 0;
}

/* OUTPUT:
Enter size of array:4

Enter 4 elements
1112

Enter searching number:1


The given no 1 appears more than 2 times in array

*/

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;

printf("After inserting value the array elements are:\n");


for(i=0;i<=n;i++)
printf("%d\t",x[i]);

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
*/

// PROGRAM TO PRINT INTERSECTION OF GIVEN TWO SETS

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

printf("\n Enter %d numbers:",n1);


for(i=0;i<n1;i++)
scanf("%d",&array1[i]);

printf("\n Enter range for set-2:");


scanf("%d",&n2);

printf("\n Enter %d numbers:",n2);


for(i=0;i<n2;i++)
scanf("%d",&array2[i]);

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 range for set-1:4

Enter 4 numbers:10 20 30 40

Enter range for set-2:3

Enter 3 numbers:10 40 60

Intersection of above two sets is:


10 40

*/

/* PROGRAM TO PRINT MAXIMUM DIFFERENCE BETWEEN


ELEMENTS IN A GIVEN 1-D ARRAY */

#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

*/

/* PROGRAM TO PRINT SQUARE AND CUBE OF A GIVEN 1-D


ARRAY ELEMENTS */

#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]);
}

printf("\n ARRAY ELEMENT \t SQUARE \t CUBE");


printf("\n ---------------------------------------");

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

ARRAY ELEMENT SQUARE CUBE


---------------------------------------
10 100 1000
20 400 8000
30 900 27000
40 1600 64000
*/

// PROGRAM TO DELETE AN ELEMENT IN THE 1-D ARRAY

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

printf("After deleting array elements are:\n");


for(i=0;i<n-1;i++)
printf("%d\t",x[i]);
}

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

*/

/* Program to put even and odd location elements in separate


arrays */

#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];
}

printf("\n EVEN LOCATION ARRAY IS:");


for(i=0;i<j;i++)
printf("%5d",evenarray[i]);

printf("\n ODD LOCATION ARRAY IS:");


for(i=0;i<k;i++)
printf("%5d",oddarray[i]);

180
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:5

Enter 5 numbers:10 20 30 40 50

EVEN LOCATION ARRAY IS: 10 30 50


ODD LOCATION ARRAY IS: 20 40

*/

/* PROGRAM TO DELETE DUPLICATE ELEMENT FROM A GIVEN 1-D


ARRAY */

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

printf("\n Array before deleting duplicate elements:",n);


for(i=0;i<n;i++)
printf("%4d",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
}
}

printf("\n Array after deleting duplicate elements:",n);


for(i=0;i<n;i++)
printf("%4d",a[i]);

getch();
return 0;
}
/* OUTPUT:
Enter max range of an array:5

Enter 5 numbers:21 63 58 21 63

Array before deleting duplicate elements:21 63 58 21 63


Array after deleting duplicate elements:21 63 58

*/

/* Program to interchange even and odd location elements in a 1-


D array */

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

printf("\n EXCHANGED ARRAY IS:");


for(i=0;i<n;i++)
printf("%5d",a[i]);

182
getch();
return 0;
}
/* OUTPUT:
Enter n value for array elements:5

Enter 5 numbers:10 20 30 40 50

EXCHANGED ARRAY IS: 20 10 40 30 50


*/

/* PROGRAM TO ADD GIVEN TWO MATRICES */

#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]);
}

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 THE GIVEN A MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]);
}

183
printf("\n");
}

printf("\n THE GIVEN B MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",b[i][j]);
}
printf("\n");
}

printf("\n ADDITION OF A+B IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]+b[i][j]); //For substraction replace + by -
}
printf("\n");
}
getch();
return 0;
}

/* OUTPUT:
Enter number of rows and columns
3 3

Enter 3X3 A matrix values


123
456
789

Enter 3X3 B matrix values


111
111
111

THE GIVEN A MATRIX IS


----------------------
1 2 3
4 5 6
7 8 9

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

*/

/* PROGRAM TO SUBTRACT GIVEN TWO MATRICES */

#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]);
}

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 THE GIVEN A MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)

185
{
printf("%4d",a[i][j]);
}
printf("\n");
}

printf("\n THE GIVEN B MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",b[i][j]);
}
printf("\n");
}

printf("\n ADDITION OF A+B IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]-b[i][j]); //For substraction replace + by -
}
printf("\n");
}
getch();
return 0;
}

/* OUTPUT:
Enter number of rows and columns
3 3

Enter 3X3 A matrix values


123
456
789

Enter 3X3 B matrix values


111
111
111

THE GIVEN A MATRIX IS


----------------------

186
1 2 3
4 5 6
7 8 9

THE GIVEN B MATRIX IS


----------------------
1 1 1
1 1 1
1 1 1

ADDITION OF A+B IS
----------------------
0 1 2
3 4 5
6 7 8

*/

/* PROGRAM ON MATRIX MULTIPLICATION OF GIVEN TWO


MATRICES */

#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]);
}

printf("Enter B MATRIX number of rows and columns \n");


scanf("%d%d",&row2,&col2);

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

printf("\n THE GIVEN A MATRIX IS \n");


printf("----------------------------\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}

printf("\n THE GIVEN B MATRIX IS \n");


printf("----------------------------\n");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("%4d",b[i][j]);
}
printf("\n");
}

printf("\n THE MULTIPLICATION IS \n");


printf("----------------------------\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
c[i][j]=0;
for(k=0;k<row2;k++)
c[i][j]+=a[i][k]*b[k][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

Enter 3X3 A matrix values


123
456
789
Enter B MATRIX number of rows and columns
33

Enter 3X3 B matrix values


100
010
001

THE GIVEN A MATRIX IS


----------------------------
1 2 3
4 5 6
7 8 9

THE GIVEN B MATRIX IS


----------------------------
1 0 0
0 1 0
0 0 1

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;

printf("Enter number of rows and columns of the matrix:");


scanf("%d%d", &row,&col);

printf("Enter %dX%d matrix elements:\n",row,col);


for(i=0;i<row;++i)
{
for(j=0;j<col;++j)
{
scanf("%d",&a[i][j]);
b[i][j]=a[i][j];
}
}

printf("The given matrix is:\n");


for(i=0;i<row;++i)
{
for(j=0;j<col;++j)
{
printf("%5d",a[i][j]);
}
printf("\n");
}

printf("After arranging rows in ascending order\n");


for(i=0;i<row;++i)
{
for(j=0;j<col;++j)
{
for(k=(j+1);k<col;++k)
{
if(a[i][j]>a[i][k])
{
temp=a[i][j];
a[i][j]=a[i][k];
a[i][k]=temp;
}

190
}
}
}

for(i=0;i<row;++i)
{
for(j=0;j<col;++j)
{
printf("%5d",a[i][j]);
}
printf("\n");
}

printf("After arranging the columns in descending order \n");


for(j=0;j<col;++j)
{
for(i=0;i<row;++i)
{
for(k=i+1;k<row;++k)
{
if(b[i][j]<b[k][j])
{
temp=b[i][j];
b[i][j]=b[k][j];
b[k][j]=temp;
}
}
}
}

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

*/

// PROGRAM TO PRINT LOWER TRIANGULAR MATRIX

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

printf("Given matrix is\n");


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%3d",a[i][j]);
printf("\n");
}

printf("\n Lower triangular matrix:\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:

Enter number of Rows & Columns of a Matrix A:3 3


Enter the A[0][0] location value:10
Enter the A[0][1] location value:20
Enter the A[0][2] location value:30
Enter the A[1][0] location value:40
Enter the A[1][1] location value:50
Enter the A[1][2] location value:60
Enter the A[2][0] location value:70
Enter the A[2][1] location value:80
Enter the A[2][2] location value:90

Given Matrix is:


10 20 30
40 50 60
70 80 90

Lower triangular matrix:


10
40 50
70 80 90
*/

// PROGRAM TO ADD ELEMENTS OF LOWER TRIANGULAR MATRIX

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

int main()

193
{
int i,j,a[10][10],sum,m,n;

printf("Enter the number of Rows:");


scanf("%d",&m);

printf("\n Enter the number of Columns:");


scanf ("%d",&n);

printf("\n Enter %dX%d matrix values",m,n);


for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}

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];
}

printf("\n The Addition of Lower Triangle Elements=%d",sum);

getch();
return 0;
}

/* OUTPUT:
Enter the number of Rows:3

Enter the number of Columns:3

Enter 3X3 matrix values


123
456
789

The Addition of Lower Triangle Elements=19

*/

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

printf("\n 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");
}

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

Enter 3X3 matrix values


100
230
456

THE GIVEN MATRIX IS


---------------------------
1 0 0
2 3 0
4 5 6

Given matrix is lower triangular matrix

*/

// PROGRAM TO ADD EACH ROW AND COLUMN OF A GIVEN


MATRIX

#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

Enter 0 row 0 column value:10

Enter 0 row 1 column value:20

Enter 0 row 2 column value:30

Enter 1 row 0 column value:40

Enter 1 row 1 column value:50

Enter 1 row 2 column value:60

Enter 2 row 0 column value:70

Enter 2 row 1 column value:80

Enter 2 row 2 column value:90

197
EACH ROW and COLUMN SUM IS

10 20 30 = 60
40 50 60 =150
70 80 90 =240
-----------------
120 150 180

*/

/* PROGRAM TO SORT ROWS OF A GIVEN MATRIX */

#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]);
}

printf("\n BEFORE 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");
}

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

Enter 3X3 matrix:1 2 3


30 20 10
25 14 64

BEFORE SORTING ROWS THE GIVEN MATRIX IS


---------------------------------------
1 2 3
30 20 10
25 14 64

AFTER SORTING ROWS THE GIVEN MATRIX IS


---------------------------------------
1 2 3
10 20 30
14 25 64

*/

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

printf("\n EACH ROW SUM IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
sum=0;
for(j=0;j<col;j++)
{
sum=sum+a[i][j];
}
printf("%\n %d row sum=%d",i,sum);
}

getch();
return 0;
}

/* OUTPUT:
Enter number of rows and columns
3 3Enter number of rows and columns
33

Enter 3X3 matrix values


123
456
421

EACH ROW SUM IS

200
---------------------------

0 row sum=6
1 row sum=15
2 row sum=7

*/

// PROGRAM TO CHECK WHETEHR A GIVEN MATRIX IS SPARSE


MATRIX OR NOT

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

int main()
{
int arr[6][6],i,j,row,col,counter=0;

printf("Enter number of rows:");


scanf("%d",&row);
printf("Enter number of columns:");
scanf("%d",&col);

printf("Enter %dX%d matrix\n",row,col);


for(i=0;i<row;++i)
{
for(j=0;j<col;++j)
{
scanf("%d",&arr[i][j]);
if(arr[i][j]==0)
{
++counter;
}
}
}

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

*/

/* PROGRAM TO CHECK GIVEN MATRIX IS SYMMETRIC OR


ASYMETRIC */

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

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

printf("\n THE GIVEN A MATRIX IS \n");

202
printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}

printf("\n THE TRANSPOSE MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
trans[i][j]=a[j][i];
}
}

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

printf("Given matrix is a symmtric");


}
getch();
return 0;

203
}

/* OUTPUT:
Enter number of rows and columns
22

Enter 2X2 matrix values1 2


21

THE GIVEN A MATRIX IS


---------------------------
1 2
2 1

THE TRANSPOSE MATRIX IS


---------------------------
1 2
2 1
Given matrix is a symmtric

*/

/* PROGRAM TO PRINT TRANSPOSE OF A GIVEN MATRIX */

#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]);
}

printf("\n THE GIVEN A MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)

204
{
printf("%4d",a[i][j]);
}
printf("\n");
}

printf("\n THE TRANSPOSE MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[j][i]);
}
printf("\n");
}

getch();
return 0;
}

/* OUTPUT:
Enter number of rows and columns
33

Enter 3X3 matrix values1 5 3 6 4 2 10 20 30

THE GIVEN A MATRIX IS


---------------------------
1 5 3
6 4 2
10 20 30

THE TRANSPOSE MATRIX IS


---------------------------
1 6 10
5 4 20
3 2 30

*/

// PROGRAM TO PRINT UPPER TRIANGULAR MATRIX

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

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

printf("Given matrix is\n");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%3d",a[i][j]);
printf("\n");
}

printf("\n Upper triangular matrix:\n");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i>j)
a[i][j]=0;
printf("%5d",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
/* OUTPUT:

Enter number of Rows & Columns of a Matrix:3 3


Enter the 3X3 matrix:1 2 3 4 5 6 7 8 9
Given matrix is
1 2 3
4 5 6
7 8 9

Upper triangular matrix:


1 2 3
0 5 6
0 0 9

206
*/

// PROGRAM TO ADD ELEMENTS OF UPPER TRIANGULAR MATRIX

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

int main()
{
int i,j,a[10][10],sum,m,n;

printf("Enter the number of Rows:");


scanf("%d",&m);

printf("\n Enter the number of Columns:");


scanf ("%d",&n);

printf("\n Enter %dX%d matrix values",m,n);


for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}

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];
}

printf("\n The Addition of Upper Triangle Elements=%d",sum);

getch();
return 0;
}

/* OUTPUT:
Enter the number of Rows:3

Enter the number of Columns:3

207
Enter 3X3 matrix values
123
456
789

The Addition of Upper Triangle Elements=11

*/

/* PROGRAM TO CHECK WHETHER A GIVEN MATRIX IS UPPER


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

printf("\n 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");
}

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

Enter 3X3 matrix values


123
064
008

THE GIVEN MATRIX IS


---------------------------
1 2 3
0 6 4
0 0 8

Given matrix is upper triangular matrix

*/

/* PROGRAM TO SORT COLUMNS OF A GIVEN MATRIX */

#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]);
}

printf("\n BEFORE 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");
}

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

Enter 2X2 matrix:10 20


56

BEFORE SORTING COLUMNS THE GIVEN MATRIX IS


------------------------------------------
10 20
5 6

AFTER SORTING COLUMNS THE GIVEN MATRIX IS


-------------------------------------------
5 6
10 20

*/

/* PROGRAM TO PRINT SUM OF EACH COLUMN 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]);
}

printf("\n EACH COLUMN SUM IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
sum=0;
for(j=0;j<col;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

Enter 3X3 matrix values


123
456
789

EACH COLUMN SUM IS


---------------------------

0 column sum=12
1 column sum=15
2 column sum=18

*/

/* PROGRAM TO COMPARE GIVEN TWO MATRICES */

#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 THE GIVEN A MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}

printf("\n THE GIVEN B MATRIX IS \n");


printf("---------------------------\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%4d",b[i][j]);
}
printf("\n");
}

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

printf("A and B matrices are same");

getch();
return 0;

213
}

/* OUTPUT:
Enter number of rows and columns
22

Enter 2X2 A matrix values:10 20 30 40

Enter 2X2 B matrix values:10 20 15 40

THE GIVEN A MATRIX IS


---------------------------
10 20
30 40

THE GIVEN B MATRIX IS


---------------------------
10 20
15 40
---------------------------
A and B matrices are not same

*/

// Progarm to find determinant of a given 2X2 matrix

#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

DETERMINANT OF GIVEN MATRIX=-3

*/

/* PROGRAM TO FIND DETERMINANT OF A GIVEN 3X3 MATRIX */

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

int main()
{
int a[3][3],i,j;
int det=0;

printf("Enter the 3X3 matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\n Given matrix is\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}

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

*/

/* PROGRAM TO FIND SUM OF THE DIAGONAL ELEMENTS OF A


GIVEN MATRIX */
#include<stdio.h>
#include<conio.h>

int main()
{
int a[4][4],i,j,sum=0;
//clrscr();

printf("Enter elements for 3x3 matrix\n\n");


for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);
}

printf("\nElements in matrix order\n");


for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("\nADDITION OF DIAGONAL ELEMENTS=");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==j)
sum=sum+a[i][j];
}

216
}
printf("%d",sum);
getch();
return 0;
}
/* OUTPUT:
Enter elements for 3x3 matrix
10 5 4 98 25 30 2 56 11

Elements in matrix order


10 5 4
98 25 30
2 56 11

ADDITION OF DIAGONAL ELEMENTS=46


*/

// PROGRAM TO INTERCHANGE DIAGONAL ELEMENTS OF A


MATRIX

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

printf("\nGiven matrix is\n");

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

printf("\n After interchanging diagonal elements result matrix is:\n");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
}

getch();
return 0;
}
/* OUTPUT:

Enter number of Rows & Columns of a Matrix:3 3


Enter the 3X3 matrix:10 20 30 40 50 60 70 80 90

Given matrix is
10 20 30
40 50 60
70 80 90

After interchanging diagonal elements result matrix is:


30 20 10
40 50 60
90 80 70
*/

/* PROGRAM TO CHECK WHETHER A GIVEN MATRIX IS


INDENTITY MATRIX OR NOT */

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

printf("\n 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");
}

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

Enter 2X2 matrix values0 0


11

THE GIVEN MATRIX IS


---------------------------
0 0
1 1

Given matrix is not a identity matrix

*/

/* PROGRAM TO FIND INVERSE OF A GIVEN 3X3 MATRIX */

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

int main()
{
int arr[3][3],i,j;
float det=0;

printf("Enter 3X3 matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&arr[i][j]);

printf("\n Given matrix is\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",arr[i][j]);
printf("\n");
}

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

Inverse matrix is:


0.47 0.07 -0.20
-0.67 -0.67 1.00
-0.20 0.40 -0.20

*/

// PROGRAM TO GENERATE MAGIC SQUARE


// Sum of each row,column and diagonal elements is same

#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

The magic square for 3 x 3 is :


8 1 6

3 5 7

222
4 9 2

*/

// PROGRAM TO CHECK WHETHER A GIVEN MATRIX IS MAGIC


SQUARE OR NOT
// Sum of each row,column and diagonal elements is same

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

int main()
{
int size,arr[4][4],i,j,flag=0;
int sum,sumrow,sumcol;

//clrscr();

printf("Enter any 3X3 matrix:");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&arr[i][j]);
}

printf("Entered matrix is :\n");


for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("\t%d",arr[i][j]);
}
}

//sum of diagondal elements


sum=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(i==j)
sum=sum+arr[i][j];
}

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

// PROGRAM TO ADD TWO NUMBERS WITHOUT ARGUMENTS AND


WITHOUT RETURN VALUES

#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

Enter two numbers


10 20

Addition of 10 and 20 is 30
End of main
*/

// PROGRAM TO ADD TWO NUMBERS WITHOUT ARGUMENTS AND


WITH RETURN VALUES

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

void add(int x,int y)


{
printf("\n Addition of %d and %d is %d",x,y,x+y);
}

/* OUTPUT:
Enter two numbers
10 20

Calling method add


Addition of 10 and 20 is 30
End of main
*/

// PROGRAM TO ADD TWO NUMBERS WITHOUT ARGUMENTS AND


WITH RETURN VALUES

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

int add(int x,int y)


{
return(x+y);
}

/* OUTPUT:
Enter two numbers
10 20

Calling method add


Addition of 10 and 20 is 30
End of main
*/

/* PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER


USING FUNCTIONS */

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

int fact(int); //function prototype

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
*/

// PROGRAM ON WITH ARGUMENTS AND WITH RETURN VALUES

/* FINDING VALUE OF Ncr=N!/((N-r)!*r!) */

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

// PROGRAM ON WITH ARGUMENTS AND WITH RETURN VALUES

/* FINDING VALUE OF Npr=N!/((N-r)!) */

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

printf("Enter a binary number: ");


scanf("%d", &n);
gray=convert(n);
printf("\n The Gray of %d is %d",n,gray);

getch();
return 0;
}

int convert(int bin)


{
int x,y,res=0,i=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:

Enter a decimal number: 7

The binary equivalent of 7 is 111

231
*/

// Program to print the address of a user defined function

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

Address of function d=4199136


It is function

*/

// Program to return a pointer from the function

#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

*/

// PROGRAM ON FUNCTION WITH ARGUMENTS AND WITHOUT


RETURN VALUES
/* SWAPPING OF GIVEN TWO NUMBERS USING CALL BY VALUE
*/

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

void swap(int x,int y)


{
int temp;
temp=x;
x=y;
y=temp;

printf("\n AFTER SWAPPING in swap function x=%d and y=%d",x,y);


}

233
/* OUTPUT:
Enter two numbers
10 5

BEFORE SWAPPING a=10 and b=5


AFTER SWAPPING in swap function x=5 and y=10
AFTER SWAPPING in main a=10 and b=5
*/

// PROGRAM ON FUNCTION WITH ARGUMENTS AND WITHOUT


RETURN VALUES
/* SWAPPING OF GIVEN TWO NUMBERS USING CALL BY
REFERENCE */

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

void swap(int *x,int *y)


{
int temp;
temp=*x;
*x=*y;
*y=temp;

printf("\n AFTER SWAPPING in swap function x=%d and y=%d",*x,*y);


}

/* 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
*/

// Program to swap two variables using bitwise operators

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

void swap(int,int);

int main()
{
int a,b;

printf("Enter a,b values:");


scanf("%d%d",&a,&b);

swap(a,b);

getch();
return 0;
}

void swap(int x,int y)


{
printf("\n Before swapping x=%d and y=%d",x,y);
x=x^y;
y=x^y;
x=x^y;

printf("\n After Swapping x=%d and y=%d",x,y);


}

/* OUTPUT:

Enter a,b values:10 20

Before swapping x=10 and y=20


After Swapping x=20 and y=10

*/

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:

Enter a number: 123


Sum of the digits of a number 123 is 6

*/

// Program to find sum of N numbers using recursion

#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

*/

// PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER USING


RECURSION

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

// Program to find biggest number in a given array using


recursion

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

int large(int[], int, int);

int main()
{
int n,big,arr[20],i;

printf("Enter size of the array:");


scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d",&arr[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;
}

int large(int arr[], int n, int big)


{
if(n>=1)
{
if(arr[n]>big)
{
big=arr[n];
}
return(big=large(arr,n-1,big));
}
else
{
return big;
}
}

/* OUTPUT:

Enter size of the array:5


10 20 30 40 50

The big number in the arr is: 50

*/

// Program to convert given binary into gray code using recursion

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

int convert(int);

int main()
{
int n,gray;

printf("Enter a binary number: ");


scanf("%d", &n);
gray=convert(n);
printf("\n The Gray of %d is %d",n,gray);

239
getch();
return 0;
}

int convert(int bin)


{
int x,y,res=0,i=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:

Enter a binary number: 1000

The Gray of 1000 is 1100

*/

// Program to convert given decimal to binary using recursion

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

240
int convert(int);

int main()
{
int n,bin;

printf("Enter a decimal number: ");


scanf("%d", &n);
bin=convert(n);
printf("\n The binary equivalent of %d is %d",n,bin);

getch();
return 0;
}

int convert(int n)
{
if(n==0)
{
return 0;
}

else
{
return(n%2)+10*convert(n/2);
}
}

/* OUTPUT:

Enter a decimal number: 7

The binary equivalent of 7 is 111

*/

// Program to print Fibonacci series using recursion

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

Enter maximum number:10


Fibonacci series:
0 1 1 2 3 5 8 13 21 34

*/

// Program to find GCD of given two numbers using recursion

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

int gcd(int x,int y)


{
if(x==0)
return(y);
else
return(gcd(y%x,x));
}

/* OUTPUT:

ENTER TWO INTEGER VALUES:


10 5
THE GCD OF 10 and 5 IS:5

*/

/* PROGRAM TO FIND NCR VALUE USING RECURSION */

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

Enter n,r values:4 2

Result NCR=6.000000
*/

// Program to find whether a given number is prime or not using


recursion

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

int prime(int x,int i)


{
if(i==1)
return(1);
else
{
if(x%i==0)

244
return(0);
else
prime(x,i-1);
}
}

/* OUTPUT:

Enter a number: 4
4 is not a prime number

*/

// Program to find reverse of a given number using recursion

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

int rev(int);

int main()
{
int n;
//clrscr();
printf("\n Enter a number: ");
scanf("%d",&n);

printf("Reverse of a number %d is %d",n,rev(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

*/

// Program to find the length of the string using recursion

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

int findlength(const char *);

int main()
{
char str[50];
puts("Enter any string:");
gets(str);

printf ("String length=%d",findlength(str));


getch();
return 0;
}

int findlength(const char * s)


{
if(s==NULL||*s=='\0')
return 0;
else
return 1+findlength(s+1);
}

/* OUTPUT:
Enter any string:
strlen is a string function
String length=27

*/

// Program to copy one string to another using recursion

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

void copy(char [], char [], int);

246
int main()
{
char str1[20], str2[20];

puts("Enter string to copy: ");


gets(str1);

copy(str1,str2,0);

printf("The given string is: %s", str1);


printf("\n The copied string is: %s", str2);

getch();
return 0;
}

void copy(char str1[], char str2[], int i)


{
str2[i]=str1[i];
if(i==strlen(str1))
return;
copy(str1,str2,i+1);
}

/* OUTPUT:

Enter string to copy:


IT IS C
The given string is: IT IS C
The copied string is: IT IS C

*/

// Program to find the first capital letter in a string using


recursion

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

char caps_test(char *);

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

char caps_test(char *str)


{
static int i=0;
if(i<strlen(str))
{
if(isupper(str[i]))
{
return str[i];
}
else
{
i=i+1;
return caps_test(str);
}
}
else
return 0;
}

/* OUTPUT:

Enter any string:it is Example of Recursion


The first capital letter is:E

*/

// Program to find whether a given string is polyndrome or not

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

void test(char str[], int i)


{
int len;
len=strlen(str)-(i+1);
if(str[i]==str[len])
{
if(i+1==len||i==len)
{
printf("The given string is a palindrome\n");
return;
}
test(str,i+1);
}
else
{
printf("The given string is not a palindrome\n");
}
}

/* OUTPUT:
Enter any string:
LIRIL
The given string is a palindrome

*/

// Program print the reverse string using recursion

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

void reverse(char *str);

249
int main()
{
char str[50];
puts("Enter any string:");
gets(str);

puts("String in reverse order is:");


reverse(str);

getch();
return 0;
}

void reverse(char *str)


{
if(*str)
{
reverse(str+1);
printf("%c", *str);
}
}

/* OUTPUT:

Enter any string:


I AM RAGHU
String in reverse order is:
UHGAR MA I

*/

// Program to illustrate static variable

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

printf("\n i value is:%d and x value is:%d",i,x);


i++;
x++;
}

/* OUTPUT:

i value is:5 and x value is:5


i value is:6 and x value is:5
i value is:7 and x value is:5
i value is:8 and x value is:5
i value is:9 and x value is:5
i value is:10 and x value is:5
i value is:11 and x value is:5
i value is:12 and x value is:5
i value is:13 and x value is:5
i value is:14 and x value is:5

*/

// Program to illustrate register storage class

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

// Program to illustrate global variable

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

void fun1();
void fun2();
void fun3();

extern int x=10; //or int x=10;

int main()
{

printf("\n x value in main method is:%d",x);

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:

x value in main method is:10


x value in function-1 is:15
x value in function-2 is:25
x value in function-3 is:45

*/

POINTERS

// Program to add, multiply, subtract and divide two 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:

Enter value for a and b:20 10

Enter choice 1-Add 2-Subtract 3-Product 4-Division 5-Exit:1

Sum=30
Do you want to continue(y/n):y
Enter value for a and b:30 5

Enter choice 1-Add 2-Subtract 3-Product 4-Division 5-Exit:3

Product=150
Do you want to continue(y/n):

*/

//PROGRAM ON POINTER ADDRESS

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

/*printf("\n Address of i=%u",*k);


printf("\n Address of p=%u",k);
printf("\n Address of k=%u",&k);
printf("\n Value of k=%u",k);
printf("\n Value of i=%d",**k);*/
getch();
return 0;
}

/* 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
*/

// PROGRAM TO READ AND DISPLAY 1-D ARRAY CONTENTS


USING POINTERS

#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

Values in reverse order 50 40 30 20 10


Values in forward order 10 20 30 40 50

*/

// PROGRAM TO READ AND DISPLAY 2-D ARRAY CONTENTS


USING POINTERS AND FUNCTIONS

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

//void read(int(*)[],int,int);
//void display(int(*)[],int,int);

void read(int(*p)[3],int u,int v)


{
int i,j;
for(i=0;i<u;i++)
for(j=0;j<v;j++)
scanf("%d",(*(p+i)+j));
}

void display(int(*p)[3],int u,int v)


{
int i,j;

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
*/

// Program to concatenate two strings using pointers

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

printf("Before concatenation the strings are\n");


puts(s1);

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:

Enter second string: IT IS


Enter second string: A BOOK

Before concatenation the strings are


IT IS
A BOOK

Concatenation string is:IT IS A BOOK

*/

// Program to copy string using pointers

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

void copystring(char*,char*);

int main()
{
char*str1,*str2;
//clrscr();

printf("Enter any string:");


gets(str1);

258
copystring(str2,str1);
printf("\n Copied string=%s",str2);

getch();
return 0;
}

void copystring(char *dest,char *src)


{
while(*src!='\0')
*dest++=*src++;
*dest='\0';
return;
}

/* OUTPUT:

Enter any string: How are you?


Copied string=How are you?

*/

// Program to illustrate dynamic memory allocation


// Finding largest number from given numbers

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

int main()
{
int i,n;
int *value;

printf("Enter total number of elements:");


scanf("%d",&n);

value=(int*)calloc(n,sizeof(int)); /* Allocates the memory for 'n'


elements */

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

printf("\n Largest element=%d",*value);

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

*/

// Program to find length of the string using pointers

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

int main()
{
char str[50],*p;
int len=0;

puts("Enter any string:");


gets(str);

p=str;
while(*(p+len))
len++;

260
printf("Length of entered string=%d\n",len);

getch();
return 0;
}

/* OUTPUT:

Enter any string:


HE IS RAGHU
Length of entered string=11

*/

// Program to find sum of given two one-dimensional array


elements using malloc

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

int main()
{
int i,n,*a,*b,*c;

printf("Enter size for A and B arrays:");


scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
b=(int *)malloc(n*sizeof(int));

printf("Enter Elements of First array:\n");


for(i=0;i<n;i++)
{
scanf("%d",a+i);
}

printf("Enter Elements of Second array:\n");


for(i=0;i<n;i++)
{
scanf("%d",b+i);
}

c=(int *)malloc(n*sizeof(int));

for(i=0;i<n;i++)
{

261
*(c+i)=*(a+i)+*(b+i);
}

printf("Resultant array is:\n");


for(i=0;i<n;i++)
{
printf("%6d",*(c+i));
}

getch();
return 0;
}

/* OUTPUT:

Enter size for A and B arrays:4


Enter Elements of First array:
10 20 30 40
Enter Elements of Second array:
4567
Resultant array is:
14 25 36 47

*/

// PROGRAM ON POINTER OPERATIONS

#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

*/

// Program to print reverse string using pointers

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

int main()
{
char str[100],*p;
int i,len;

printf("Enter a string: ");


gets(str);
p=str;

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:

Enter a string: IT IS C PROGRAM


Reversed String: MARGORP C SI TI

*/

// Program to find sum of all the elements of given one


dimensional array

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

int main()
{
int a[10],i,sum=0,*ptr,n;

printf("Enter size of an array:");


scanf("%d",&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++;
}

printf("The sum of array elements is:%d",sum);

getch();
return 0;
}

/* OUTPUT:

Enter size of an array:3


10 20 30
The sum of array elements is:60

*/
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

Given string=How are you


Length of string with spaces=11
*/

/* PROGRAM TO PRINT CHARACTER ARRAY ADDRESS */

#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

*/

/* PROGRAM ON STRING BUILT-IN FUNCTIONS */

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

int main()
{
char str1[20],str2[20];
//clrscr();

printf("Enter first string in upper case\n");


gets(str1);
printf("Enter second string in lower case\n");
gets(str2);

printf("\n First string length=%d",strlen(str1));


printf("\n First string in lower case=%s",strlwr(str1));
printf("\n Second string in upper case=%s",strupr(str2));
strcat(str2,str1);
printf("\n After concant string2=%s",str2);
printf("\n After copy string2=%s",strcpy(str2,str1));
printf("\n First string in reverse=%s",strrev(str1));

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

First string length=15


First string in lower case=it is c program
Second string in upper case=C IS A LANGUAGE
After concant string2=C IS A LANGUAGEit is c program
After copy string2=it is c program
First string in reverse=margorp c si ti
*/

/* PROGRAM TO COPY ONE STRING TO ANOTHER STRING */

#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

Original string str1=WELCOME


Copied string in str2=WELCOME

267
*/

/* PROGRAM TO COUNT NUMBER CHARACTERS, WORDS AND LINES IN A


STRING */

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

int main()
{
int i=0,l,chars=0,words=1,lines=1;
char str[50],ch;
//clrscr();

printf("Enter string with $ at the end to terminate it\n");


do
{
ch=getchar();
str[i++]=ch;
}while(ch!='$');

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++;
}

printf("\n\n Number of characters=%d",chars);


printf("\n\n Number of words=%d",words);
printf("\n\n Number of lines=%d",lines);

getch();
return 0;
}

/*
Enter string with $ at the end to terminate it

268
THIS IS RAGHU
HOW ARE YOU$

Given string=THIS IS RAGHU


HOW ARE YOU$

Number of characters=25
Number of words=6
Number of lines=2
*/

/* PROGRAM TO PRINT STRING IN REVERSE ORDER */

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

/* PROGRAM TO CONCAT GIVEN TWO STRINGS */

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

printf("Enter second string: \n");


gets(str2);

printf("\n Original string str1=%s",str1);


for(i=0;str1[i]!='\0';i++);
for(j=0;str2[j]!='\0';j++)
str1[i++]=str2[j];
str2[i]='\0';

printf("\n Concated string in str1=%s",str1);

getch();
return 0;
}

/* OUTPUT:
Enter first string:
Hello!
Enter second string:
It is string concat program

Original string str1=Hello!


Concated string in str1=Hello!It is string concat program

*/

/* PROGRAM TO COMPARE TWO STRINGS */

#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

Given strings C STRINGS and C STRINGS are equal


*/

/* PROGRAM TO COMPARE TWO STRINGS */

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

int main()
{
int i=0,j,x;
char str1[50],str2[50];
//clrscr();

printf("Enter first string\n");


gets(str1);
printf("Enter second string\n");
gets(str2);

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

string1 is greater than string2

*/

// Program to print the words ending with letter 'm'

#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]=' ';

printf("\n Words ending with letter 'm' are:\n");


for(t=0,i=0;i<strlen(str);i++)
{
if((str[i]==' ')&&(str[i-1]=='m'))
{
for(j=t;j<i;j++)
printf("%c",str[j]);
t=i+1;
printf("\n");
}
else
{
if(str[i]==' ')
{
t=i+1;
}
}
}

getch();
return 0;
}

/* OUTPUT:
Enter string:c program is easy
Words ending with letter 'm' are:
program

*/

// PROGRAM TO COUNT VOWELS,CONSONANTS, BLANK SPACES,


WORDS, SYMBOLS & DIGITS IN A GIVEN STRING

#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

*/

/* PROGRAM TO REPLACE VOWEL BY @ SYMBOL IN A STRING */

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

275
int main()
{
int i;
char str[50];
//clrscr();

printf("Enter string\n");
gets(str);

printf("\n String before replacing @ symbol=%s",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]='@';
}

printf("\n String after replacing @ symbol=%s",str);

getch();
return 0;
}

/*
OUTPUT:
Enter string
ravinder

String before replacing @ symbol=ravinder


String after replacing @ symbol=r@v@nd@r

*/

/* PROGRAM TO PRINT SUB STRINGS WITH SPECIFIED LENGTH */

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

printf("The subsets are\n");

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;

printf("\n Enter main string:");


scanf("%s",str1);
printf("\n Enter position:");
scanf("%d",&pos);
printf("\n Enter sub string:");
scanf("%s",str2);

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:

Enter main string:PROGRAM


Enter position:2
Enter sub string:abcd
The resultant string is:
PRabcdOGRAM

*/

// Program to delete n characters from a given position in a given string

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

printf("\n Resultant string is:");


printf("%s",str);

279
getch();
return 0;
}

/* OUTPUT:

Enter any string:PROGRAM

Enter position to delete characters:2

Enter number of characters to be deleted:3

Resultant string is:PRAM

*/

// program to search whether sub string is in given string or not

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

int main()
{
char str[100], word[30];
int i,j,len1=0,len2=0,flag;

printf("\n Enter a string:");


gets(str);

printf("\n Enter the sub string you want to search:");


gets(word);

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:

Enter a string:It is C program

Enter the sub string you want to search:program


Sub string is found

*/

// Program to print all possible subsets of the string

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

void fun_subset(int, int, int);


char str[50],n;

int main()
{

int i,len;

printf("Enter the length of string:");


scanf("%d",&len);

printf("Enter string with %d characters length:",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;
}

void fun_subset(int start,int index,int y)


{
int i,j;
if(index-start+1==y)
{
if(y==1)
{
for(i=0;i<n;i++)
printf("%c\n",str[i]);
}

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:

Enter the length of string : 4


Enter the string with 4 characters length: JAVA
The fun_subsets are :

282
J
A
V
A
JA
JV
JA
AV
AA
VA
JAV
JAA
AVA
JAVA

*/

/* PROGRAM TO TEST WHETHER A STRING IS POLYNDROME OR NOT */

#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

*/

// Program to find the largest & smallest word in a string

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

void copy_fun(char *,char *);


void fun(char *,char [],char []);

int main()
{
char str[100],big[50],small[50];

printf("Enter string:");
gets(str);

fun(str,big,small);

printf("\n Biggest Word is:%s",big);


printf("\n Smallest Word is:%s",small);

getch();
return 0;
}

void fun(char arr[],char str1[],char str2[])

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

void copy_fun(char *s,char *t)


{
while(*s!='\0')

285
{
*t=*s;
*s++;
*t++;
}
*t='\0';
}

/* OUTPUT:

Enter string:C is a language

Biggest Word is:language


Smallest Word is:C

*/

// Program to remove repeated characters in a given string

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

printf("The string after removing repeated letters is:%s",str);

getch();
return 0;
}

/* OUTPUT:

Enter the string:IT IS C PROGRAM


The string after removing repeated letters is:IT SCPROGAM

*/

// Program to delete all repeated words in a given string

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

printf("Enter any string:\n");


gets(str1);

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

String after removing repeted words is:


C is a language. rich in operators

*/

// Program to remove newstr spaces from the given string

#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

*/

// Program to remove given Word from a String

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

// Converts the string into 2D array

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

// Compares the string with given word

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++;
}

printf("\n %c repeated times=%d",ch,c);


i++;
}

getch();
return 0;
}

/* OUTPUT:

Enter any string:IT IS C

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

*/

// Program to replace the word by given word

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

printf("Enter any string:\n");


gets(input);
printf("\n Enter which word do you want to replace?:\n");
scanf("%s",word);
printf("\n Enter replacing word:\n");
scanf("%s",repword);
len=strlen(input);

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;

printf("\n After replacing string is:\n");

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?

Enter which word do you want to replace?:


Ravi

Enter replacing word:


RAGHU
After replacing string is:
RAGHU how are you?

*/

// Program to reverse every Word of given String

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

// Converts the string into 2D array

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

// reverses each word of a given string

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

*/

/* PROGRAM TO PRINT GIVEN STRING IN ALPHABETICAL ORDER */

#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

*/

// PROGRAM TO SORT LIST OF WORDS

#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

The sorted string:

Apple
Book
Pen
Slate
*/

// Program to find the largest & smallest word in a string

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

296
#include<string.h>
#include<ctype.h>

void copy_fun(char *,char *);


void fun(char *,char [],char []);

int main()
{
char str[100],big[50],small[50];

printf("Enter string:");
gets(str);

fun(str,big,small);

printf("\n Biggest Word is:%s",big);


printf("\n Smallest Word is:%s",small);

getch();
return 0;
}

void fun(char arr[],char str1[],char str2[])


{
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++;
}

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

void copy_fun(char *s,char *t)


{
while(*s!='\0')
{
*t=*s;
*s++;
*t++;
}
*t='\0';
}

/* OUTPUT:

Enter string:C is a language

Biggest Word is:language


Smallest Word is:C

*/

// program to search whether a given word is in the string or not

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

printf("\n Enter the word you want to search:");


gets(word);

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:

Enter a string:It is C program

Enter the word you want to search:program

Word found

*/

// Program find the frequency of the word 'is' in given string

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

int main()
{

299
char str[100];
int k,count=0,n=0,i,s,space;

printf("\n Enter a string:");


gets(str);

while(str[count]!='\0')
{
count++;
}

/* Finding the frequency of the word 'is' */

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:

Enter a string:It is C program. C is a language.


Frequency of the word 'is'= 2

*/

// Program to insert character or word in any desired location in a string


using pointers

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

printf("\n Enter the String:");


gets(str1);

printf("\n Enter the string to be inserted:");


gets(str2);

printf("\n Enter the position you like to insert:");


scanf("%d",&pos);

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:

Enter the String:C is a language

Enter the string to be inserted:procedure oriented

301
Enter the position you like to insert:4

The string after modification is:

C is a procedure oriented language

*/

/* PROGRAM TO ENCODE GIVEN STRING BY ADDING 3 PLACES */

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

printf("\n Encoded string:");


for(i=0;str[i]!='\0';i++)
{
printf("%c",str[i]+3);
}

getch();
return 0;
}

/*
Enter string
RAVINDER

Given string=RAVINDER
Encoded string:UDYLQGHU

*/

// Program to find the total of all digits present in the string

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

int main()
{
char str[40];
int i,nd=0,total=0;

printf("Enter the string containing both digits and alphabet: \n");


scanf("%s",str);

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

Number of Digits in the string=3

Total of all digits=10

*/

// Program to implement isupper(), islower(), isalpha(), isalnum() and


isspace() functions

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

303
#include <ctype.h>

int main()
{
char ch;

puts("Enter any character:");


ch=getche();

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:

Enter any character:Z


Given character Z is an alphabet
Given character Z is a alpha-numeric
Given character Z is a uppercase letter

*/

// PROGRAM TO PRINT ASCII VALUES OF STRING

#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

*/

// Program to find the first capital letter in a string

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

int main()
{
char str[100];
int i=0;

printf("\n Enter a string:");


gets(str);

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:

Enter a string:today is Monday


Firt capital letter M is found at location 10

*/

// PROGRAM TO CONVERT EACH LETTER TO OPPOSITE CASE

#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

Given string =It is C Program


After conversion each letter to opposite case string is:iT IS c pROGRAM

*/

// PROGRAM TO CONVERT UPPER-CASE STRING TO LOWER-CASE & VICE-


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

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

Enter string in lower case:


for is a keyword
Given string =for is a keyword
Now in uppercase=FOR IS A KEYWORD
*/

// Program to check whether a given two string are anagrams or not

/*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;

printf("Enter first string:");


gets(str1);

printf("Enter second string:");


gets(str2);

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

int test(char a[],char b[])


{
int first[26]={0},second[26]={0},k=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:

Enter first string:pot


Enter second string:top
"pot" and "top" are anagrams.

*/

STRUCTURES,UNIONS

/* PROGRAM ON STRUCTURE FOR STUDENT DETAILS */

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

Enter student name


RAGHU

Enter student roll no,3 subject marks


1 90 99 98

Student name=RAGHU
roll no=1
Student total marks=287
average=95.666664

*/

/* PROGRAM ON STRUCTURE COPY */

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

Enter two values 4 10


Sum of x and y=14
Multiplication of x and y=40
*/

/* PROGRAM ON STRUCTURE WITH ARRAYS */

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

struct stu // for union replace struct keyword by union keyword


{
int rno,m1,m2,m3,tot;
char name[10];
float avg;
}s[20];
//struct stu s1;
//}s1,s2,s3,s4;

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:

Enter 1 student name


RAGHU

Enter 1 student roll no,3 subject marks


1 90 99 98

1 student name=RAGHU
roll no=1
1 student total marks=287
average=95.666664
Enter 2 student name
VIJAY

Enter 2 student roll no,3 subject marks


2 90 77 12

2 student name=VIJAY
roll no=2
2 student total marks=179
average=59.666668
*/

// PROGRAM ON BIT FIELDS

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

//Program on Nested Structure

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

Enter 1 Employee's Name,Age,salary NAGRAJ 30 50000

Enter city, district & state ASIFNAGAR KARIMNAGAR TELENGANA

Enter 2 Employee's Name,Age,salary SRAVAN 27 40000

Enter city, district & state JAMMIKUNTA KARIMNAGAR TELENGANA

1 Employee's Name NAGRAJ


1 Employee's Age 30
1 Employee's Salary 50000
1 Employee's City ASIFNAGAR
1 Employee's District KARIMNAGARTELENGAN1
1 Employee's State TELENGANA

2 Employee's Name SRAVAN


2 Employee's Age 27
2 Employee's Salary 40000
2 Employee's City JAMMIKUNTA
2 Employee's District KARIMNAGAR
2 Employee's State TELENGANA
*/

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

void fun(struct s s1);

int main()
{
struct s s1={10,7.4};
fun(s1);

getch();
return 0;
}

void fun(struct s s2)


{
printf("\n The structure members before modification are :");
printf("a=%d b=%f",s2.a,s2.b);
s2.a=s2.a+10;
s2.b=s2.b+10;
printf(" \n The structure members after modification are :");
printf("a=%d b=%f",s2.a,s2.b);
}

/* OUTPUT:

The structure members before modification are :a=10 b=7.400000


The structure members after modification are :a=20 b=17.400000

*/

/* PROGRAM ON STRUCTURE AND POINTERS */

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

printf("\n Student Name=%s \n Roll number=%d",s1->name,s1->rno);


printf("\n Student Total marks=%d \n Average=%f",s1->m1+s1-
>m2+s1->m3,(s1->m1+s1->m2+s1->m3)/3.0);

getch();
return 0;
}

/* OUTPUT:

Enter student name


Durga Prasad Palle

Enter roll no,3 subject marks


1 60 90 80

Student Name=Durga Prasad Palle


roll number=1
Student Total marks=230
Average=76.666667
*/

/* PROGRAM ON STRUCTURE WITH POINTERS AS PARAMETERS


TO FUNCTION */

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

void fun(struct num *n3)


//fun(struct num n2)
{
printf("\n mul=%f",n3->x*n3->y);
}

/* OUTPUT:

sum=103.300000
mul=329.999995
*/

/* Program to illustrate call by reference in structures */

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

void myfun(struct example *s1)


{
s1->a=s1->a+10;
s1->b=s1->b+10;
}

/* OUTPUT:

The structure members before modification are :a=10 b=45.62


The structure members after modification are :a=20 b=55.62

*/

// Program to illustrate union

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

printf("\n Enter y value:");


scanf("%f",&u1.y);

printf("\n Given y value=%f",u1.y);

getch();
return 0;
}

/* OUTPUT:

Enter x value:10

Given x value=10
Enter y value:20.45

Given y value=20.450000

*/

// Program to illustrate difference between union and structure

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

printf("Size of structure(in bytes)=%d",sizeof(x1));


printf("\n Size of union(in bytes)=%d",sizeof(y1));

getch();
return 0;
}

/* OUTPUT:

Size of structure(in bytes)=6


Size of union(in bytes)=4

*/

// Program to use structure within union, display the contents of


structure elements

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

printf("\nEnter branch: ");


scanf("%s",&det.st.branch);

printf("\n The student details are:");


printf("\n Name:%s",det.st.name);
printf("\nRollno:%d",det.st.rollno);
printf("\n Branch:%s",det.st.branch);

getch();
return 0;
}

/* OUTPUT:

Enter name: RAGHU

Enter rollno: 01

Enter branch: CSE

The student details are:


Name:RAGHU
Rollno:1
Branch:CSE

*/

// Program to illustrate typedef data type

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

board color=black and door color=yellow


Weight of he=60 and she=120
*/

// Program to illustrate enum data type

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

int main()
{
int i;

printf("\n Example on enum data type\n");

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

*/

// Program to illustrate enum data type

#include<stdio.h>
#include<conio.h>
enum gen{MALE,FEMALE};

int main()
{
int gender;

printf("\n Enter gender value 0-Male 1-Female:");


scanf("%d",&gender);

if(gender==MALE)
{
printf("Gender is male");
}

if(gender==FEMALE)
{
printf("Gender is female");
}

getch();
return 0;
}

/* OUTPUT:

Enter gender value 0-Male 1-Female:0


Gender is male

*/

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;

printf("Enter the name of file to read from it:\n");


gets(fname);

fp=fopen(fname,"r"); // read mode

if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}

printf("The contents of %s file are :\n", fname);


while((ch=fgetc(fp))!=EOF)
printf("%c",ch);
fclose(fp);

getch();
return 0;
}

/* OUTPUT:

Enter the name of file to read from it:


myfile.txt
The contents of myfile.txt file are :
It is file example program

*/

// Program to write data into file

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

int main()

324
{
char str[100], fname[20];
FILE *fp;

printf("Enter the name of file to write:\n");


gets(fname);

fp=fopen(fname,"w"); // write mode

if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}

printf("\n Enter data to insert into file\n");


gets(str);

fputs("Writing data to the file",fp);


fprintf(fp,"%s",str);

printf("\n Data inserted in file");


fclose(fp);

getch();
return 0;
}

/* OUTPUT:

Enter the name of file to write:


myfile.txt

Enter data to insert into file


C supports file operations

Data inserted in file

*/

// Program to find size of the specified file

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

int main()

325
{
FILE *fp;
char fname[20];
int size=0;

printf("Enter name of the file to find it's size:");


scanf("%s",fname);
fp=fopen(fname, "r");

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

printf("The size of given file is(in bytes):%d\n",size);


fclose(fp);

getch();
return 0;
}

/* OUTPUT:

Enter name of the file to find it's size:file1.txt

File opened successfullyThe size of given file is(in bytes):20

*/

// Program to find whether a given file is regular file or directory

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

Enter file name:file1.txt


It is regular file

*/

// Program to illustrate fscanf() and fprintf() methods to store


and retrieve students data

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

printf("\n do u want to continue(y/n):");


ch=getche();
}while(ch=='y');

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:

Enter student name:RAGHU


Enter student branch:ECE
Enter student roll number:01

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

Enter student name:SHYAM


Enter student branch:CSE
Enter student roll number:03

do u want to continue(y/n):n

STUDENT NAME BRANCH ROLL NUMBER


------------------------------------------
RAGHU CSE 01
DURGA ECE 02
SHYAM CSE 03

*/

// Program to send all vowels to file called VOWEL.TXT


//and consonants to file called CONSONANT.TXT from the given
file

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

int main()
{
char ch, fname[20];
FILE *fp1,*fp2,*fp3;

printf("Enter the name of file to read from it:");


gets(fname);

fp1=fopen(fname,"r");
fp2=fopen("VOWEL.TXT","w");
fp3=fopen("CONSONANT.TXT","w");

if(fp1==NULL || fp2==NULL || fp3==NULL)


{
perror("Error.... \n");
getch();
exit(1);

329
}

printf("The contents of %s file are :\n", fname);


while((ch=fgetc(fp1))!=EOF)
printf("%c",ch);

rewind(fp1);

//EXTRACTING VOWELS & CONSONANTS


while((ch=fgetc(fp1))!=EOF)
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
fprintf(fp2,"%c",ch);
else
fprintf(fp3,"%c",ch);
}

printf("\n Success.....find the result in files");

fclose(fp1);
fclose(fp2);
fclose(fp3);

getch();
return 0;
}

/* OUTPUT:

Enter the name of file to read from it:file1.txt


The contents of file1.txt file are :
it is file program

Success.....find the result in files

*/

// Program to rename a specified file

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

330
int main()
{
char fname[20],newname[20];
FILE *fp;
int status;

printf("Enter the name of file to rename:");


gets(fname);

printf("Enter new file name:");


gets(newname);

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:

Enter the name of file to rename:file1.txt


Enter new file name:rvrfile.txt

File is renamed

*/

// Program to to display file content with line numbers

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

331
int main()
{
FILE *fp;
char ch,fname[20];
int lineno=1;

puts("Enter the file name:");


gets(fname);
fp=fopen(fname,"r");

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

printf("\n press any key to close...");


getch();
return 0;
}

/* OUTPUT:

Enter the file name:


file1.txt

1:it is first line

332
2:RAGHU
3:RAVINDER
4:DURGA PRASAD
5:APPLE

press any key to close...

*/

// Program to delete specified file

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

int main()
{
char fname[20];
FILE *fp;
int status;

printf("Enter the name of file to delete:");


gets(fname);
fp=fopen(fname,"r");

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:

Enter the name of file to delete:z.txt

333
File is deleted

*/

// Program to copy data from one file to another

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

int main()
{
char ch, sfname[20],dfname[20];
FILE *fp1,*fp2;

printf("Enter source file name to read from it:\n");


gets(sfname);
fp1=fopen(sfname,"r"); // read mode

printf("Enter destination file name to write:\n");


gets(dfname);
fp2=fopen(dfname,"w"); // write mode

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

printf("\n File copied successfully");

fclose(fp1);
fclose(fp2);

334
getch();
return 0;
}

/* OUTPUT:

Enter source file name to read from it:


myfile.txt
Enter destination file name to write:
newfile.txt

File copied successfully

*/

// Program to compare two files data

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

int main()
{
FILE *fp1,*fp2;
int ch1,ch2;
char fname1[40],fname2[40];

printf("Enter name of first file:") ;


gets(fname1);

printf("Enter name of second file:");


gets(fname2);

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

// ASSUME: Content in File1.txt: Welcome to C


//Content in File1.txt: Welcome to Java

/* OUTPUT:
Enter name of first file:file1.txt
Enter name of second file:file2.txt
Data in the two is different
*/

// Program to delete specified line content from the file

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

int main()
{
FILE *fp1, *fp2;
char fname[20],ch;
int del_line,temp=1;

printf("Enter file name:");

336
scanf("%s",fname);

if(fp1==NULL)
{
printf("\n Unable to open file");
getch();
exit(1);
}

fp1=fopen(fname,"r");
ch=getc(fp1);

printf("\n File content before deleted:\n");


while(ch!=EOF)
{
printf("%c",ch);
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++;

//except the line to be deleted


if(temp!=del_line)
{
//copy all lines in file newfile.txt
putc(ch,fp2);
}
}
fclose(fp1);
fclose(fp2);
remove(fname);

//rename the file


rename("newfile.txt", fname);
printf("\n The contents of file after being modified are as follows:\n");

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

File content before deleted:


1111111
2222222
3333333
4444444
5555555

Enter line number of the line to be deleted:3

The contents of file after being modified are as follows:


111111
2222222
4444444
5555555

*/

// Program to find total number of lines a given file

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

fp=fopen(fname,"r"); // read mode

if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}

printf("The contents of %s file are :\n", fname);


while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
if(ch=='\n')
line++;
}

printf("\n Total number of lines in a file=%d",line);


fclose(fp);

getch();
return 0;
}

/* OUTPUT:

Enter the name of file to read from it:


file1.txt
The contents of file1.txt file are :
apple
bus
candle

Total number of lines in a file=3

*/

// Program to list files in specified directory

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

339
int main()
{
DIR *d;
struct dirent *dir;

//d=opendir("."); // . indicates current directory

d=opendir("c:\\mydir\\");

printf("List of files are:\n");


if(d)
{
while((dir=readdir(d))!=NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}

getch();
return 0;
}

/* OUTPUT:

List of files are:


.
..
file1.txt
file2.txt
file3.txt
file4.txt

*/

// Program to merge two files and store result in a new file

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

printf("\n Enter name of second file:");


gets(file2);

printf("\n Enter file name to store contents of two files:");


gets(newfile);

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

printf("Two files were merged into %s file successfully.\n",newfile);

fclose(fp1);
fclose(fp2);
fclose(fp3);

getch();
return 0;
}

341
/* OUTPUT:

Enter name of first file:file1.txt

Enter name of second file:file2.txt

Enter file name to store contents of two files:file3.txt


Two files were merged into file3.txt file successfully.

*/

// Program to illustrate use of arrays with structure for


emmployee data

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

struct emp e[10]; //ARRAY OF STRUCTURES

printf("\n Enter how many employees data you want to enter:");


scanf("%d",&n);

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

fprintf(fp,"%s\t\t%s\t%d",e[i].name,e[i].dept,e[i].id); //moving to files


}
fclose(fp);

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:

Enter how many employees data you want to enter:2

Enter 1-employee name:RAVI

Enter 1-employee department:HR

343
Enter 1-employee id:10

Enter 2-employee name:VIJAY

Enter 2-employee department:FINANCE

Enter 2-employee id:11

EMPLOYEE NAME DEPARTMENT ID


-----------------------------------------

RAVI HR 10
VIJAY FINANCE 11

*/

// Program to append data to the exist file

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

int main()
{
char ch,str[100],fname[20];
FILE *fp;

printf("Enter the name of file to read from it:\n");


gets(fname);

fp=fopen(fname,"a+"); // append mode

if(fp==NULL)
{
perror("Error while opening the file.\n");
exit(1);
}

printf("The contents of %s file are :\n", fname);


while((ch=fgetc(fp))!=EOF)
printf("%c",ch);

printf("\n Now enter new data to insert into file\n");


gets(str);

fprintf(fp,"%s",str);

344
printf("\n Data inserted in file\n");

rewind(fp); //movues file pointer to beginning of the file

printf("--------------------------------\n");
printf("File content after appending is:\n");
while((ch=fgetc(fp))!=EOF)
printf("%c",ch);

fclose(fp);

getch();
return 0;
}

/* OUTPUT:

Enter the name of file to read from it:


myfile.txt
The contents of myfile.txt file are :
it is append program
Now enter new data to insert into file
Now i am adding this line

Data inserted in file


--------------------------------
File content after appending is:
it is append program
Now i am adding this line

*/

// Program to append one file data to another

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

printf("\n Source file data is:\n");


printf("\n ---------------------------------\n");
while((ch=fgetc(fp1))!=EOF)
printf("%c",ch);

printf("\n Enter destination file name to append:");


gets(dfname);
fp2=fopen(dfname,"a+"); // write mode

if(fp2==NULL)
{
perror("Error while creating the file.\n");
exit(1);
}

printf("\n Target file data is before append:\n");


printf("\n ---------------------------------\n");
while((ch=fgetc(fp2))!=EOF)
printf("%c",ch);

rewind(fp1); //movues file pointer to beginning of the file

// APPEND PROCESS
while((ch=fgetc(fp1))!=EOF)
fprintf(fp2,"%c",ch);

rewind(fp2);

printf("\n Target file data is after append:\n");


printf("\n ---------------------------------\n");
while((ch=fgetc(fp2))!=EOF)
printf("%c",ch);

fclose(fp1);
fclose(fp2);

346
getch();
return 0;
}

/* OUTPUT:

Enter source file name to read from it: file1.txt

Source file data is:


---------------------------------
it is first file

Enter destination file name to append: file2.txt

Target file data is before append:


---------------------------------
it is second file

Target file data is after append:

---------------------------------
it is second file
it is first file

*/

// Program to illustrate fseek() and ftell() methods to print file


data in reverse order and printing position

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

printf("Enter data and end with $ symbol:\n");


while((c=getchar())!='$')
putc(c,fp);

printf("\n Total number of characters in a file=%d\n", ftell(fp));


fclose(fp);

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

printf("\n File content in reverse order is:\n");


fseek(fp,-1L,2); /* Position to the last character */
do
{
putchar(getc(fp));
}
while(!fseek(fp,-2L,1));
fclose(fp);

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

File content in reverse order is:


C SI TI

*/

// Program to compare two files data

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

int main()
{
FILE *fp1,*fp2;
int ch1,ch2;
char fname1[40],fname2[40];

printf("Enter name of first file:") ;


gets(fname1);

printf("Enter name of second file:");


gets(fname2);

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

// ASSUME: Content in File1.txt: Welcome to C


//Content in File1.txt: Welcome to Java

/* OUTPUT:
Enter name of first file:file1.txt
Enter name of second file:file2.txt
Data in the two is different
*/

// PROGRAM TO FIND SUM OF COMMAND LINE ARGUMENTS

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

int main(int argc,char *argv[])

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

/* PROGRAM FOR LINEAR SEARCH */

#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]);
}

printf("\n Enter element to be search");


scanf("%d",&key);
for(i=0;i<n-1;i++)
{
if(x[i]==key)
{
printf("%d Element is found at location %d",key,i+1);
getch();
exit(0);
}
}
printf("%d is not found",key);
//printf("\n END OF PROGRAM");

getch();
return 0;
}

/* OUTPUT:

Enter how many elements do you want to enter 4


Enter the elements 10 20 14 87

Enter element to be search 14


14 element is found at location 3

Second run:
enter how many elements do you want to enter 3
enter the elements
4
12
52

enter element to be search 20


20 is not found

*/

/* PROGRAM FOR BINARY SEARCH */

#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]);
}

printf("\n Enter element to be search");


scanf("%d",&key);
low=0;
high=n-1;

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:

Enter the total number of elements 4


Enter the elements in ascending order
10
20
56

353
89

Enter element to be search 20


20 element found at location 2
*/

SORTING

/* PROGRAM FOR BUBBLE SORT */

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

Enter the total number of elements 6


Enter the elements 2 12 89 35 76 26

Numbers before sort 2 12 89 35 76 26


Numbers after sort 2 12 26 35 76 89
*/

/* PROGRAM FOR INSERTION SORT */

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

void inssort(int a[10],int n);

int main()
{
void inssort(int [],int );
int a[10],i,n;
//clrscr();
printf("\n Enter number of Elements: ");
scanf("%d",&n);

printf("\n Enter the elements: ");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("\n Elements before sorting: ");

for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
// CALLING INSERTION SORT
inssort(a,n);

printf("\n Elements after sorting:");


for(i=0;i<n;i++)
printf("\t %d",a[i]);

getch();
return 0;

355
}

void inssort(int a[10],int n)


{
int i,j,k,temp;
for(i=0;i<n;i++)
for(j=0;j<i;j++)

if(a[i]<a[j])
{
temp=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[j]=temp;
break;
}
}

/* OUTPUT:

Enter number of Elements: 5

Enter the elements: 65 21 8547 30 26

Elements before sorting: 65 21 8547 30 26


Elements after sorting: 21 26 30 65 8547

*/

// PROGRAM ON SELECTION SORT

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 500

void ssort(int elements[], int maxsize);


int arr[MAXSIZE],n;

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

printf("\nArray before sorting:\n");


for (i = 0; i<n; i++)
printf("%10d",arr[i]);
printf ("\n");

//calling selection sort


ssort(arr, n);

printf("\nArray after sorting:\n");


for (i = 0; i <n; i++)
printf("%10d", arr[i]);

getch();
return 0;
}

// selection sort definition


void ssort(int arr[], int array_size)
{
int i, j, k;
int min, temp;
for(i=0; i<n-1; i++)
{
min=i;
for(j=i+1; j<n; j++)
{
if(arr[j]<arr[min])
min=j;
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}

/* OUTPUT:

How many arr you want to sort: 5

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

Array before sorting:


10 23 5 687 58

Array after sorting:


5 10 23 58 687
*/

/* PROGRAM FOR QUICK SORT WITH RECURSION */

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

void qsort(int b[],int left,int right);

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

printf("\n Numbers before sort");


for(i=0;i<n;i++)
printf("%4d",x[i]);
l=0;
r=n-1;

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:

Enter the total number of elements 5

Enter the elements 10 4 35 26 85

Numbers before sort 10 4 35 26 85


Numbers after sort 4 10 26 35 85

359
*/

// PROGRAM ON MERGE SORT

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

// MERGE SORT METHOD DEFINITION

void merge_sort(int low,int high)


{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}

360
// MERGE METHOD DEFINITION

void merge(int low,int mid,int high)


{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}

/* OUTPUT:

MERGE SORT

361
Enter the total numbers: 5

Enter 5 numbers:
10
2
53
698
12

SORTED ORDER: 2 10 12 53 698

*/

/* PROGRAM ON MERGE SORT USING TWO ARRAYS */

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

int main()
{
int a[10],b[10],c[20],i,j,k,f,s;
//clrscr();

//f=first array s=second array


printf("\n ENTER HOW MANY NUMBERS YOU WANT TO PUT IN ARRAY
A:");
scanf("%d",&f);

printf("\n ENTER %d NUMBERS INTO ARRAY A IN ASCENDING


ORDER\n",f);
for(i=0;i<f;i++)
scanf("%d",&a[i]);

printf("\n ENTER HOW MANY NUMBERS YOU WANT TO PUT IN ARRAY


B:");
scanf("%d",&s);

printf("\n ENTER %d NUMBERS INTO ARRAY B IN ASCENDING


ORDER\n",s);
for(i=0;i<s;i++)
scanf("%d",&b[i]);

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++];

printf("\n\n THE ELEMENTS IN MERGED LIST C ARE \n");


for(i=0;i<f+s;i++)
printf("%4d",c[i]);

getch();
return 0;
}

/* OUTPUT:

ENTER HOW MANY NUMBERS YOU WANT TO PUT IN ARRAY A: 4

ENTER 4 NUMBERS INTO ARRAY A IN ASCENDING ORDER


20 25 30 35

ENTER HOW MANY NUMBERS YOU WANT TO PUT IN ARRAY B: 6

ENTER 6 NUMBERS INTO ARRAY B IN ASCENDING ORDER


10 15 28 32 46 59

THE ELEMENTS IN MERGED LIST C ARE


10 15 20 25 28 30 32 35 46 59
*/

// PROGRAM FOR HEAP SORT

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

printf("\n Enter number of elements:");


scanf("%d",&n);

printf("\n Enter the elements:");


for(i=0;i<n;i++)
scanf("%d",&arr[i]);

heapsort(arr,n);

printf("\nThe Sorted Elements Are:\n");


for(i=0;i<n;i++)
printf("\t%d",arr[i]);

getch();
return 0;
}

void heapsort(int arr[],int n)


{
int i,temp;
heapify(arr,n);

for(i=n-1;i>0;i--)
{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
adjust(arr,i);
}
}

void heapify(int arr[],int n)


{
int k,i,j,item;
for(k=1;k<n;k++)
{
item=arr[k];
i=k;
j=(i-1)/2;

while((i>0)&&(item>arr[j]))

364
{
arr[i]=arr[j];
i=j;
j=(i-1)/2;
}
arr[i]=item;
}
}

void adjust(int arr[],int n)


{
int i,j,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:

Enter number of elements:5

Enter the elements:2 54 10 69 35

The Sorted Elements Are:


2 10 35 54 69

*/

365
DATA STRUCTURES

/* PROGRAM TO IMPLEMENT STACK USING ARRAYS */

#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

DO YOU WANT TO CONTINUE(Y/N):y

ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
1
ENTER ELEMENT TO INSERT INTO STACK 65

DO YOU WANT TO CONTINUE(Y/N): y

ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
1
ENTER ELEMENT TO INSERT INTO STACK 85

DO YOU WANT TO CONTINUE(Y/N): y

ENTER CHOICE
1)PUSH 2)POP 3)DISPLAY 4)EXIT
3

ELEMENTS IN STACK ARE


23...65...85...
DO YOU WANT TO CONTINUE(Y/N) y

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
}

// INSERTING ELEMENT-PUSH MENTHOD


void push()
{
n=(struct stack*)malloc(sizeof(struct stack));
printf("\n ENTER THE ELEMENT TO BE INSERTED\n");
scanf("%d",&n->data);
n->next=top;
top=n;
}

//DELETING ELEMENT-POP METHOD


void pop()
{
temp=top;
if(temp==NULL)
printf("STACK IS EMPTY\n");
else
{
printf("%d IS DELETED\n",temp->data);
top=top->next;
}
}

//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:

MENU OPTIONS ARE:


1.PUSH
2.POP
3.display
4.EXIT
ENTER YOUR CHOICE:1
ENTER THE ELEMENT TO BE INSERTED
10

370
MENU OPTIONS ARE:
1.PUSH
2.POP
3.display
4.EXIT
ENTER YOUR CHOICE:1
ENTER THE ELEMENT TO BE INSERTED
20

MENU OPTIONS ARE:


1.PUSH
2.POP
3.display
4.EXIT
ENTER YOUR CHOICE:3
THE ELEMENTS IN THE STACK ARE..
20-10-

MENU OPTIONS ARE:


1.PUSH
2.POP
3.display
4.EXIT
ENTER YOUR CHOICE:2
20 IS DELETED

MENU OPTIONS ARE:


1.PUSH
2.POP
3.display
4.EXIT

ENTER YOUR CHOICE:3


THE ELEMENTS IN THE STACK ARE..
10-
MENU OPTIONS ARE:
1.PUSH
2.POP
3.display
4.EXIT
ENTER YOUR CHOICE:

*/

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

*/

/* PROGRAM TO IMPLEMENT CIRCULAR QUEUE */

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

printf("\n\nEnter Your Choice -> ");


scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid Choice");
}
printf("\n\nDo u Want to Continue(y/n) -> ");
ch = getche();
} while (ch == 'y' || ch == 'Y');
return 0;
}

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

Enter Your Choice -> 1

Enter data You want to insert ->10

Data inserted successfully

Do u Want to Continue(y/n) -> y


1 -> Insert
2 -> Delete
3 -> Display
4 -> Exit

Enter Your Choice -> 1

Enter data You want to insert ->20

Data inserted successfully

Do u Want to Continue(y/n) -> y


1 -> Insert
2 -> Delete
3 -> Display

377
4 -> Exit

Enter Your Choice -> 3

:: Queue Elements are ::

10
20

Do u Want to Continue(y/n) -> y


1 -> Insert
2 -> Delete
3 -> Display
4 -> Exit

Enter Your Choice -> 2

Element deleted Successfully

Do u Want to Continue(y/n) -> y


1 -> Insert
2 -> Delete
3 -> Display
4 -> Exit

Enter Your Choice -> 3

:: Queue Elements are ::

20

Do u Want to Continue(y/n) ->n

*/

// Program to implement single linked list

#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

*/

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

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:

QUEUE IMPLEMENTATION USING LINKED LIST

1: INSERT
2: DELETE
3: DISPLAY
4: EXIT

Enter your choice: 1


Enter data to insert into queue:10

Enter your choice: 1


Enter data to insert into queue:20

Enter your choice: 3

Queue elements are: 10 20

Enter your choice: 2

Deleted element is :10

Enter your choice: 3

Queue elements are: 20

Enter your choice: 2

386
Deleted element is :20

Enter your choice: 3

Queue elements are:

Enter your choice:4


*/

// Program to implement single linked list

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

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;

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:

QUEUE IMPLEMENTATION USING LINKED LIST

1: INSERT
2: DELETE
3: DISPLAY
4: EXIT

Enter your choice: 1


Enter data to insert into queue:10

394
Enter your choice: 1
Enter data to insert into queue:20

Enter your choice: 3

Queue elements are: 10 20

Enter your choice: 2

Deleted element is :10

Enter your choice: 3

Queue elements are: 20

Enter your choice: 2

Deleted element is :20

Enter your choice: 3

Queue elements are:

Enter your choice:4


*/

/*Program for Infix to postfix conversion using stack*/

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

// calling precedence function


else if(precd(str[i])<=precd(s[top]))
{
printf("%c",pop());
push(str[i]);
}
else
push(str[i]);
}
while(top>=0)
printf("%c",pop());
getch();
return 0;
}
// push function definition
void push(char st)
{
s[++top]=st;
}
// pop function definition
char pop()
{
char st;

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:

Enter infix expression:(a+b)*(c-d)

Postfix expression is:ab+cd-*

second run:

Enter infix expression:x+y/z

Postfix expression is:xyz/+


*/

// Program to convert infix expression to prefix

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

printf("\n Enter Infix Expression:");


scanf("%s",infix);

push('$'); // to identify of empty stack


strrev(infix);
while((ch=infix[i++])!='\0')
{
if(ch==')')
push(ch);
else if(isalnum(ch))
prefix[k++]=ch;
else if(ch=='(')
{
while(s[top]!=')')
prefix[k++]=pop();
elem=pop();
}
else
{
while(priority(s[top])>=priority(ch))
prefix[k++]=pop();
push(ch);
}
}

while(s[top]!='$') /* Pop from stack till empty */


prefix[k++]=pop();
prefix[k]='\0';
strrev(prefix);
strrev(infix);
printf("\n\nGiven Infix Expression is:%s \n Prefix Expression is:
%s\n",infix,prefix);
getch();
}

void push(char element)


{
s[++top]=element;
}

398
char pop()
{
return(s[top--]);
}

int priority(char element)


{
switch(element)
{
case '$': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/':return 3;
}
}

/* OUTPUT:

Enter Infix Expression:a+b

Given Infix Expression is:a+b


Prefix Expression is:+ab

*/

// Porgram to evaluate postfix expression

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

printf("\n Enter Postfix Expression:");


scanf("%s",postfix);

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

printf("\n Given Postfix Expression:%s\n",postfix);


printf("\n Result after Evaluation: %d\n",s[top]);
getch();
return 0;
}

/* OUTPUT:

Read the Postfix Expression ? 23+7*

Given Postfix Expn: 23+7*

Result after Evaluation: 35

400
*/

// Program for Breadth First Search (BFS) graph traversal

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

printf("\n Enter graph data in matrix form:\n");


for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&arr[i][j]);

printf("\n Enter the starting vertex:");


scanf("%d",&v);
bfsearch(v);

printf("\n THE BFS ORDER IS:\n");

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:

Enter the number of vertices:6

Enter graph data in matrix form:


011100
100011
100001
100000
010000
011000

Enter the starting vertex:1

The node which are reachable are:


1 2 3 4 5 6

*/

// Program for Depth First Search (DFS) graph traversal

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

int arr[10][10],reach[20],n;

void dfsearch(int vertex)


{
int i;
reach[vertex]=1;
for(i=1;i<=n;i++)
if(arr[vertex][i]&&!reach[i])
{
printf("\n %d->%d",vertex,i);
dfsearch(i);

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

printf("\n Enter the adjacency matrix:\n");


for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&arr[i][j]);

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

Enter the adjacency matrix:


011100
100011
100001
100000
010000

403
011000

1->2
2->5
2->6
6->3
1->4

Graph is connected

*/

OTHERS

// Program to open a website

#include<windows.h>
#include<stdlib.h>

int main()
{
system("explorer http://www.google.com");
return 0;
}

// Program to print environment variables

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

int main(int argc, char *argv[], char * envp[])


{
int i;
for(i=0;envp[i]!=NULL;i++)
{
printf("\n%s",envp[i]);
}
getch();
return 0;
}

/* 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

*/

// FACTORIAL OF A NUMBER FOR BIG NUMBERS

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

printf("\nFactorial of %d is :\n ",n);


for(i=length;i>=0;i--)
{
printf("%d",f[i]);
}

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

*/

// Program to illustrate user defined header file

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

#include "MYHEADER.h" //in this file add method already created

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:

Addition of 10 and 20=30

*/

// Program to print IP-ADDRESS of system

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

int main()
{
system("C:\\Windows\\System32\\ipconfig");

407
getch();
return 0;
}

/* OUTPUT:

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :


IP Address. . . . . . . . . . . . : 172.16.3.249
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

*/

// PROGARM TO FIND 1'S COMPLEMENT OF A GIVEN NUMBER

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

// PROGARM TO FIND 2'S COMPLEMENT OF A GIVEN NUMBER

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

void convert(char *);

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

void convert(char *bin)


{
int len,i,c=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:

Enter any binary number:101


The 2's complement is: 011

*/

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

printf("Enter first string:");


gets(str1);

printf("Enter second string:");


gets(str2);

l1=strlen(str1);
l2=strlen(str2);
len=l1+l2;

//Logic to find matched characters and make it as empty in that position


for(i=0;i<l1;i++)
{
str=str1[i];
for(j=0;j<l2;j++)
{
if(str==str2[j])
{
str2[j]=' ';
str1[i]=' ';
count=count+1;
break;
}
}
}
//total length - matched count
scount=(len-(count+count));
ch=scount;

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

*/

// PROGRAM TO SHUTDOWN COMPUTER

#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

*/

/* Program to demonstrates the SYSYTEM() function */

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

Enter any system command ex:dir or copy etc.,blank to exit


date
The current date is: Fri 10/31/2014
Enter the new date: (mm-dd-yy)

413
*/

/* PROGRAM ON TOWER OF HANOI USING RECURSION*/

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

void hanoi(int n,char sndl,char indl,char dndl)


{
if(n!=0)
{
/* move n-1 disks from starting to intermediate needle*/
hanoi(n-1,sndl,dndl,indl);

/* move n disks from star to destination*/


printf("MOVE DISK %d from %c to %c \n",n,sndl,dndl);

/* move n-1 disks from intermediate to destination needle*/


hanoi(n-1,indl,sndl,dndl);
}
}

/* OUTPUT:
ENTER THE NUMBER OF DISKS 3

TOWER OF HANOI PROBLEM WITH 3 DISKS


MOVE DISK 1 from L to R
MOVE DISK 2 from L to C

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

*/

//Program to display current system time

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

Current time: 11:26:50 2014

*/

GRAPHICS

// PROGRAM TO DRAW A LINE

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

int main()
{
/* request auto detection */
415
int gdriver=DETECT,gmode,errorcode;
char msg[80];

/* initialize graphics and local variables */


initgraph(&gdriver,&gmode,"c:\\turboc3\\BGI");

/* read result of initialization */


errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key:");
getch();
exit(1);
}
//Drawing a line

line(10,10,100,200);

moveto(300,300); //moving co-ordinates


sprintf(msg,"(%d,%d)", getx(),gety());
outtextxy(300,300,msg);

//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;
}

// Program to show an example on circle graphics

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

// Program to draw a circle with different colors

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

// Filling a circle with color


setcolor(BLUE);
setfillstyle(SOLID_FILL,BLUE);
circle(200,200,40);
floodfill(200,200,BLUE); //fills the color at specified region

getch();
closegraph();
return 0;
}

// Program to draw various diagrams

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

// Program to create a face animation

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

// Program to show an example on different fonts

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

// Program to set text backgound with different colors

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

// Program to set screen backgound with different


colors

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

main()
422
{
int gd=DETECT,gm,i;
clrscr();

initgraph(&gd,&gm,"c:\\turboc3\\BGI");

for(i=0;i<=15;i++) //0-15 indicates diff colors


{
setbkcolor(i);
delay(1000);
}

getch();
return 0;
}

// Program to draw a bar-chart

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

// Program to create a piechart

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

// Program to show an example for polygon

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

// Program to create a square animation

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

// Progarm to print text with different colors


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

main()
{
int i;
clrscr();
for(i=0;i<=15;i++)
{
textcolor(i+BLINK);
delay(1000);
cprintf(" C is a programming language");
}
getch();
return 0;
}

// program to show an example to moving car animation

#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

You might also like