0% found this document useful (0 votes)
9 views

C Lang All Programs

By Naresh It, Shiva Chaitanya Sir

Uploaded by

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

C Lang All Programs

By Naresh It, Shiva Chaitanya Sir

Uploaded by

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

Classroom Programs Naresh IT 1

Chapter-1

C-Language Basics

1. Program to print hello on output screen:

#include<stdio.h>
main()
{
printf("hello");
}

Output:
Hello

2. Program to demonstrate Escape Sequences:

#include<stdio.h>
main()
{
printf(" hello");
printf("\n Welcome to \t naresh IT");
printf("\n \'Hi\' \n");
printf(" \"hello\" \n");
printf("\a abcd\befg");
}

Output:
hello
Welcome to naresh IT
'Hi'
"hello"
abcefg

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

Example Programs on Declaring Variables, Assigning Values


and Printing them using Format Specifiers:

1. Program to add two Numbers:


#include<stdio.h>
main()
{
int x=20,y=30,z;

z=x+y;

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

Output:
sum=50
sum of 20 and 30 is 50

2. Program to find area and perimeter of circle:


#include<stdio.h>
main()
{
float pi=3.14,r=7.9,a,p;

a=pi*r*r;
p=2*pi*r;

printf(" area=%f",a);
printf("\n perimeter=%f",p);
}

Output:
area=195.967422
perimeter=49.612003

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

3. Program to print student details:


#include<stdio.h>
main()
{
char section='A';
int m=786;
float avrg=78.6;

printf(" section:%c \n marks:%d \n average marks:%f",section,m,avrg);


}

Output:
section:A
marks:786
average marks:78.599998

4. Program to print a string:


#include<stdio.h>
main()
{
char x[10]="Raju";

printf("Hi %s",x);
}

Output:
Hi Raju

5. Program to print decimal, octal and hexa decimal values:


#include<stdio.h>
main()
{
int x=30;

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

printf("decimal value=%d\n",x);
printf("octal value=%o\n",x);
printf("hexa decimal value=%x\n",x);
printf("hexa decimal value=%X",x);
}

Output:
decimal value=30
octal value=36
hexa decimal value=1e
hexa decimal value=1E

6. Program to print floating point value in scientific notation:


#include<stdio.h>
main()
{
float f=234.5678;

printf("float value=%E",f);
}

Output:
float value=2.345678E+002

Note: 2.345678E+002 = 2.345678*102

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

Example Programs on Reading Data From Keyboard using


scanf():

1. Program to add two numbers by reading two integers from


keyboard:
#include<stdio.h>
main()
{
int x,y,z;

printf("Enter x,y:");
scanf("%d%d",&x,&y);

z=x+y;

printf("sum=%d",z);
}

Output:
Enter x,y:20
30
sum=50

2. Program to find area and perimeter of the circle by reading radius


value from keyboard:
#include<stdio.h>
main()
{
float r,pi=3.14;

printf("Enter radius:");
scanf("%f",&r);

printf(" area=%f \n perimeter=%f",pi*r*r,2*pi*r);


}

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

Output:
Enter radius:6.4
area=128.614410
perimeter=40.192001

3. Program to print student details like section, marks and average


marks by reading them from keyboard:
#include<stdio.h>
main()
{
char section;
int marks;
float avrg;

printf("Enter section, marks and average marks:");


scanf("%c%d%f",&section,&marks,&avrg);

printf(" section:%c \n marks:%d \n average marks:%f",section,marks,avrg);


}

Output:
Enter section, marks and average marks:A
786
78.6
section:A
marks:786
average marks:78.599998

4. Program to read and print a string:


#include<stdio.h>
main()
{
char x[20];

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

printf("Enter your name:");


scanf("%s",x);

printf("Hi %s",x);
}

Output:
Enter your name:Ravi
Hi Ravi

5. Program to find average of three numbers:


#include<stdio.h>
main()
{
int x,y,z;
float avrg;

printf("Enter three numbers:");


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

avrg=(float)(x+y+z)/3; // type conversion (or) type casting

printf("average=%f",avrg);
}

Output:
Enter three numbers:10
20
31
average=20.333334

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

6. Program to swap (interchange) two numbers:


#include<stdio.h>
main()
{
int x,y,temp;

printf("Enter two numbers:");


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

printf("Before swapping:\n");
printf("x=%d \t y=%d",x,y);

temp=x;
x=y;
y=temp;

printf("\n After swapping:\n");


printf("x=%d \t y=%d",x,y);
}

Output:
Enter two numbers:10 20
Before swapping:
x=10 y=20
After swapping:
x=20 y=10

7. Program to swap two numbers without taking third variable:


#include<stdio.h>
main()
{
int x,y;

printf("Enter two numbers:");


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

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

printf("Before swapping:\n");
printf("x=%d \t y=%d",x,y);

x=x+y;
y=x-y;
x=x-y;

printf("\n After swapping:\n");


printf("x=%d \t y=%d",x,y);
}

Output:
Enter two numbers:20 30
Before swapping:
x=20 y=30
After swapping:
x=30 y=20

C-Language Basics V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-2

Operators

1. Program to demonstrate Arithmetic Operators:

#include<stdio.h>
main()
{
int x,y,a,b,c,d,e;

printf("Enter x,y:");
scanf("%d%d",&x,&y);

a=x+y;
b=x-y;
c=x*y;
d=x/y;
e=x%y;

printf("sum=%d\n",a);
printf("difference=%d\n",b);
printf("product=%d\n",c);
printf("Quotient=%d\n",d);
printf("remainder=%d",e);
}

Output:
Enter x,y:10 6
sum=16
difference=4
product=60
Quotient=1
remainder=4

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

2. Program to check whether the given number is even or odd:


#include<stdio.h>
main()
{
int n;

printf("Enter a Number:");
scanf("%d",&n);

if(n%2==0)
printf("Even Number");
else
printf("Odd Number");
}

Output:
Enter a Number:8
Even Number

3. Program to check whether a person is eligible to vote or not:


#include<stdio.h>
main()
{
int age;

printf("Enter your age:");


scanf("%d",&age);

if(age>=18)
printf("Eligible to Vote");

else
printf("Not Eligible to Vote");
}

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

Output:
Enter your age:23
Eligible to Vote

4. Program to find result of a student in one subject. Max marks are


100. Student must get minimum 40 marks for pass:
#include<stdio.h>
main()
{
int m;

printf("Enter maths sub marks:");


scanf("%d",&m);

if(m>=40)
printf("Pass");
else
printf("Fail");
}

Output:
Enter maths sub marks:52
Pass

5. Program to find result of a student in 3 subjects. Max marks are 100


for each subject. Minimum 40 marks are required in each subject
for pass:
#include<stdio.h>
main()
{
int m,p,c;

printf("Enter 3 subjects marks:");


scanf("%d%d%d",&m,&p,&c);

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

if(m>=40 && p>=40 && c>=40)


printf("Pass");

else
printf("Fail");
}

Output:
Enter 3 subjects marks: 44 66 55
Pass

6. Program to demonstrate Not Operator:


#include<stdio.h>
main()
{
if(!0)
printf("hi");
}

Output:
hi

Note: 0 => False 1 / Non-Zero => True

7. Program to demonstrate Assignment Operators:


main()
{
int x=5;

printf("x=%d\n",x);

x*=3;
printf("x=%d\n",x);

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

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

x-=2;
printf("x=%d\n",x);

x/=3;
printf("x=%d\n",x);

x%=4;
printf("x=%d\n",x);
}

Output:
x=5
x=15
x=25
x=23
x=7
x=3

8. Program to demonstrate Bitwise Operators:


#include<stdio.h>
main()
{
int x=12,y=13;

printf("x&y=%d\n",x&y);
printf("x|y=%d\n",x|y);
printf("x^y=%d\n",x^y);
printf("x<<2=%d\n",x<<2);
printf("x>>2=%d\n",x>>2);
printf("~x=%d",~x);
}

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

Output:
x=5
x=15
x=25
x=23
x=7
x=3

9. Program to demonstrate Unary Minus Operator:


#include<stdio.h>
main()
{
int x=8;

printf("x=%d\n",x); //x=8

x=-x; //unary minus


printf("x=%d\n",x); //x=-8

x=-x;
printf("x=%d",x); //x=8
}

Output:
x=8
x=-8
x=8

10. Program to demonstrate Size Of Operator:


#include<stdio.h>
main()
{
short int x;

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

printf("int size=%d\n",sizeof(int));
printf("float size=%d\n",sizeof(float));
printf("char size=%d\n",sizeof(char));
printf("double size=%d\n",sizeof(double));
printf("size of x=%d",sizeof(x));
}

Output:
int size=4
float size=4
char size=1
double size=8
size of x=2

11. Program to check whether the given number is even or odd using
Ternary Operator [Conditional Operator]:
#include<stdio.h>
main()
{
int n;

printf("Enter n:");
scanf("%d",&n);

n%2==0 ? printf("Even") : printf("Odd");


}

Output:
Enter n:7
Odd

12. Program to find biggest in two different numbers using Ternary


Operator [Conditional Operator]:
#include<stdio.h>
main()

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

{
int x,y,big;

printf("Enter two different numbers:");


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

// x>y ?printf("%d is big",x):printf("%d is big",y);

big = x>y ? x : y;

printf("biggest number=%d",big);
}

Output:
Enter two different numbers:40 30
biggest number=40

13. Program to demonstrate Address Of Operator:


#include<stdio.h>
main()
{
int x=20;
float y=67.89;

printf("x value=%d\n",x);
printf("x address=%d\n",&x);

printf("y value=%f\n",y);
printf("y address=%d",&y);
}

Output:
x value=20
x address=6487580
y value=67.889999
y address=6487576

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

14. Program to demonstrate Comma Operator:


#include<stdio.h>
main()
{
int a, x, y, z; // separating multiple variables
x=1, y=2, z=3; // separating multiple assignments
printf("x=%d\ty=%d\tz=%d\n",x,y,z);
a=(x,y,z); // Assigns z value to a
printf("a=%d\n",a);
a = x=x+1, y=x+y, z=y+z; // separating multiple expressions
printf("a=%d",a);
}
Output:
x=1 y=2 z=3
a=3
a=2

Programs On Increment Operator:


Post Increment & Pre Increment as Single Statement:

Post Increment Pre Increment

#include<stdio.h> #include<stdio.h>
main() main()
{ {
int x=5; int x=5;

x++; //Post Increment ++x; // Pre Increment

printf("%d",x); printf("%d",x);
} }

Output: Output:
6 6

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

Post Increment & Pre Increment in case of Assignment:

Post Increment Pre Increment

#include<stdio.h> #include<stdio.h>
main() main()
{ {
int x=5,y; int x=5,y;

y=x++; y=++x;

printf("x=%d\n",x); printf("x=%d\n",x);
printf("y=%d",y); printf("y=%d",y);
} }

Output: Output:
x=6 x=6
y=5 y=6

Note:

y=x++; :
In this statement, first x value will be assigned to y. Then x value will be
increased.

Y=++x; :
In this statement, first x value will be increased. Then x value be assigned
to y.

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

Post Increment & Pre Increment in case of Printing:

Post Increment Pre Increment

#include<stdio.h> #include<stdio.h>
main() main()
{ {
int x=5; int x=5;

printf("%d\n",x++); printf("%d\n",++x);
printf("%d",x); printf("%d",x);
} }

Output: Output:
5 6
6 6

Note:

printf("%d\n",x++); :
In this statement, first x value will be printed. After that x value be
increased.

printf("%d\n",++x); :
In this statement, first x value be increased, then x value will be printed.

Operators V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-3

Control Structures

Example Programs on ‘if’:

1. Program to check whether the given number is even or od using ‘if’:

#include<stdio.h>
main()
{
int n;

printf("Enter a Number:");
scanf("%d",&n);

if(n%2==0)
printf("Even Number");

if(n%2!=0)
printf("Odd Number");
}

Output:
Enter a Number:7
Odd Number

2. Program to find biggest in three different numbers using ‘if’:

#include<stdio.h>
main()
{
int x,y,z,big;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

printf("Enter three different numbers:");


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

big=x;

if(y>big)
big=y;

if(z>big)
big=z;

printf("biggest number=%d",big);
}

Output:
Enter three different numbers:10 50 20
biggest number=50

Example Programs on ‘if else’:

1. Program to check whether the given number is even or odd using


‘if else’:
#include<stdio.h>
main()
{
int n;

printf("Enter a Number:");
scanf("%d",&n);

if(n%2==0)
printf("Even Number");
else
printf("Odd Number");
}

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

Output:
Enter a Number:8
Even Number

2. Program to check whether a person is eligible to vote or not:


#include<stdio.h>
main()
{
int age;

printf("Enter your age:");


scanf("%d",&age);

if(age>=18)
printf("Eligible to Vote");

else
printf("Not Eligible to Vote");
}

Output:
Enter your age:23
Eligible to Vote

3. Program to find biggest in two different numbers:


#include<stdio.h>
main()
{
int a,b;

printf("Enter two different numbers:");


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

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

if(a>b)
printf("%d is big",a);
else
printf("%d is big",b);
}

Output:
Enter two different numbers:20 10
20 is big

Example Programs on ‘if else if’:

1. Program to find result of a student in 3 subjects. Max marks are 100


for each subject.
Minimum 40 marks are required in each subject for pass.
If average is >=60 then result is “First Division”.
If average is between 50 to 59 then result is “Second Division”.
If average is between 40 to 49 then result is “Third Division”:

#include<stdio.h>
main()
{
int m,p,c,avrg;

printf("Enter 3 subjects marks:");


scanf("%d%d%d",&m,&p,&c);

avrg = (m+p+c)/3;

printf("average marks=%d\n",avrg);

if(m<40 || p<40 || c<40)


printf("Fail");
else if(avrg>=60)
printf("First Division");

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

else if(avrg>=50)
printf("Second Division");
else
printf("Third Division");
}

Output:
Enter 3 subjects marks:65 69 62
average marks=65
First Division

2. Program to check whether the given number is positive or negative


or zero:

#include<stdio.h>
main()
{
int n;

printf("Enter a number:");
scanf("%d",&n);

if(n>0)
printf("Positive");
else if(n<0)
printf("Negative");
else
printf("Zero");
}

Output:
Enter a number: -5
Negative

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

3. Program to find biggest in three different numbers:


#include<stdio.h>
main()
{
int x,y,z;

printf("Enter 3 different numbers:");


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

if(x>y && x>z)


printf("%d is big",x);
else if(y>x && y>z)
printf("%d is big",y);
else
printf("%d is big",z);
}

Output:
Enter 3 different numbers:20 30 10
30 is big

4. Program to demonstrate Math Functions:


#include<stdio.h>
#include<math.h>
main()
{
float k;

k=sqrt(100);
printf("squareroot of 100=%f\n",k);

k=pow(2,3);
printf("2 power 3 = %f \n",k);

printf("5 power 3=%f\n",pow(5,3));

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

printf("sin90=%f \n",sin((90*3.14)/180));

k=cos((0*3.14)/180);
printf("cos0=%f\n",k);

k=tan((45*3.14)/180);
printf("tan45=%f",k);
}

Output:
squareroot of 100=10.000000
2 power 3 = 8.000000
5 power 3=125.000000
sin90=1.000000
cos0=1.000000
tan45=0.999204

5. Program to find roots of quadratic equation:


#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float k;

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

k=pow(b,2)-4*a*c;

if(k==0)
printf("root=%f",-b/(2*a));
else if(k>0)
{

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

printf("root1=%f\n",(-b+sqrt(k))/(2*a));
printf("root2=%f\n",(-b-sqrt(k))/(2*a));
}
else
{
printf("root1=%f+i%f",-b/(2*a),sqrt(-k)/(2*a));
printf("root2=%f-i%f",-b/(2*a),sqrt(-k)/(2*a));
}

Output:
Enter a,b,c:2 5 -3
root1=0.500000
root2=-3.000000

Example programs on nested if:

1. Program to find biggest in three different numbers using ‘nested if’:

#include<stdio.h>
main()
{
int x,y,z;

printf("Enter three different numbers:");


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

if(x>y)
{
if(x>z)
printf("%d is big",x);
}
if(y>x)
{

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

if(y>z)
printf("%d is big",y);
}
if(z>x)
{
if(z>y)
printf("%d is big",z);
}
}

Output:
Enter three different numbers:40 20 60
60 is big

(OR)

#include<stdio.h>
main()
{
int x,y,z;

printf("Enter three different numbers:");


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

if(x>y)
{
if(x>z)
printf("%d is big",x);
else
printf("%d is big",z);
}
else
{
if(y>z)
printf("%d is big",y);

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

else
printf("%d is big",z);
}
}

Output:
Enter three different numbers:60 90 50
90 is big

2. Program to find result of a student using ‘nested if’:


#include<stdio.h>
main()
{
int m,p,c,avrg;

printf("Enter 3 subjects marks:");


scanf("%d%d%d",&m,&p,&c);

if(m>=40 && p>=40 && c>=40)


{
avrg = (m+p+c)/3;
printf("average marks=%d\n",avrg);

if(avrg>=60)
printf("First Division");

else if(avrg>=50)
printf("Second Division");

else
printf("Third Division");
}
else
printf("Fail");
}

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

Output:
Enter 3 subjects marks:70 65 80
average marks=71
First Division

Example Program on ternary operator:

1. Program to find biggest in three different numbers using Ternary


Operator [Conditional Operator]:
#include<stdio.h>
main()
{
int x,y,z,big;

printf("Enter three different numbers:");


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

big = x>y ? (x>z?x:z) : (y>z?y:z);

printf("biggest number=%d",big);
}

Output:
Enter three different numbers: 50 20 40
biggest number=50

Example programs on ‘switch’:

1. Program to check whether the given number is even or odd using


switch:
#include<stdio.h>
main()
{

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 12

int x;

printf("enter a number:");
scanf("%d",&x);

switch(x%2)
{
case 0:
printf("Even");
break;
case 1:
printf("Odd");
}
}

Output:
enter a number: 5
Odd

2. Program to print Month Name of given Month Number:


#include<stdio.h>
main()
{
int n;

printf("Enter month number:");


scanf("%d",&n);

switch(n)
{
case 1:
printf("January");
break;
case 2:
printf("February");

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 13

break;

//write case 3 to case 11

case 12:
printf("December");
break;
default:
printf("Invalid Month Number");
}
}

Output:
Enter month number:12
December

3. Program to perform Arithmetic Operations using ‘switch’:


#include<stdio.h>
main()
{
char op;
int x,y;

printf("Enter an Operator (+ - * /):");


scanf("%c",&op);

printf("Enter two numbers:");


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

switch(op)
{
case '+':
printf("sum=%d",x+y);
break;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 14

case '-':
printf("difference=%d",x-y);
break;

case '*':
printf("product=%d",x*y);
break;

case '/':
printf("Quotient=%d",x/y);
break;

default:
printf("Invalid Operator");

}
}

Output:
Enter an Operator (+ - * /):*
Enter two numbers:5 4
product=20

4. Program to check whether the given Alphabet is vowel or


consonant:
#include<stdio.h>
#include<stdlib.h>
main()
{
char alpha;

printf("Enter an Alphabet:");
scanf("%c",&alpha);

alpha=tolower(alpha);

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 15

switch(alpha)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("vowel");
break;
default:
printf("Consonant");
}
}

Output:
Enter an Alphabet: i
Vowel

5. Menu driven program using ‘switch’:


#include<stdio.h>
main()
{
int choice;

printf(" 1. Apple \n 2. Mango \n 3. Grapes \n");

printf("Enter your favorite fruit:");


scanf("%d",&choice);

switch(choice)
{
case 1:
printf("ur favorite fruit is Apple");
break;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 16

case 2:
printf("ur favorite fruit is Mango");
break;
case 3:
printf("ur favorite fruit is Grapes");
break;
default:
printf("Ur choice is not available in memu");
}
}

Output:
1. Apple
2. Mango
3. Grapes
Enter your favorite fruit:2
ur favorite fruit is Mango

Example Programs on ‘while’ loop:

1. Program to print first ‘n’ numbers:


#include<Stdio.h>
main()
{
int n,i;

i=1; //Initialization

printf("Enter n:");
scanf("%d",&n);

while(i<=n) //Condition
{
printf("%d\n",i);
i++; // Step

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 17

}
}

Output:
Enter n:5
1
2
3
4
5

2. Program to print first ‘n’ numbers in reverse order:


#include<stdio.h>
main()
{
int i,n;

printf("Enter n:");
scanf("%d",&n);

i=n;

while(i>=1)
{
printf("%d\n",i);
i--;
}
}

Output:
Enter n:4
4
3
2
1

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 18

3. Program to print multiplication table using while loop:


#include<stdio.h>
main()
{
int n,i;

printf("Enter n:");
scanf("%d",&n);

i=1;

while(i<=10)
{
printf(" %d * %d = %d \n",n,i,n*i);
i++;
}
}

Output:
Enter n:5
5*1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

4. Program to find factorial of a number using ‘while’ loop:


#include<stdio.h>
main()
{

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 19

int n,f=1,i;

printf("Enter n:");
scanf("%d",&n);

i=1;
while(i<=n)
{
f=f*i;
i++;
}
printf("%d ! = %d",n,f);
}

(Or)

#include<stdio.h>
main()
{
int n,f=1,i;

printf("Enter n:");
scanf("%d",&n);

i=n;
while(i>=1)
{
f=f*i;
i--;
}
printf("%d ! = %d",n,f);
}

Output:
Enter n:4
4 ! = 24

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 20

5. Program to count number of digits in given number:


#include<stdio.h>
main()
{
int n,count;

printf("Enter n:");
scanf("%d",&n);

count=0;

while(n!=0)
{
n=n/10;
count++;
}

printf("number of digits=%d",count);
}

Output:
Enter n:456
number of digits=3

6. Program to find sum of digits of a given number:


#include<stdio.h>
main()
{
int n,sum,d;

printf("Enter n:");
scanf("%d",&n);

sum=0;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 21

while(n!=0)
{
d=n%10;
sum=sum+d;
n=n/10;
}

printf("sum of digits=%d",sum);
}

Output:
Enter n:123
sum of digits=6

7. Program to print reverse number:


#include<stdio.h>
main()
{
int n,r,d;

printf("Enter n:");
scanf("%d",&n);

r=0;

while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}

printf("reverse number=%d",r);
}

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 22

Output:
Enter n:123
reverse number=321

8. Program to check whether the given number is palindrome or not:


#include<stdio.h>
main()
{
int n,r,d,temp;

printf("Enter n:");
scanf("%d",&n);

temp=n;

r=0;
printf("\nbefore entering into loop:\n");
printf("n=%d\n",n);
printf("temp=%d\n",temp);

while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
printf("\n\nafter loop:\n");
printf("n=%d\n",n);
printf("temp=%d\n",temp);

if(temp==r)
printf("Palindrome");
else
printf("Not a Palindrome");
}

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 23

Output:
Enter n:121

before entering into loop:


n=121
temp=121

after loop:
n=0
temp=121
Palindrome

9. Program to check whether the given number is Armstrong Number


or not:
#include<stdio.h>
#include<math.h>
main()
{
int n,sum,d,temp;

printf("Enter n:");
scanf("%d",&n);

temp=n;

sum=0;

while(n!=0)
{
d=n%10;
sum=sum+pow(d,3);//sum=sum+d*d*d;
n=n/10;
}

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 24

if(temp==sum)
printf("%d is Armstrong",temp);
else
printf("%d is Not An Armstrong",temp);
}

Output:
Enter n:153
153 is Armstrong

Example programs on ‘do while’ Loop:

1. Program to print first ‘n’ numbers using ‘do while’ Loop:


#include<stdio.h>
main()
{
int i,n;
printf("Enter n:");
scanf("%d",&n);

i=1;
do
{
printf("%d\n",i);
i++;
}while(i<=n);

Output:
Enter n:4
1
2
3
4

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 25

2. Program to print first ‘n’ numbers in reverse order using ‘do while’
loop:
#include<stdio.h>
main()
{
int i,n;

printf("Enter n:");
scanf("%d",&n);

i=n;

do
{
printf("%d\n",i);
i--;
}while(i>=1);

Output:
Enter n:4
4
3
2
1

Example Programs on ‘for’ Loop:

1. Program to print first ‘n’ numbers:


#include<stdio.h>
main()
{
int n,i;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 26

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
printf("%d\n",i);
}

Output:
Enter n:4
1
2
3
4

2. Program to print first ‘n’ numbers in reverse order:


#include<stdio.h>
main()
{
int n,i;

printf("Enter n:");
scanf("%d",&n);

for(i=n;i>=1;i--)
printf("%d\n",i);
}

Output:
Enter n:4
4
3
2
1

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 27

3. Program to print first ‘n’ Even Numbers:


#include<stdio.h>
main()
{
int i,n;

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
printf("%d\n",2*i);
}

Output:
Enter n:5
2
4
6
8
10

4. Program to print first ‘n’ Odd Numbers:

#include<stdio.h>
main()
{
int i,n;

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
printf("%d\n",2*i-1);
}

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 28

Output:
Enter n:5
1
3
5
7
9

5. Program to print even numbers between 2 to n:


#include<stdio.h>
main()
{
int i,n;

printf("Enter n:");
scanf("%d",&n);

for(i=2;i<=n;i=i+2)
printf("%d\n",i);
}

Output:
Enter n:10
2
4
6
8
10

6. Program to print odd numbers between 1 to n:


#include<stdio.h>
main()
{
int i,n;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 29

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i=i+2)
printf("%d\n",i);
}

Output:
Enter n:10
1
3
5
7
9

7. Program to print even and odd numbers between 1 to n and count


number of even numbers & odd numbers:
#include<stdio.h>
main()
{
int i,n, ce=0, co=0;

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
if(i%2==1)
{
printf("%d is odd\n",i);
co++;

}
else

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 30

{
printf("%d is even\n",i);
ce++;
}

}
printf("number of even numbers=%d\n",ce);
printf("number of odd numbers=%d",co);
}

Output:
Enter n:10
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
number of even numbers=5
number of odd numbers=5

8. Printing 10 to 100 by taking 10 as step:


#include<stdio.h>
main()
{
int i;

for(i=10;i<=100;i=i+10)
printf("%d\n",i);
}

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 31

Output:
10
20
30
40
50
60
70
80
90
100

9. Program to find sum of first ‘n’ numbers (or)


Program to find sum of the series 1+2+3+………….+n :
#include<stdio.h>
main()
{
int n,i,sum;

printf("Enter n:");
scanf("%d",&n);

sum=0;

for(i=1;i<=n;i++)
sum=sum+i;

printf("sum=%d",sum);
}

Output:
Enter n:5
sum=15

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 32

10. Program to find sum of squares of first ‘n’ numbers (Or)


Program to find sum of the series 12+22+32+……….+n2 :
#include<stdio.h>
main()
{
int n,i,sum;

printf("Enter n:");
scanf("%d",&n);

sum=0;

for(i=1;i<=n;i++)
sum=sum+i*i;

printf("sum=%d",sum);
}

Output:
Enter n:4
sum=30

11. Program to find sum of the series 12-22+32-42……….n2 :


#include<stdio.h>
main()
{
int n,i,sum,x=1;

printf("Enter n:");
scanf("%d",&n);

sum=0;

for(i=1;i<=n;i++)
{

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 33

sum=sum+i*i*x;
x=-x;
}
printf("sum=%d",sum);
}

Output:
Enter n:4
sum=-10

12. Program to print factors and number of factors of a given number:


#include<stdio.h>
main()
{
int n,i,count;
printf("Enter n:");
scanf("%d",&n);
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
printf("%d\n",i);
count++;
}
}
printf("number of factors=%d",count);
}
Output:
Enter n:6
1
2
3
6
number of factors=4

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 34

13. Program to check whether the given number is prime number or


not:
#include<stdio.h>
main()
{
int n,i,count;

printf("Enter n:");
scanf("%d",&n);

count=0;

for(i=1;i<=n;i++)
{
if(n%i==0)
count++;

printf("number of factors=%d\n",count);

if(count==2)
printf("%d is prime number",n);
else
printf("%d is not a prime number",n);

Output:
Enter n:7
number of factors=2
7 is prime number

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 35

14. Program to check whether the given number is perfect number or


not:
#include<stdio.h>
main()
{
int n,i,sum;

printf("Enter n:");
scanf("%d",&n);

sum=0;

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

if(sum==n)
printf("%d is perfect number",n);
else
printf("%d is not a perfect number",n);

Output:
Enter n:6
6 is perfect number

15. Program to find power value without using pow() function:


#include<stdio.h>
main()
{
int n,p,r=1,i;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 36

printf("Enter a number AND power:");


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

for(i=1;i<=p;i++)
r=r*n;

printf(" %d power %d=%d",n,p,r);


}

Output:
Enter a number AND power:2 3
2 power 3=8

16. Program to print Fibonacci Series:


#include<stdio.h>
main()
{
int x=-1,y=1,i,f,n;

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
f=x+y; //pv+cv
printf("%d\n",f); // cv
x=y;
y=f;
}
}

Output:
Enter n:10
0

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 37

1
1
2
3
5
8
13
21
34

17. Program to print Multiplication Table of given number using for:


#include<stdio.h>
main()
{
int n,i;

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=10;i++)
printf("%d * %d = %d\n",n,i,n*i);
}

Output:
Enter n:3
3*1=3
3*2=6
3*3=9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 38

18. Program to find factorial of a given number using for:


#include<stdio.h>
main()
{
int n,i,f=1;

printf("Enter n:");
scanf("%d",&n);

//for(i=1;i<=n;i++) (or)
for(i=n;i>=1;i--)
f=f*i;

printf("%d! = %d",n,f);
}

Output:
Enter n:4
4! = 24

Example programs on goto:

1. Program to print first ‘n’ numbers using ‘goto’:


#include<stdio.h>
main()
{
int i,n;

printf("Enter n:");
scanf("%d",&n);

i=1;
nareshit:
printf("%d\n",i);
i++;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 39

if(i<=n)
goto nareshit;
}

Output:
Enter n:5
1
2
3
4
5

2. Program to print first ‘n’ numbers in reverse order:


#include<stdio.h>
main()
{
int i,n;
printf("Enter n:");
scanf("%d",&n);
i=n;
nareshit:
printf("%d\n",i);
i --;
if(i>=1)
goto nareshit;
}

Output:
Enter n:5
5
4
3
2
1

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 40

3. Program to print multiplication table using goto:


#include<stdio.h>
main()
{
int n,i=1;

printf("Enter n:");
scanf("%d",&n);

abc:

printf("%d * %d = %d\n",n,i,n*i);
i++;

if(i<=10)
goto abc;
}

Output:
Enter n:5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

4. Program to resist the user from entering negative value:


#include<stdio.h>
main()
{

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 41

int n;

xyz:

printf("Enter a +ve value:");


scanf("%d",&n);

if(n<0)
goto xyz;

printf("%d",n);

Output:
Enter a +ve value:-5
Enter a +ve value:-12
Enter a +ve value:-20
Enter a +ve value:-8
Enter a +ve value:25
25

5. Program to get Absolute Value using goto [Jumping Forward]:


#include<stdio.h>
main()
{
int n;

printf("enter a +ve value:");


scanf("%d",&n);

if(n>0)
goto abcd;

n=-n;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 42

abcd:
printf("%d",n);
}

Output:
enter a +ve value:5
5

Example programs on ‘break’:

1. Program to demonstrate terminating the loop in the middle of


execution using ‘break’:
#include<stdio.h>
main()
{
int i;
for(i=1;i<=10;i++)
{
printf("%d\n",i);
if(i==5)
break;
}
}

Output:
1
2
3
4
5

2. Program to check whether a given number is prime or not:


#include<stdio.h>
main()

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 43

{
int n,i,found;

printf("Enter n:");
scanf("%d",&n);

found=0;

for(i=2;i<n;i++)
{
if(n%i==0)
{
found=1;
break;
}
}
if(found==0)
printf("%d is prime number",n);
else
printf("%d is not a prime number",n);
}

Output:
Enter n:7
7 is prime number

Example programs on ‘continue’:

1. Program to demonstrate continue:


#include<stdio.h>
main()
{
int i;

for(i=1;i<=10;i++)

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 44

{
if(i==5 || i==7)
continue;
printf("%d\n",i);
}
}

Output:
1
2
3
4
6
8
9
10

2. Using continue in ‘while’ loop:


#include<stdio.h>
main()
{
int i=1;

while(1) //true
{
printf("%d\n",i);
i++;
if(i<=5)
continue;
break;
}
}

Output:
1

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 45

2
3
4
5

Example Programs on Nested Loops:

1. Program to print 5 stars for 10 lines:


#include<stdio.h>
main()
{
int i,j;

for(i=1;i<=10;i++)
{
for(j=1;j<=5;j++)
printf(" * ");
printf("\n");
}
}

Output:

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

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 46

2. Program to print prime numbers between the given range:


#include<stdio.h>
main()
{
int n,count,i,s,e;

printf("enter starting and ending numbers:");


scanf("%d%d",&s,&e);

for(n=s;n<=e;n++)
{
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("%d\n",n);
}
}

Output:
enter starting and ending numbers: 1 20
2
3
5
7
11
13
17
19

3. Program to print Perfect Numbers between the given range:


#include<stdio.h>

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 47

main()
{
int n, sum, i,s,e;

printf("Enter starting and ending values:");


scanf("%d%d",&s,&e);

for(n=s;n<=e;n++)
{

sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("%d\n",n);
}
}

Output:
Enter starting and ending values: 1 10000
6
28
496
8128

4. Program to print Armstrong Numbers between the given range:


#include<stdio.h>
main()
{
int n,temp,sum,d,s,e;

printf("Enter starting and ending value:");

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 48

scanf("%d%d",&s,&e);

for(n=s;n<=e;n++)
{
temp=n;
sum=0;
while(n!=0)
{
d=n%10;
sum=sum+d*d*d;
n=n/10;
}
if(sum==temp)
printf("%d\n",temp);

n=temp;

}
}

Output:
Enter starting and ending value:1 10000
1
153
370
371
407

5. Program to print following pattern:


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

#include<stdio.h>

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 49

main()
{
int i,j,n;

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf(" * ");
printf("\n");
}
}

Output:
Enter n:5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

6. Program to print following pattern:


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

#include<stdio.h>
main()
{
int n,i,j;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 50

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i+j>=n+1)
printf(" * ");
else
printf(" ");
}
printf("\n");
}
}

Output:
Enter n:5
*
* *
* * *
* * * *
* * * * *

7. Program to print following pattern:


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

#include<stdio.h>
main()
{
int i,j,n;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 51

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(" * ");
printf("\n");
}
}

Output:
Enter n:5
*
* *
* * *
* * * *
* * * * *

8. Program to print following pattern:


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

#include<stdio.h>
main()
{
int n,i,j;

printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 52

{
for(j=1;j<=n;j++)
{
if(i==1 || i==n || j==1 || j==n)
printf(" * ");
else
printf(" ");
}
printf("\n");
}
}

Output:

Enter n:5
* * * * *
* *
* *
* *
* * * * *

9. Program to print following pattern:


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

#include<stdio.h>
main()
{
int n,i,j;

printf("Enter n:");
scanf("%d",&n);

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 53

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(j==1 || i==n || i==j )
printf(" * ");
else
printf(" ");
}
printf("\n");
}
}

Output:
Enter n:5
*
* *
* *
* *
* * * * *

10. Program to print following pattern:


*
*
* * * * *
*
*

#include<stdio.h>
main()
{
int n,i,j,p;

printf("Enter n:");
scanf("%d",&n);

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 54

p=(n/2)+1;

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==p || j==p)
printf(" * ");
else
printf(" ");
}
printf("\n");
}
}

Output:
Enter n:5
*
*
* * * * *
*
*

11. Program to print following pattern:


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

#include<stdio.h>
main()
{
int n,i,j,p;

printf("Enter n:");

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 55

scanf("%d",&n);

p=(n/2)+1;

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==p || j==p || (i==1 && j<=p) || (i==n && j>=p)
|| (j==1 && i>=p) || (j==n && i<=p))
printf(" * ");
else
printf(" ");
}
printf("\n");
}
}
Enter n:5
* * * *
* *
* * * * *
* *
* * * *

12. Program to print following pattern:


+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +

#include<stdio.h>
main()
{
int n,i,j,p;

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 56

printf("Enter n:");
scanf("%d",&n);

p=(n/2)+1;

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if((i+j)%2==0)
printf(" + ");
else
printf(" - ");
}
printf("\n");
}
}

Output:
Enter n:5
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +

13. Program to print following pattern:


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 57

#include<stdio.h>
main()
{
int n,i,j;

printf("Enter n:");
scanf("%d",&n);

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

Output:
Enter n:5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

14. Program to print following pattern:


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

#include<stdio.h>
main()
{

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 58

int n,i,j;

printf("Enter n:");
scanf("%d",&n);

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

Output:
Enter n:5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Control Structures V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-4

Functions

1. No Argument, No Return:

#include<stdio.h>
void add(); // Function Declaration (or) Function Prototype
void add() //Function Definition
{
int x,y;

printf("Enter x,y:");
scanf("%d%d",&x,&y);

printf("sum=%d\n",x+y);
}
main()
{
add(); //Call of Function
add();
add();
add();
}

Output:
Enter x,y:10 20
sum=30
Enter x,y:5 4
sum=9
Enter x,y:20 40
sum=60
Enter x,y:30 50
sum=80

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

2. Argument, No Return:

#include<stdio.h>
void add(int,int);
void add(int x,int y)
{
printf("sum=%d\n",x+y);
}
main()
{
add(5,4);
add(60,40);
add(20,40);
add(100,600);
}

Output:
sum=9
sum=100
sum=60
sum=700

3. No Argument, Return:

#include<stdio.h>
int add();
int add()
{
int x,y;

printf("Enter x,y:");
scanf("%d%d",&x,&y);

return x+y;
}

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

main()
{
int k;
k=add();
printf("sum=%d\n",k);

k=add();
printf("sum=%d\n",k);

printf("sum=%d\n",add());

printf("sum=%d\n",add());
}

Output:
Enter x,y:10 20
sum=30
Enter x,y:5 4
sum=9
Enter x,y:3 2
sum=5
Enter x,y:6 8
sum=14

4. Argument, Return:

int add(int,int);
#include<stdio.h>

int add(int x,int y)


{
return x+y;
}

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

main()
{
int k;

k=add(1,2);
printf("sum=%d\n",k);

printf("sum=%d",add(4,3));
}

Output:
sum=3
sum=7

5. Write a function to find average of three numbers:

#include<stdio.h>
float average(int,int,int);
float average(int x,int y,int z)
{
return (float)(x+y+z)/3;
}
main()
{
float k;
k=average(10,31,20);
printf("average=%f",k);

printf("\naverage=%f",average(20,55,39));
}

Output:
average=20.333334
average=38.000000

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

6. Write a Function to find factorial of a number:


#include<stdio.h>
int fact(int);
int fact(int n)
{
int f=1,i;

for(i=1;i<=n;i++)
f=f*i;

return f;
}
main()
{
printf("4!=%d\n",fact(4));
printf("5!=%d\n",fact(5));
}

Output:
4!=24
5!=120

7. Write a Function to find power values:

#include<stdio.h>
int power(int,int);

int power(int n,int p)


{
int r=1,i;
for(i=1;i<=p;i++)
r=r*n;
return r;
}

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

main()
{
int k;
k=power(2,3);
printf("2 power 3=%d\n",k);

printf("5 power 2 =%d",power(5,2));


}

Output:
2 power 3=8
5 power 2 =25

8. Write a function to check whether the number is even or odd


using Argument, No Return style:

#include<stdio.h>
void iseven(int);
void iseven(int n)
{
if(n%2==0)
printf("%d is even\n",n);
else
printf("%d is odd\n",n);
}
main()
{
iseven(4);
iseven(3);
}

Output:
4 is even
3 is odd

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

9. Write a function to check whether the number is even or odd


using Argument, Return style:

#include<stdbool.h>
#include<stdio.h>
bool iseven(int);

bool iseven(int n)
{
if(n%2==0)
return true;
else
return false;
}

main()
{
bool b;
b=iseven(7);
if(b)
printf("even");
else
printf("odd");
}

Output:
odd

10. Write a function to check whether the number is prime or not:

#include<stdbool.h>
#include<stdio.h>
bool isprime(int);

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

bool isprime(int n)
{
int count=0,i;
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
main()
{
bool k;

k=isprime(7));
if(k)
printf("prime number");
else
printf("not a prime number");
}
Output:
Prime number

11. Write a function to print multiplication table of a number:

#include<stdio.h>
void mtable(int);
void mtable(int n)
{
int i;
for(i=1;i<=10;i++)
printf("%d*%d=%d\n",n,i,n*i);
}

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

main()
{
mtable(5);
}
Output:
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

12. Write the functions to find biggest and smallest in two different
numbers:

#include<stdio.h>
int max(int,int);
int min(int,int);
int max(int x,int y)
{
return x>y?x:y;
}
int min(int x,int y)
{
return x<y?x:y;
}
main()
{
printf("max value=%d\n",max(10,20));
printf("min value=%d\n",min(10,20));
}

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

Output:
max value=20
min value=10

13. Write the functions to find square and cube values:

#include<stdio.h>
int square(int);
int cube(int);
int square(int n)
{
return n*n;
}
int cube(int n)
{
return n*n*n;
}
main()
{
printf("%d\n",square(5));
printf("%d\n",cube(5));
}

Output:
25
125

Programs on Recursion:

1. Write a program to find factorial vale using Recursion:

#include<stdio.h>
#include<stdio.h>
int fact(int);

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}
main()
{
int k;
k=fact(4);
printf("4!=%d",k);
}
Output:
4!=24

2. Write a program to find power value using Recursion:

#include<stdio.h>
int power(int,int);
int power(int n,int p)
{
if(p==0)
return 1;
else
return n*power(n,p-1);
}
main()
{
int k;
k=power(2,3);
printf("2 power 3=%d",k);
}
Output:
2 power 3=8

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 12

3. Write a program to sum of digits using recursion:


#include<stdio.h>
int sum(int);
int sum(int n)
{
if(n==0)
return 0;
else
return n%10+sum(n/10);
}
main()
{
int k;
k=sum(456);
printf("sum of digits=%d",k);
}
Output:
sum of digits=15

4. Write a program to print Fibonacci Series using Recursion:

#include<stdio.h>
int fibo(int n)
{
if(n==0 || n==1)
return n;
else
return fibo(n-1)+fibo(n-2);
}
main()
{
int i;
for(i=0;i<10;i++)
printf("%d\n",fibo(i));
}

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 13

Output:
0
1
1
2
3
5
8
13
21
34

Programs on Storage Classes:

1. Program to demonstrate “auto” storage class:

#include<stdio.h>
void f1();
main()
{
auto int a=100; //auto variable
f1();
printf("%d\n",a); //100

// printf("%d\n",x);

/* Uncomment above staement and run it.


Gives Error.Because
we cannot access x variable of f1() in main(). */
}
void f1()
{
auto int x=200; //auto variable
printf("%d\n",x); //200

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 14

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

/* Uncomment above staement and run it.


Gives Error. Because
we cannot access a variable of main() in f1(). */
}
Output:
200
100

2. Program to demonstrate “extern” storage class:


#include<stdio.h>
void f1();
void f2();
int x=50; //Extern Variable (or) Global Variable
void main()
{
printf("from main:%d\n",x);
x=100;
f1();
}
void f1()
{
printf("from f1: %d\n",x);
x=200;
f2();
}
void f2()
{
printf("from f2: %d\n",x);
}

Output:
from main:50
from f1: 100
from f2: 200

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 15

3. auto VS extern [Local VS Global]:

int x=60; //global variable [extern]


void f1();
void f2();
#include<stdio.h>
main()
{
f2();
f1();
printf("%d\n",x); //prints 200. It is Modified global value
}
void f1()
{
int x=50; //local variable [auto]
printf("%d\n",x); //prints 50. Takes local value.
x=100; //Modifies local x
}
void f2()
{
printf("%d\n",x); // prints 60. Global value
x=200; // Modifies global x
}

Output:
60
50
200

4. Program to demonstrate static local variable:

void f1();
static int x=0;

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 16

void f1()
{
static int x=0; //static local variable
x++;

printf("%d\n",x);
}
main()
{
int i;

for(i=1;i<=5;i++)
f1();
}

Output:
1
2
3
4
5

5. Program to demonstrate static global variable:

#include<stdio.h>
void f1();
static int x=0; //static global variable

void f1()
{
x++;
printf("function call number is %d\n",x);
}

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 17

main()
{
int i;

for(i=1;i<=5;i++)
f1();

printf("number of function calls=%d",x);


}

Output:
function call number is 1
function call number is 2
function call number is 3
function call number is 4
function call number is 5
number of function calls=5

6. Program to demonstrate “register” storage class:

#include<stdio.h>
main()
{
register int i;

for(i=1;i<=5;i++)
printf("%d\n",i);
}

Output:
1
2
3
4
5

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 18

7. Program to get default values of all storage classes:

int e; //extern
main()
{
int a; //auto
static int s; //static
register int r; //register

printf("auto default=%d\n",a);
printf("extern default=%d\n",e);
printf("static default=%d\n",s);
printf("register default=%d\n",r);
}

Output:
auto default=0
extern default=0
static default=0
register default=1

Functions V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-5

Arrays

1. Program to demonstrate Initializing Single Dimensional Array:

#include<stdio.h>
main()
{
int x[5] = {10,15,30,25,20},i;

printf("%d\n\n",x[2]); //30

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

Output:
30

10
15
30
25
20

2. Program to read and print single dimensional array:

#include<stdio.h>
main()
{
int x[10],n,i;

printf("Enter number of elements:");

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

scanf("%d",&n);

printf("Enter x-array elements:");


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

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

Output:
Enter number of elements:5
Enter x-array elements:15 10 25 20 30
15
10
25
20
30

3. Program to assign one array to another:

#include<stdio.h>
main()
{
int x[10],y[10],n,i;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

for(i=0;i<n;i++)
y[i]=x[i];

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

printf("x array\t y-array\n");


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

Output:
Enter number of elements:5
Enter x-array elements:10 50 20 40 30
x array y-array
----------------------------------
10 10
50 50
20 20
40 40
30 30

4. Program to print array elements in reverse order:

#include<stdio.h>
main()
{
int x[10],n,i;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

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

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

Output:
Enter number of elements:4
Enter x-array elements:20 40 30 80
80
30
40
20

5. Program to assign one array to another in reverse order:

#include<stdio.h>
main()
{
int x[10],y[10],n,i;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

for(i=0;i<n;i++)
y[n-i-1]=x[i];

printf("x array\t y-array\n");


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

Output:
Enter number of elements:4
Enter x-array elements:10 20 30 40

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

x array y-array
------------------------------------------------
10 40
20 30
30 20
40 10

6. Program to append two arrays:


#include<stdio.h>
main()
{
int x[10],y[10],z[20],n,i;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

printf("Enter y-array elements:");


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

for(i=0;i<n;i++)
{
z[i]=x[i];
z[n+i]=y[i];
}

for(i=0;i<2*n;i++)
printf("%d\n",z[i]);
}

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

Output:
Enter number of elements:4
Enter x-array elements:10 20 30 40
Enter y-array elements:11 22 33 44
10
20
30
40
11
22
33
44

7. Program to append two arrays alternatively:

#include<stdio.h>
main()
{
int x[10],y[10],z[20],n,i;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

printf("Enter y-array elements:");


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

for(i=0;i<n;i++)
{
z[2*i]=x[i];
z[2*i+1]=y[i];

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

for(i=0;i<2*n;i++)
printf("%d\n",z[i]);
}

Output:
Enter number of elements:4
Enter x-array elements:10 20 30 40
Enter y-array elements:11 22 33 44
10
11
20
22
30
33
40
44

8. Program to find sum of elements of the Array:

#include<stdio.h>
main()
{
int x[10],n,i,sum=0;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

for(i=0;i<n;i++)
sum=sum+x[i];

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

printf("sum of elements=%d",sum);
}

Output:
Enter number of elements:4
Enter x-array elements:10 20 30 40
sum of elements=100

9. Program to find maximum and minimum value in the array:

#include<stdio.h>
main()
{
int x[10],n,i,max,min;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

max=x[0];
min=x[0];

for(i=1;i<n;i++)
{
if(x[i]>max)
max=x[i];
if(x[i]<min)
min=x[i];
}

printf("maximum value=%d\n",max);

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

printf("minimum value=%d",min);
}

Output:
Enter number of elements:4
Enter x-array elements:10 50 20 15
maximum value=50
minimum value=10

10. Program to sort the array elements using Bubble Sort:

#include<stdio.h>
main()
{
int x[10],n,i,j,temp;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

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

Output:
Enter number of elements:5
Enter x-array elements:6 8 7 9 2
2
6
7
8
9

11. Program to search for an element in the array using Linear Search:

#include<stdio.h>
main()
{
int x[10],n,e,i,found;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

printf("Enter the searching element:");


scanf("%d",&e);

found=0;

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

for(i=0;i<n;i++)
{
if(e==x[i])
{
found=1;
break;
}
}

if(found==1)
printf("%d is existed in array",e);
else
printf("%d is not existed in array",e);
}

Output:
Enter number of elements:5
Enter x-array elements:10 50 20 90 30
Enter the searching element:20
20 is existed in array

12. Program to search for an element in the array using Binary Search:

#include<stdio.h>
main()
{
int x[10],n,e,i,found,l,h,mid;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements in ascending order:");


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

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 12

printf("Enter the searching element:");

scanf("%d",&e);
found=0;

l=0;
h=n-1;

while(l<=h)
{
mid=(l+h)/2;
if(e==x[mid])
{
found=1;
break;
}
else if(e>x[mid])
l=mid+1;
else
h=mid-1;
}
if(found==1)
printf("%d is Found",e);
else
printf("%d is not Found",e);
}

Output:
Enter number of elements:5
Enter x-array elements in ascending order:10 20 30 40 50
Enter the searching element:30
30 is Found

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 13

13. Program to modify an element in the array:

#include<stdio.h>
main()
{
int x[10],n,i,p,e;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

printf("Enter the position to be modified:");


scanf("%d",&p);

printf("Enter new element:");


scanf("%d",&e);

x[p-1]=e;

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

Output:
Enter number of elements:5
Enter x-array elements:10 20 50 40 30
Enter the position to be modified:2
Enter new element:100
10
100
50
40
30

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 14

14. Program to insert an element in the array:

#include<stdio.h>
main()
{
int x[10],n,i,p,e;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

printf("Enter the position to be inserted:");


scanf("%d",&p);

printf("Enter new element:");


scanf("%d",&e);

for(i=n;i>=p;i--)
x[i]=x[i-1];

x[p-1]=e;

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

Output:
Enter number of elements:5
Enter x-array elements:10 20 50 40 30
Enter the position to be inserted:2
Enter new element:100
10
100

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 15

20
50
40
30

15. Program to delete an element in the array:

#include<stdio.h>
main()
{
int x[10],n,p,i;

printf("Enter number of elements:");


scanf("%d",&n);

printf("Enter x-array elements:");


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

printf("Enter the position to be deleted:");


scanf("%d",&p);

for(i=p-1;i<n-1;i++)
x[i]=x[i+1];

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

Output:
Enter number of elements:5
Enter x-array elements:10 20 50 40 30
Enter the position to be deleted:2
10
50

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 16

40
30

16. Program to demonstrate initializing two-dimensional array:

#include<stdio.h>
main()
{
int x[][3]={ {10,20,30},
{40,50,60} },i,j;

printf("%d\n\n",x[0][2]);

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

Output:
30

10 20 30
40 50 60

17. Program to read and print square matrix:

#include<stdio.h>
main()
{
int x[5][5],n,i,j;

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 17

printf("Enter number of rows or columns:");


scanf("%d",&n);

printf("Enter x-matrix elements:");


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

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

Output:
Enter number of rows or columns:2
Enter x-matrix elements:10 20 15 25
10 20
15 25

18. Program to read and print rectangle matrix:

#include<stdio.h>
main()
{
int x[5][5],y[5][5],m,n,i,j;

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


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

printf("Enter x-matrix elements:");


for(i=0;i<m;i++)

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 18

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

printf("\nX-Matrix elements are:\n");


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

Output:
Enter number of rows and columns:2 3
Enter x-matrix elements:10 20 30 40 50 60

X-Matrix elements are:


10 20 30
40 50 60

19. Program to assign one matrix to another:

#include<stdio.h>
main()
{
int x[5][5],y[5][5],m,n,i,j;

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


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

printf("Enter x-matrix elements:");


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

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 19

for(i=0;i<m;i++)
for(j=0;j<n;j++)
y[i][j]=x[i][j];

printf("\nY-Matrix elements are:\n");


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

Output:
Enter number of rows and columns:2 3
Enter x-matrix elements:10 20 30 40 50 60

Y-Matrix elements are:


10 20 30
40 50 60

20. Program to find transpose of given matrix:

#include<stdio.h>
main()
{
int x[5][5],y[5][5],m,n,i,j;

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


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

printf("Enter x-matrix elements:");


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

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 20

for(i=0;i<m;i++)
for(j=0;j<n;j++)
y[j][i]=x[i][j];

printf("\nX-Matrix elements are:\n");


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

printf("\nY-Matrix elements are:\n");


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

Output:
Enter number of rows and columns:2 3
Enter x-matrix elements:10 20 30 40 50 60

X-Matrix elements are:


10 20 30
40 50 60

Y-Matrix elements are:


10 40
20 50
30 60

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 21

21. Program to find sum of elements of two-dimensional array:

#include<stdio.h>
main()
{
int x[5][5],n,i,j,sum=0;

printf("Enter number of rows or columns:");


scanf("%d",&n);

printf("Enter x-matrix elements:");


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

for(i=0;i<n;i++)
for(j=0;j<n;j++)
sum=sum+x[i][j];

printf("sum of elements=%d",sum);
}

Output:
Enter number of rows or columns:2
Enter x-matrix elements:10 20 30 40
sum of elements=100

22. Program to find sum of elements of principal diagonal:

#include<stdio.h>
main()
{
int x[5][5],n,i,j,sum=0;

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 22

printf("Enter number of rows or columns:");


scanf("%d",&n);

printf("Enter x-matrix elements:");


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

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+x[i][j];
}
}
printf("sum of elements=%d",sum);
}

Output:
Enter number of rows or columns:3
Enter x-matrix elements:10 20 30 40 50 60 70 80 90
sum of elements=150

23. Program to find sum of elements of upper left triangle:

#include<stdio.h>
main()
{
int x[5][5],n,i,j,sum=0;

printf("Enter number of rows or columns:");


scanf("%d",&n);

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 23

printf("Enter x-matrix elements:");


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

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j<=n-1)
sum=sum+x[i][j];
}
}
printf("sum of elements=%d",sum);
}

Output:
Enter number of rows or columns:3
Enter x-matrix elements:10 20 30 40 50 60 70 80 90
sum of elements=220

24. Program to add two matrices:

#include<stdio.h>
main()
{
int x[5][5],y[5][5],z[5][5],m,n,i,j;

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


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

printf("Enter x-matrix elements:");


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

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 24

printf("Enter y-matrix elements:");


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

for(i=0;i<m;i++)
for(j=0;j<n;j++)
z[i][j] = x[i][j]+y[i][j];

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

Output:
Enter number of rows and columns:2 3
Enter x-matrix elements:10 20 30 40 50 60
Enter y-matrix elements:11 22 33 44 55 66
21 42 63
84 105 126

25. Program to multiply two matrices:

#include<stdio.h>
main()
{
int x[5][5],y[5][5],z[5][5],m,n,p,q,i,j,k;

printf("Enter x-matrix size:");


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

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 25

printf("Enter y-matrix size:");


scanf("%d%d",&p,&q);

if(n!=p)
printf("Matrix Multiplication is not possible");
else
{
printf("Enter x-matrx elements:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);

printf("Enter y-matrx elements:");


for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&y[i][j]);

for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
z[i][j]=0;
for(k=0;k<p;k++)
z[i][j] = z[i][j]+x[i][k]*y[k][j];
}
}

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

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 26

Output:
Enter x-matrix size:2 3
Enter y-matrix size:3 2
Enter x-matrx elements:10 20 30 40 50 60
Enter y-matrx elements:11 44 22 55 33 66
1540 3520
3520 8470

26. Program to demonstrate initializing three-dimensional array:

#include<stdio.h>
main()
{
int x[2][2][3] = { {{10,20,30},{40,50,60}},
{{11,22,33},{44,55,66}} };
int i,j,k;

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

Output:
10 20 30
40 50 60

11 22 33
44 55 66

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 27

27. Program to read and print three-dimensional array:

#include<stdio.h>
main()
{
int x[2][2][3];
int i,j,k;

printf("Enter x-matrix elements [12 elements]:\n");


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

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

Output:
Enter x-matrix elements [12 elements]:
10 20 30 40 50 60 11 22 33 44 55 66
10 20 30
40 50 60

11 22 33
44 55 66

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 28

28. Program to demonstrate four-dimensional array:


#include<stdio.h>
main()
{
int x[2][2][2][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24};
int i,j,k,l;

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
for(l=0;l<3;l++)
printf("%d\t",x[i][j][k][l]);
printf("\n");
}
printf("\n");
}
printf("..............................\n");
}
}
Output:
1 2 3
4 5 6

7 8 9
10 11 12
..............................
13 14 15
16 17 18

19 20 21
22 23 24
..............................

Arrays V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-6

Strings

1. Program to demonstrate Initializing and Printing a String:

#include<stdio.h>
main()
{
char x[20]={'r','a','j','u'};
int i;

printf("%s\n",x);

printf("%c\n",x[2]);

printf(x);

printf("\n");

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

for(i=0;x[i]!=0;i++)
printf("%c",x[i]);

printf("\n");

puts(x);
}

Output:
raju
j

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

raju
raju
raju
raju

2. Program to demonstrate initializing and printing a string:

#include<stdio.h>
main()
{
char x[10]="Raju";

printf("Hi %s",x);
}

Output:
Hi Raju

3. Program to read and print a string:

#include<stdio.h>
main()
{
char x[20];

printf("Enter your name:");


scanf("%s",x);

printf("Hi %s",x);
}

Output:
Enter your name:Ramu
Hi Ramu

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

4. Program to read and print a string:

#include<stdio.h>
main()
{
char x[20],y[20];

printf("Enter a String:");
scanf("%s",x);

printf("%s\n",x);

fflush(stdin);

printf("Enter a string:");
gets(y);
puts(y);
}

Output:
Enter a String:Ravi
Ravi
Enter a string:Krishna
Krishna

5. Program to find length of a string using strlen() function:

#include<stdio.h>
#include<string.h>
main()
{
char x[20];
int n;

printf("Enter a string:");

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

scanf("%s",x);

n=strlen(x);

printf("number of char=%d",n);
}

Output:
Enter a string:raju
number of char=4

6. Program to find length of a string without using strlen() function:

#include<stdio.h>
main()
{
char x[20];
int count;

printf("Enter a string:");
scanf("%s",x);

/* for(count=0;x[count]!=0;count++)
{
}

OR */

count=0;
while(x[count]!='\0')
{
count++;
}
printf("number of char=%d",count);
}

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

Output:
Enter a string:raju
number of char=4

7. Program to copy a string using strcpy() function:


#include<string.h>
#include<stdio.h>
main()
{
char x[20],y[20];

printf("Enter a string:");
scanf("%s",x);

strcpy(y,x);

printf(" x string=%s \n y string=%s",x,y);


}

Output:
Enter a string:raju
x string=raju
y string=raju

8. Program to copy a string without using strcpy() function:

#include<stdio.h>
main()
{
char x[20],y[20];
int i;

printf("Enter a string:");
scanf("%s",x);

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

for(i=0;x[i]!='\0';i++)
y[i]=x[i];

y[i]='\0';

printf(" x string=%s \n y string=%s",x,y);


}

Output:
Enter a string:ravi
x string=ravi
y string=ravi

9. Program to print reverse string using strrev() function:

#include<string.h>
#include<stdio.h>
main()
{
char x[20];

printf("Enter a string:");
scanf("%s",x);

strrev(x);

printf("reverse string:%s",x);

}
Output:
Enter a string:ramu
reverse string:umar

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

10. Program to print reverse string without using strrev() function:

#include<string.h>
#include<stdio.h>
main()
{
char x[20],y[20];
int i,n;

printf("Enter a string:");
scanf("%s",x);

n=strlen(x);

for(i=0;x[i]!=0;i++)
y[n-i-1]=x[i];

y[i]=0;

printf("reverse string:%s",y);

Output:
Enter a string:ramu
reverse string:umar

11. Program to concatenate two strings using strcat() function:

#include<string.h>
#include<stdio.h>
main()
{
char x[10],y[10];

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

printf("Enter two strings:");


scanf("%s%s",x,y);

strcat(x,y);

printf("concatenated string=%s",x);
}

Output:
Enter two strings:raj
kumar
concatenated string=rajkumar

12. Program to concatenate two strings without using strcat() function:

#include<string.h>
#include<stdio.h>
main()
{
char x[10],y[10],z[20];
int i,n;

printf("Enter two strings:");


scanf("%s%s",x,y);

n = strlen(x);

for(i=0;x[i]!=0;i++)
z[i]=x[i];

for(i=0;y[i]!=0;i++)
z[n+i]=y[i];

z[n+i]=0;

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

printf("concatenated string=%s",z);
}
Output:
Enter two strings:sai
kumar
concatenated string=saikumar

13. Program to compare two strings:

#include<string.h>
#include<stdio.h>
main()
{
char x[10],y[10];
int k;
printf("Enter two strings:");
scanf("%s%s",x,y);

k=strcmp(x,y);
printf("%d\n",k);
if(k>0)
printf("x is big");
else if(k<0)
printf("y is big");
else
printf("both are equal");
}

Output:
Enter two strings:raj
kumar
1
x is big

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

14. Program to check whether the given string is palindrome or not:

#include<string.h>
#include<stdio.h>
main()
{
char x[10],y[10];

printf("Enter a string:");
scanf("%s",x);

strcpy(y,x);

strrev(y);

if(strcmp(x,y)==0)
printf("Palindrome");
else
printf("Not a Palindrome");
}

Output:
Enter a string:liril
Palindrome

15. Program to convert the given string to lower case using strlwr()
function:

#include<stdio.h>
#include<string.h>
main()
{
char x[20];
printf("Enter a string:");
scanf("%s",x);

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

printf("Before calling strlwr=%s\n",x);

strlwr(x);

printf("After calling strlwr=%s\n",x);


}

Output:
Enter a string:RAMU
Before calling strlwr=RAMU
After calling strlwr=ramu

16. Program to convert the given string to upper case using strupr()
function:

#include<stdio.h>
#include<string.h>
main()
{
char x[20];

printf("Enter a string:");
scanf("%s",x);

printf("Before calling strupr=%s\n",x);

strupr(x);

printf("After calling strupr=%s\n",x);


}

Output:
Enter a string:ramu
Before calling strupr=ramu
After calling strupr=RAMU

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 12

17. Program to demonstrate initializing two-dimensional array:

#include<stdio.h>
main()
{
char ch;
int nw=0,nl=0,nc=0,nupr=0,nlwr=0,nd=0,ns=0;

printf("Enter Text [press F6 or ctrl+Z to stop]:\n");


while((ch=getchar())!=EOF)
{
if(ch==' ')
nw++;
else if(ch=='\n')
{
nl++;
nw++;
}
else if(ch>='A' && ch<='Z')
nupr++;
else if(ch>='a' && ch<='z')
nlwr++;
else if(ch>='0' && ch<='9')
nd++;
else
ns++;
}

printf("Number of Words=%d\n",nw);
printf("Number of Lines=%d\n",nl);
printf("Number of upper case chars=%d\n",nupr);
printf("Number of lower case chars=%d\n",nlwr);
printf("Number of digits=%d\n",nd);
printf("Number of special chars=%d",ns);

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 13

Output:
Enter Text [press F6 or ctrl+Z to stop]:
H! Every1,
How @re you?
^Z
Number of Words=5
Number of Lines=2
Number of upper case chars=3
Number of lower case chars=11
Number of digits=1
Number of special chars=4

18. Program to demonstrate initializing double dimensional character


array:

#include<stdio.h>
main()
{
char x[10][10]={"raju","kiran","sai","krishna"};
int i;

printf("%s\n\n",x[1]);

for(i=0;i<4;i++)
printf("%s\n",x[i]);
}

Output:
kiran

raju
kiran
sai
krishna

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 14

19. Program to read and print Double Dimensional Character Array:

#include<stdio.h>
main()
{
char x[10][10];
int i,n;

printf("Enter number of strings:");


scanf("%d",&n);

printf("Enter %d strings:\n",n);
for(i=0;i<n;i++)
scanf("%s",x[i]);

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

Output:
Enter number of strings:5
Enter 5 strings:
ravi sai sravan arun vijay
ravi
sai
sravan
arun
vijay

Strings V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-7

Pointers

1. Program to demonstrate pointers and printing addresses:

#include<stdio.h>
main()
{
int x=30,*p; // p -> pointer variable
float y=56.78,*q;

p=&x;

printf("size of p=%d\n",sizeof(p));
printf("size of q=%d\n",sizeof(q));

printf("value=%d\n",x); //30

printf("address=%d\n",&x);

printf("value=%d\n",*&x); //30

printf("address=%d\n",p);

printf("value=%d\n",*p); //30

*p = 50;
printf("%d",x);
}

Output:
size of p=8
size of q=8
value=30

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

address=6487564
value=30
address=6487564
value=30
50

2. Same Pointer variable can point to many data variables:

#include<stdio.h>
main()
{
int x=20,y=30,*p;

p=&x;
printf("%d\n",*p); //20

p=&y;
printf("%d\n",*p); //30
}

Output:
20
30

3. Many Pointer variables can point to single data variable:

#include<stdio.h>
main()
{
int x=30,*p1,*p2;

p1=&x;
p2=&x;

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

printf("%d\n",*p1);

printf("%d\n",*p2);
}

Output:
30
30

4. Pointer Arithmetic:

#include<stdio.h>
main()
{
short int x=15,*p;

p=&x;

printf("address=%d\n",p);

p++; //Pointer Arithmetic

printf("address=%d\n",p);
}

Output:
address=6487574
address=6487576

5. Pointer Arithmetic:

#include<stdio.h>
main()
{

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

int x[] = {10,20,30,40,50},*p;

p=x; // (or) p=&x[0];


printf("%d\n",*p); //10

p++; //Pointer Arithmetic


printf("%d\n",*p); //20

p=p+2; //Pointer Arithmetic


printf("%d\n",*p);

p--;
printf("%d\n",*p);

p=p-2;
printf("%d\n",*p); //10

p=p+3;
printf("%d\n",*p);
}

Output:
10
20
40
30
10
40

6. Pointer Arithmetic:

#include<stdio.h>
main()
{
short int x[] = {10,20,30,40,50},*p;

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

p=x;

printf("%d\n",*p);

p=p+4;
printf("%d\n",*p);

p--;
printf("%d\n",*p);

p=p-2;
printf("%d\n",*p);

p++;
printf("%d\n",*p);
}

7. Program to access single dimensional array using pointers:

//printing 1d-array using pointers

#include<stdio.h>
main()
{
int x[]={20,25,30,35,40},*p,i;

p=x;

for(i=0;i<5;i++)
printf("%d\n",*(x+i));
//printf("%d\n",*(p+i));
//printf("%d\n",*p++);
}

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

Output:
20
25
30
35
40

8. Program to assign one array to another using pointers:


#include<stdio.h>
main()
{
int x[]={10,20,30},y[5],*p,i;

p=x;

for(i=0;i<3;i++)
*(y+i)=*(x+i);

for(i=0;i<3;i++)
printf("%d\n",*(y+i));
}

Output:
10
20
30

9. Program to access two-dimensional array using pointers:

#include<stdio.h>
main()
{
int x[2][3] = {{10,20,30},{40,50,60}},*p,i,j;

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

p=x;

for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%d\t",*p++);
//printf("%d\t",*(*(x+i)+j));
printf("\n");
}
}

Output:
10 20 30
40 50 60

10. Program to access three-dimensional array using pointers:

#include<stdio.h>
main()
{
int x[2][2][3] = { {{10,20,30},{40,50,60}},
{{10,20,30},{40,50,60}} },*p,i,j,k;

p=x;

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<3;k++)
{
printf("%d\t",*(*(*(x+i)+j)+k));
//printf("%d\t",*p++);
}
printf("\n");

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

}
printf("\n");
}
}

Output:
10 20 30
40 50 60

10 20 30
40 50 60

11. Program to demonstrate pointer array [Array of Pointers]:

#include<stdio.h>
main()
{
int x[] = {10,20,30,40,50},*p[5],i;

for(i=0;i<5;i++)
p[i]=&x[i];

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

Output:
10
20
30
40
50

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

12. Pointer to Pointer:

#include<stdio.h>
main()
{
int x=50,*p1,**p2,***p3;

p1=&x;
p2=&p1;
p3=&p2;

printf("%d\n",x);
printf("%d\n",*p1);
printf("%d\n",**p2);
printf("%d",***p3);
}

Output:
50
50
50
50

13. Call By Value:

#include<stdio.h>
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
//printf("x=%d\ny=%d\n",x,y);
}

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

main()
{
int a=20,b=30;
swap(a,b); // call by value
printf("a=%d\nb=%d\n",a,b);
}

Output:
a=20
b=30

14. Call By Reference:

#include<stdio.h>
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
//printf("x=%d\ny=%d\n",x,y);
}
main()
{
int a=20,b=30;
swap(&a,&b); // call by reference
printf("a=%d\nb=%d\n",a,b);
}

Output:
a=30
b=20

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

15. Dynamic memory allocation using malloc():

#include<stdlib.h>
#include<stdio.h>
main()
{
int *p;

p = (int*)malloc(sizeof(int));

printf("Enter an Integer:");
scanf("%d",p);

printf("%d",*p);

free(p);
}

Output:
Enter an Integer:40
40

16. Dynamic memory allocation using calloc():

#include<stdio.h>
#include<stdlib.h>
main()
{
int *p;

p=(int*)calloc(10,sizeof(int));

*(p+0) = 40;
*(p+1) = 50;
*(p+2) = 30;

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 12

printf("%d\n",*(p+0));
printf("%d\n",*(p+1));
printf("%d\n",*(p+2));
}

Output:
40
50
30

Pointers V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-8

User-Defined Data Types


[Structures, Unions & Enumerations]

1. Program to demonstrate Initializing a structure variable:

#include<stdio.h>
struct student
{
int rno;
char name[20];
};
main()
{
struct student s1={25,"Sai"};

printf("%d\t%s",s1.rno,s1.name);
}

Output:
25 Sai

2. Program to demonstrate reading and printing structure elements:

#include<stdio.h>
struct student
{
int rno;
char name[20];
};
main()
{
struct student s1;

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

printf("Enter rno,name:");
scanf("%d%s",&s1.rno,s1.name);

printf("rno=%d\tname=%s\n",s1.rno,s1.name);
}

Output:
Enter rno,name:20 Arun
rno=20 name=Arun

3. Program to demonstrate typedef keyword:

#include<stdio.h>
main()
{
typedef int rama;
typedef rama krishna;
rama x=20;
krishna y=30;

printf("%d",x+y);
}

Output:
50

4. Program to assign one structure to another:

#include<stdio.h>
typedef struct employee
{
int empid;
char ename[20];
double sal;
}emp;

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

main()
{
emp e1,e2;

printf("Enter empid,ename, sal:");


scanf("%d%s%Lf",&e1.empid,e1.ename,&e1.sal);

e2 = e1;

printf("e1:\n%d\t%s\t%Lf\n",e1.empid,e1.ename,e1.sal);
printf("e2:\n%d\t%s\t%Lf\n",e2.empid,e2.ename,e2.sal);
}

Output:
Enter empid,ename, sal:1234 raju 5000
e1:
1234 raju 5000.000000
e2:
1234 raju 5000.000000

5. Structure as Argument:

#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
int marks;
}std;
void show(std);
void show(std s) //Structure as Argument
{
printf("%d\t%s\t%d\n",s.rno,s.name,s.marks);
}

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

main()
{
std s1;

printf("Enter rno,name and marks:\n");


scanf("%d%s%d",&s1.rno,s1.name,&s1.marks);

show(s1);
}

Output:
Enter rno,name and marks:
20 Sravan 500
20 Sravan 500

6. Returning Structure:

#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
int marks;
}std;
std f1();
std f1()
{
std s={12,"Vijay",456};

return s;
}
main()
{
std s1;

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

s1 = f1();

printf("%d\t%s\t%d",s1.rno,s1.name,s1.marks);
}

Output:
12 Vijay 456

7. Reading and Printing Structure Using Functions:


#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
}std;
std getstd();
void putstd(std);
void putstd(std);
std getstd()
{
std s;

printf("enter rno,name:");
scanf("%d%s",&s.rno,s.name);

return s;
}
void putstd(std a)
{
printf("%d\t%s\n",a.rno,a.name);
}
main()
{
std s1,s2,s3;

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

s1 = getstd();
s2 = getstd();
s3 = getstd();

putstd(s1);
putstd(s2);
putstd(s3);
}

Output:
enter rno,name:10 raju
enter rno,name:20 kiran
enter rno,name:30 vijay
10 raju
20 kiran
30 vijay

8. Array of Structure:

#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
int m1,m2,m3;
}std;
main()
{
std s[10]; //Array of Structure
int n,i;

printf("Enter number of students:");


scanf("%d",&n);

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

for(i=0;i<n;i++)
{
printf("Enter rno,name,3 subs marks:");

scanf("%d%s%d%d%d",&s[i].rno,s[i].name,&s[i].m1,&s[i].m2,
&s[i].m3);
}

for(i=0;i<n;i++)

printf("%d\t%s\t%d\t%d\t%d\n",s[i].rno,s[i].name,s[i].m1,s[i].
m2,s[i].m3);
}

Output:
Enter number of students:2
Enter rno,name,3 subs marks:10 raju 45 60 55
Enter rno,name,3 subs marks:20 kiran 70 90 80
10 raju 45 60 55
20 kiran 70 90 80

9. Structure of Array:

#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
int m[10]; //Structure of Array
}std;
main()
{
std s[10]; //Array of Structure
int n,i,j,ns;

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

printf("Enter number of students:");


scanf("%d",&n);

printf("Enter number of subjects:");


scanf("%d",&ns);

for(i=0;i<n;i++)
{
printf("Enter rno,name, %d subjects marks:",ns);
scanf("%d%s",&s[i].rno,s[i].name);
for(j=0;j<ns;j++)
scanf("%d",&s[i].m[j]);
}

for(i=0;i<n;i++)
{
printf("%d\t%s",s[i].rno,s[i].name);
for(j=0;j<ns;j++)
printf("\t%d",s[i].m[j]);
printf("\n");
}
}

Output:
Enter number of students:2
Enter number of subjects:3
Enter rno,name, 3 subjects marks:10 Raju 50 70 60
Enter rno,name, 3 subjects marks:20 Kiran 66 88 77
10 Raju 50 70 60
20 Kiran 66 88 77

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

10. Pointer to Structure:

#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
}std;
main()
{
std s1,*p;

p=&s1;

printf("enter rno,name:");
scanf("%d%s",&s1.rno,s1.name);

printf("%d\t%s\n",s1.rno,s1.name);

printf("%d\t%s\n",*&s1.rno,*&s1.name);

printf("%d\t%s\n",(*p).rno,(*p).name);

printf("%d\t%s",p->rno,p->name);
}

Output:
enter rno,name:10 Raju
10 Raju
10 Raju
10 Raju
10 Raju

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

11. Structure in Structure:

#include<stdio.h>
typedef struct person
{
int pid;
char pname[20];
}person;

typedef struct student


{
person p; //structure in structure
char sec;
int marks;
}std;

typedef struct employee


{
person p;
char job[20];
double sal;
}emp;

main()
{
std s1={123,"Sai",'A',456};
emp e1={1234,"Ravi","Clerk",5000};

printf("%d\t%s\t%c\t%d\n",s1.p.pid,s1.p.pname,s1.sec,s1.marks);
printf("%d\t%s\t%s\t%Lf",e1.p.pid,e1.p.pname,e1.job,e1.sal);
}

Output:
123 Sai A 456
1234 Ravi Clerk 5000.000000

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

12. Program to demonstrate union:

#include<stdio.h>
union aaa
{
int x;
double y;
};
main()
{
union aaa a1;

a1.x = 20;
printf("%d\n",a1.x); //20

a1.y = 30.5;
printf("%Lf",a1.y); //30.5
}

Output:
20
30.500000

13. Program to demonstrate union members share same memory


location:

union aaa
{
int x;
int y;
};
main()
{
union aaa a1;

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 12

a1.x = 20;
printf("%d\n",a1.y); //20

a1.y = 30;
printf("%d",a1.x); //30
}

Output:
20
30

14. Program to demonstrate enumeration:

#include<stdio.h>
enum colors
{
RED=10, BLUE=20,GREEN=30
};
typedef enum colors colors;
main()
{
colors c1,c2,c3;

c1=RED;
c2=BLUE;
c3=GREEN;

printf("%d\t%d\t%d",c1,c2,c3);
}

Output:
10 20 30

User-Defined Data Types V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-9

Files

1. Program to write text into file:

#include<stdio.h>
main()
{
FILE *fp;
char ch;

fp=fopen("demo.txt","w");

printf("Enter text [press f6 or ctrl+z]:\n");


while((ch=getchar())!=EOF)
{
putc(ch,fp);
}

fclose(fp);

printf("Text saved in file");

Output:
Enter text [press f6 or ctrl+z]:
Hello Everyone,
Have a nice day
^Z
Text saved in file

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

2. Program to append text to File:

#include<stdio.h>
main()
{
FILE *fp;
char ch;

fp=fopen("demo.txt","a");

printf("Enter text [press f6 or ctrl+z]:\n");

while((ch=getchar())!=EOF)
{
putc(ch,fp);
}

fclose(fp);

printf("Text saved in file");

Output:
Enter text [press f6 or ctrl+z]:
Hello..
This text will be appended
to existed data
^Z
Text saved in file

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

3. Program to read the text from file:

#include<stdio.h>
main()
{
FILE *fp;
char ch;

fp=fopen("demo.txt","r");

while((ch=getc(fp))!=EOF)
{
putchar(ch);
}
fclose(fp);
}

Output:
Hello Everyone,
Have a nice day
Hello..
This text will be appended
to existed data

4. Program to copy the contents from one file to another:

#include<stdio.h>
main()
{
FILE *fp1,*fp2;
char ch;

fp1=fopen("demo.txt","r");
fp2=fopen("aaa.txt","w");

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

while((ch=getc(fp1))!=EOF)
putc(ch,fp2);

printf("Contents copied into file");

fclose(fp1);
fclose(fp2);
}

Output:
Contents copied into file

5. Program to demonstrate fprintf():

#include<stdio.h>
main()
{
FILE *fp;
int rno;
char name[20];

fp=fopen("student.txt","a");

printf("Enter rno,name:");
scanf("%d%s",&rno,name);

fprintf(fp,"%d\t%s\n",rno,name);

printf("Record saved in file");


fclose(fp);
}

Output:
Enter rno,name:15 Ravi
Record saved in file

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

6. Program to demonstrate fscanf():

#include<stdio.h>
main()
{
FILE *fp;
int rno;
char name[20];

fp=fopen("student.txt","r");

fscanf(fp,"%d%s",&rno,name);

printf("%d\t%s",rno,name);

fclose(fp);
}

Output:
15 Ravi

7. Program to demonstrate writing structure into file and reading


structure from file:
#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
}std;
main()
{
std s,s1;
FILE *fp;
char ch;

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

fp=fopen("student1.xls","a+"); //append/read

do
{
printf("Enter rno,name:");
scanf("%d%s",&s.rno,s.name);
fprintf(fp,"%d\t%s\n",s.rno,s.name);
printf("Record saved in file");
fflush(stdin);
printf("\nDo you want to add one more record?[y/Y]:");
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');

rewind(fp);

while((fscanf(fp,"%d%s",&s1.rno,s1.name))!=EOF)
{
printf("%d\t%s\n",s1.rno,s1.name);
}
fclose(fp);
}

Output:
Do you want to add one more record?[y/Y]:y
Enter rno,name:2 kiran
Record saved in file
Do you want to add one more record?[y/Y]:y
Enter rno,name:3 ramu
Record saved in file
Do you want to add one more record?[y/Y]:n
1 raju
2 kiran
3 ramu

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

8. Program to demonstrate writing data into binary file & reading


data from binary file:

#include<stdio.h>
typedef struct student
{
int rno;
char name[20];
}std;
main()
{
std s,s1;
FILE *fp;
char ch;

fp = fopen("std.bin","a+b");
do
{
printf("Enter rno,name:");
scanf("%d%s",&s.rno,s.name);
fwrite(&s,sizeof(s),1,fp);
printf("Record Saved in File\n");
fflush(stdin);
printf("do u want to add one more rec?[y/Y]:");
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');

rewind(fp);

while(fread(&s1,sizeof(s1),1,fp))
{
printf("%d\t%s\n",s1.rno,s1.name);
}

fclose(fp);
}

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

Output:
Enter rno,name:1 raju
Record Saved in File
do u want to add one more rec?[y/Y]:y
Enter rno,name:2 sai
Record Saved in File
do u want to add one more rec?[y/Y]:y
Enter rno,name:3 vijay
Record Saved in File
do u want to add one more rec?[y/Y]:n
1 raju
2 sai
3 vijay

9. Random Accessing [ fseek() and ftell() ]:

//Note: “hello.txt” file has following data:

computer

#include<stdio.h>
main()
{
FILE *fp;
char ch;

fp=fopen("hello.txt","r");

printf("%d\n",ftell(fp));
ch=getc(fp);
printf("%c\n",ch);
printf("%d\n",ftell(fp));

fseek(fp,2,SEEK_CUR);
ch=getc(fp);

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

printf("%c\n",ch);
printf("%d\n",ftell(fp));

fseek(fp,-2,SEEK_CUR);
ch=getc(fp);
printf("%c\n",ch);

fseek(fp,-2,SEEK_END);
ch=getc(fp);
printf("%c\n",ch);

fseek(fp,3,SEEK_SET);
ch=getc(fp);
printf("%c\n",ch);
}

Output:
0
c
1
p
4
m
e
p

10. Program to rename a file:

#include<stdio.h>
main()
{
char x[10],y[10];

printf("Enter old name, new name:");


scanf("%s%s",x,y);

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

rename(x,y);

printf("File renamed");
}

Output:
Enter old name, new name:demo.txt
aaa.txt
File renamed

11. Program to remove a file:

#include<stdio.h>
main()
{
char x[20];

printf("Enter the file name to be deleted:");


scanf("%s",x);

remove(x);

printf("File removed");

Output:
Enter the file name to be deleted:aaa.txt
File removed

Files V.Shiwa Chaithanya


Classroom Programs Naresh IT 1

Chapter-10

Graphics

1. Program to print maximum value and minimum value in x and y


Axis:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

printf("x-axis max value=%d\n",getmaxx());


printf("y-axis max value=%d\n",getmaxy());

getch();
}

Output:
x-axis max value=639
y-axis max value=479

2. Program to draw a line:

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 2

gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(MAGENTA);
setcolor(RED);

line(100,120,220,200);

setcolor(BLUE);

line(300,320,420,400);

getch();
}

Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 3

3. Program to draw a circle with radius 50 at coordinate (100,100):

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;

gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(MAGENTA);
setcolor(WHITE);

circle(100,100,50);

getch();
}

Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 4

4. Program to fill color in circle:

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(MAGENTA);
setcolor(WHITE);

circle(100,100,50);

getch();
}

Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 5

5. Program to draw a rectangle:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(GREEN);
setcolor(RED);

rectangle(100,100,300,200);

getch();
}

Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 6

6. Program to fill color in rectangle:

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(LIGHTGREEN);
setcolor(RED);

rectangle(100,100,300,200);

setfillstyle(SOLID_FILLm,BLUE);
floodfill(120,120,RED);
getch();
}
Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 7

7. Program to draw an ellipse:

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(RED);
setcolor(WHITE);

ellipse(200,200,0,360,100,50);

setfillstyle(LINE_FILL,BLUE);
floodfill(200,200,WHITE);
getch();
}
Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 8

8. Program to draw a pie slice:

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(RED);
setcolor(WHITE);

pieslice(200,200,0,90,100);
pieslice(200,200,180,270,100);
getch();
}
Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 9

9. Program to draw an arc:

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(RED);

setcolor(WHITE);

arc(200,200,0,180,100);

getch();
}
Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 10

10. Program to a polygon with 6 coordinates:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
int x[] = {100,100,300,50,200,150,180,30,320,100,100,100};
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(RED);
setcolor(WHITE);

drawpoly(6,x);

getch();
}

Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 11

11. Program to print pixels at random coordinates with random colors:

#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd,gm
long int i;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(WHITE);

for(i=1;i<=50000;i++)
putpixel(random(getmaxx()),random(getmaxy()),random(16));

getch();
}

Output:

Graphics V.Shiwa Chaithanya


Classroom Programs Naresh IT 12

12. Program to draw text and apply font style:

#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setbkcolor(RED);

settextstyle(1,1,5);
outtextxy(30,30,"hello");

settextstyle(4,0,9);
outtextxy(100,100,"All the Best");
getch();
}
Output:

Graphics V.Shiwa Chaithanya

You might also like