C Lang All Programs
C Lang All Programs
Chapter-1
C-Language Basics
#include<stdio.h>
main()
{
printf("hello");
}
Output:
Hello
#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
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
a=pi*r*r;
p=2*pi*r;
printf(" area=%f",a);
printf("\n perimeter=%f",p);
}
Output:
area=195.967422
perimeter=49.612003
Output:
section:A
marks:786
average marks:78.599998
printf("Hi %s",x);
}
Output:
Hi Raju
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
printf("float value=%E",f);
}
Output:
float value=2.345678E+002
printf("Enter x,y:");
scanf("%d%d",&x,&y);
z=x+y;
printf("sum=%d",z);
}
Output:
Enter x,y:20
30
sum=50
printf("Enter radius:");
scanf("%f",&r);
Output:
Enter radius:6.4
area=128.614410
perimeter=40.192001
Output:
Enter section, marks and average marks:A
786
78.6
section:A
marks:786
average marks:78.599998
printf("Hi %s",x);
}
Output:
Enter your name:Ravi
Hi Ravi
printf("average=%f",avrg);
}
Output:
Enter three numbers:10
20
31
average=20.333334
printf("Before swapping:\n");
printf("x=%d \t y=%d",x,y);
temp=x;
x=y;
y=temp;
Output:
Enter two numbers:10 20
Before swapping:
x=10 y=20
After swapping:
x=20 y=10
printf("Before swapping:\n");
printf("x=%d \t y=%d",x,y);
x=x+y;
y=x-y;
x=x-y;
Output:
Enter two numbers:20 30
Before swapping:
x=20 y=30
After swapping:
x=30 y=20
Chapter-2
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
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
if(age>=18)
printf("Eligible to Vote");
else
printf("Not Eligible to Vote");
}
Output:
Enter your age:23
Eligible to Vote
if(m>=40)
printf("Pass");
else
printf("Fail");
}
Output:
Enter maths sub marks:52
Pass
else
printf("Fail");
}
Output:
Enter 3 subjects marks: 44 66 55
Pass
Output:
hi
printf("x=%d\n",x);
x*=3;
printf("x=%d\n",x);
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
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);
}
Output:
x=5
x=15
x=25
x=23
x=7
x=3
printf("x=%d\n",x); //x=8
x=-x;
printf("x=%d",x); //x=8
}
Output:
x=8
x=-8
x=8
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);
Output:
Enter n:7
Odd
{
int x,y,big;
big = x>y ? x : y;
printf("biggest number=%d",big);
}
Output:
Enter two different numbers:40 30
biggest number=40
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
#include<stdio.h> #include<stdio.h>
main() main()
{ {
int x=5; int x=5;
printf("%d",x); printf("%d",x);
} }
Output: Output:
6 6
#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.
#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.
Chapter-3
Control Structures
#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
#include<stdio.h>
main()
{
int x,y,z,big;
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
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
if(age>=18)
printf("Eligible to Vote");
else
printf("Not Eligible to Vote");
}
Output:
Enter your age:23
Eligible to Vote
if(a>b)
printf("%d is big",a);
else
printf("%d is big",b);
}
Output:
Enter two different numbers:20 10
20 is big
#include<stdio.h>
main()
{
int m,p,c,avrg;
avrg = (m+p+c)/3;
printf("average marks=%d\n",avrg);
else if(avrg>=50)
printf("Second Division");
else
printf("Third Division");
}
Output:
Enter 3 subjects marks:65 69 62
average marks=65
First Division
#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
Output:
Enter 3 different numbers:20 30 10
30 is big
k=sqrt(100);
printf("squareroot of 100=%f\n",k);
k=pow(2,3);
printf("2 power 3 = %f \n",k);
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
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)
{
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
#include<stdio.h>
main()
{
int x,y,z;
if(x>y)
{
if(x>z)
printf("%d is big",x);
}
if(y>x)
{
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;
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);
else
printf("%d is big",z);
}
}
Output:
Enter three different numbers:60 90 50
90 is big
if(avrg>=60)
printf("First Division");
else if(avrg>=50)
printf("Second Division");
else
printf("Third Division");
}
else
printf("Fail");
}
Output:
Enter 3 subjects marks:70 65 80
average marks=71
First Division
printf("biggest number=%d",big);
}
Output:
Enter three different numbers: 50 20 40
biggest number=50
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
switch(n)
{
case 1:
printf("January");
break;
case 2:
printf("February");
break;
case 12:
printf("December");
break;
default:
printf("Invalid Month Number");
}
}
Output:
Enter month number:12
December
switch(op)
{
case '+':
printf("sum=%d",x+y);
break;
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
printf("Enter an Alphabet:");
scanf("%c",&alpha);
alpha=tolower(alpha);
switch(alpha)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("vowel");
break;
default:
printf("Consonant");
}
}
Output:
Enter an Alphabet: i
Vowel
switch(choice)
{
case 1:
printf("ur favorite fruit is Apple");
break;
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
i=1; //Initialization
printf("Enter n:");
scanf("%d",&n);
while(i<=n) //Condition
{
printf("%d\n",i);
i++; // Step
}
}
Output:
Enter n:5
1
2
3
4
5
printf("Enter n:");
scanf("%d",&n);
i=n;
while(i>=1)
{
printf("%d\n",i);
i--;
}
}
Output:
Enter n:4
4
3
2
1
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
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
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
printf("Enter n:");
scanf("%d",&n);
sum=0;
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
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);
}
Output:
Enter n:123
reverse number=321
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");
}
Output:
Enter n:121
after loop:
n=0
temp=121
Palindrome
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;
}
if(temp==sum)
printf("%d is Armstrong",temp);
else
printf("%d is Not An Armstrong",temp);
}
Output:
Enter n:153
153 is Armstrong
i=1;
do
{
printf("%d\n",i);
i++;
}while(i<=n);
Output:
Enter n:4
1
2
3
4
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
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d\n",i);
}
Output:
Enter n:4
1
2
3
4
printf("Enter n:");
scanf("%d",&n);
for(i=n;i>=1;i--)
printf("%d\n",i);
}
Output:
Enter n:4
4
3
2
1
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
#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);
}
Output:
Enter n:5
1
3
5
7
9
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
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
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==1)
{
printf("%d is odd\n",i);
co++;
}
else
{
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
for(i=10;i<=100;i=i+10)
printf("%d\n",i);
}
Output:
10
20
30
40
50
60
70
80
90
100
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
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
printf("Enter n:");
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{
sum=sum+i*i*x;
x=-x;
}
printf("sum=%d",sum);
}
Output:
Enter n:4
sum=-10
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
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
for(i=1;i<=p;i++)
r=r*n;
Output:
Enter a number AND power:2 3
2 power 3=8
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
1
1
2
3
5
8
13
21
34
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
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
printf("Enter n:");
scanf("%d",&n);
i=1;
nareshit:
printf("%d\n",i);
i++;
if(i<=n)
goto nareshit;
}
Output:
Enter n:5
1
2
3
4
5
Output:
Enter n:5
5
4
3
2
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
int n;
xyz:
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
if(n>0)
goto abcd;
n=-n;
abcd:
printf("%d",n);
}
Output:
enter a +ve value:5
5
Output:
1
2
3
4
5
{
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
for(i=1;i<=10;i++)
{
if(i==5 || i==7)
continue;
printf("%d\n",i);
}
}
Output:
1
2
3
4
6
8
9
10
while(1) //true
{
printf("%d\n",i);
i++;
if(i<=5)
continue;
break;
}
}
Output:
1
2
3
4
5
for(i=1;i<=10;i++)
{
for(j=1;j<=5;j++)
printf(" * ");
printf("\n");
}
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
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
main()
{
int n, sum, i,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
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
#include<stdio.h>
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
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
#include<stdio.h>
main()
{
int n,i,j;
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
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
main()
{
int i,j,n;
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
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
main()
{
int n,i,j;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1 || i==n || j==1 || j==n)
printf(" * ");
else
printf(" ");
}
printf("\n");
}
}
Output:
Enter n:5
* * * * *
* *
* *
* *
* * * * *
#include<stdio.h>
main()
{
int n,i,j;
printf("Enter n:");
scanf("%d",&n);
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
*
* *
* *
* *
* * * * *
#include<stdio.h>
main()
{
int n,i,j,p;
printf("Enter n:");
scanf("%d",&n);
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
*
*
* * * * *
*
*
#include<stdio.h>
main()
{
int n,i,j,p;
printf("Enter n:");
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
* * * *
* *
* * * * *
* *
* * * *
#include<stdio.h>
main()
{
int n,i,j,p;
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
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
#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
#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 ",i);
}
printf("\n");
}
}
Output:
Enter n:5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
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
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;
}
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>
main()
{
int k;
k=add(1,2);
printf("sum=%d\n",k);
printf("sum=%d",add(4,3));
}
Output:
sum=3
sum=7
#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
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
#include<stdio.h>
int power(int,int);
main()
{
int k;
k=power(2,3);
printf("2 power 3=%d\n",k);
Output:
2 power 3=8
5 power 2 =25
#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
#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
#include<stdbool.h>
#include<stdio.h>
bool isprime(int);
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
#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);
}
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));
}
Output:
max value=20
min value=10
#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:
#include<stdio.h>
#include<stdio.h>
int fact(int);
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
#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
#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));
}
Output:
0
1
1
2
3
5
8
13
21
34
#include<stdio.h>
void f1();
main()
{
auto int a=100; //auto variable
f1();
printf("%d\n",a); //100
// printf("%d\n",x);
// printf("%d\n",a);
Output:
from main:50
from f1: 100
from f2: 200
Output:
60
50
200
void f1();
static int x=0;
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
#include<stdio.h>
void f1();
static int x=0; //static global variable
void f1()
{
x++;
printf("function call number is %d\n",x);
}
main()
{
int i;
for(i=1;i<=5;i++)
f1();
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
#include<stdio.h>
main()
{
register int i;
for(i=1;i<=5;i++)
printf("%d\n",i);
}
Output:
1
2
3
4
5
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
Chapter-5
Arrays
#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
#include<stdio.h>
main()
{
int x[10],n,i;
scanf("%d",&n);
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
#include<stdio.h>
main()
{
int x[10],y[10],n,i;
for(i=0;i<n;i++)
y[i]=x[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
#include<stdio.h>
main()
{
int x[10],n,i;
for(i=n-1;i>=0;i--)
printf("%d\n",x[i]);
}
Output:
Enter number of elements:4
Enter x-array elements:20 40 30 80
80
30
40
20
#include<stdio.h>
main()
{
int x[10],y[10],n,i;
for(i=0;i<n;i++)
y[n-i-1]=x[i];
Output:
Enter number of elements:4
Enter x-array elements:10 20 30 40
x array y-array
------------------------------------------------
10 40
20 30
30 20
40 10
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]);
}
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
#include<stdio.h>
main()
{
int x[10],y[10],z[20],n,i;
for(i=0;i<n;i++)
{
z[2*i]=x[i];
z[2*i+1]=y[i];
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
#include<stdio.h>
main()
{
int x[10],n,i,sum=0;
for(i=0;i<n;i++)
sum=sum+x[i];
printf("sum of elements=%d",sum);
}
Output:
Enter number of elements:4
Enter x-array elements:10 20 30 40
sum of elements=100
#include<stdio.h>
main()
{
int x[10],n,i,max,min;
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);
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
#include<stdio.h>
main()
{
int x[10],n,i,j,temp;
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;
}
}
}
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;
found=0;
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;
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
#include<stdio.h>
main()
{
int x[10],n,i,p,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
#include<stdio.h>
main()
{
int x[10],n,i,p,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
20
50
40
30
#include<stdio.h>
main()
{
int x[10],n,p,i;
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
40
30
#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
#include<stdio.h>
main()
{
int x[5][5],n,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
#include<stdio.h>
main()
{
int x[5][5],y[5][5],m,n,i,j;
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
Output:
Enter number of rows and columns:2 3
Enter x-matrix elements:10 20 30 40 50 60
#include<stdio.h>
main()
{
int x[5][5],y[5][5],m,n,i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
y[i][j]=x[i][j];
Output:
Enter number of rows and columns:2 3
Enter x-matrix elements:10 20 30 40 50 60
#include<stdio.h>
main()
{
int x[5][5],y[5][5],m,n,i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
y[j][i]=x[i][j];
Output:
Enter number of rows and columns:2 3
Enter x-matrix elements:10 20 30 40 50 60
#include<stdio.h>
main()
{
int x[5][5],n,i,j,sum=0;
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
#include<stdio.h>
main()
{
int x[5][5],n,i,j,sum=0;
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
#include<stdio.h>
main()
{
int x[5][5],n,i,j,sum=0;
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
#include<stdio.h>
main()
{
int x[5][5],y[5][5],z[5][5],m,n,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
#include<stdio.h>
main()
{
int x[5][5],y[5][5],z[5][5],m,n,p,q,i,j,k;
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]);
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");
}
}
}
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
#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
#include<stdio.h>
main()
{
int x[2][2][3];
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:
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
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
..............................
Chapter-6
Strings
#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
raju
raju
raju
raju
#include<stdio.h>
main()
{
char x[10]="Raju";
printf("Hi %s",x);
}
Output:
Hi Raju
#include<stdio.h>
main()
{
char x[20];
printf("Hi %s",x);
}
Output:
Enter your name:Ramu
Hi Ramu
#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
#include<stdio.h>
#include<string.h>
main()
{
char x[20];
int n;
printf("Enter a string:");
scanf("%s",x);
n=strlen(x);
printf("number of char=%d",n);
}
Output:
Enter a string:raju
number of char=4
#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);
}
Output:
Enter a string:raju
number of char=4
printf("Enter a string:");
scanf("%s",x);
strcpy(y,x);
Output:
Enter a string:raju
x string=raju
y string=raju
#include<stdio.h>
main()
{
char x[20],y[20];
int i;
printf("Enter a string:");
scanf("%s",x);
for(i=0;x[i]!='\0';i++)
y[i]=x[i];
y[i]='\0';
Output:
Enter a string:ravi
x string=ravi
y string=ravi
#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
#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
#include<string.h>
#include<stdio.h>
main()
{
char x[10],y[10];
strcat(x,y);
printf("concatenated string=%s",x);
}
Output:
Enter two strings:raj
kumar
concatenated string=rajkumar
#include<string.h>
#include<stdio.h>
main()
{
char x[10],y[10],z[20];
int i,n;
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;
printf("concatenated string=%s",z);
}
Output:
Enter two strings:sai
kumar
concatenated string=saikumar
#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
#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);
strlwr(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);
strupr(x);
Output:
Enter a string:ramu
Before calling strupr=ramu
After calling strupr=RAMU
#include<stdio.h>
main()
{
char ch;
int nw=0,nl=0,nc=0,nupr=0,nlwr=0,nd=0,ns=0;
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);
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
#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
#include<stdio.h>
main()
{
char x[10][10];
int i,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
Chapter-7
Pointers
#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
address=6487564
value=30
address=6487564
value=30
50
#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
#include<stdio.h>
main()
{
int x=30,*p1,*p2;
p1=&x;
p2=&x;
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);
printf("address=%d\n",p);
}
Output:
address=6487574
address=6487576
5. Pointer Arithmetic:
#include<stdio.h>
main()
{
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;
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);
}
#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++);
}
Output:
20
25
30
35
40
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
#include<stdio.h>
main()
{
int x[2][3] = {{10,20,30},{40,50,60}},*p,i,j;
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
#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");
}
printf("\n");
}
}
Output:
10 20 30
40 50 60
10 20 30
40 50 60
#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
#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
#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 value
printf("a=%d\nb=%d\n",a,b);
}
Output:
a=20
b=30
#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
#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
#include<stdio.h>
#include<stdlib.h>
main()
{
int *p;
p=(int*)calloc(10,sizeof(int));
*(p+0) = 40;
*(p+1) = 50;
*(p+2) = 30;
printf("%d\n",*(p+0));
printf("%d\n",*(p+1));
printf("%d\n",*(p+2));
}
Output:
40
50
30
Chapter-8
#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
#include<stdio.h>
struct student
{
int rno;
char name[20];
};
main()
{
struct student s1;
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
#include<stdio.h>
main()
{
typedef int rama;
typedef rama krishna;
rama x=20;
krishna y=30;
printf("%d",x+y);
}
Output:
50
#include<stdio.h>
typedef struct employee
{
int empid;
char ename[20];
double sal;
}emp;
main()
{
emp e1,e2;
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);
}
main()
{
std s1;
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;
s1 = f1();
printf("%d\t%s\t%d",s1.rno,s1.name,s1.marks);
}
Output:
12 Vijay 456
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;
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;
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;
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
#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
#include<stdio.h>
typedef struct person
{
int pid;
char pname[20];
}person;
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
#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
union aaa
{
int x;
int y;
};
main()
{
union aaa a1;
a1.x = 20;
printf("%d\n",a1.y); //20
a1.y = 30;
printf("%d",a1.x); //30
}
Output:
20
30
#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
Chapter-9
Files
#include<stdio.h>
main()
{
FILE *fp;
char ch;
fp=fopen("demo.txt","w");
fclose(fp);
Output:
Enter text [press f6 or ctrl+z]:
Hello Everyone,
Have a nice day
^Z
Text saved in file
#include<stdio.h>
main()
{
FILE *fp;
char ch;
fp=fopen("demo.txt","a");
while((ch=getchar())!=EOF)
{
putc(ch,fp);
}
fclose(fp);
Output:
Enter text [press f6 or ctrl+z]:
Hello..
This text will be appended
to existed data
^Z
Text saved in 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
#include<stdio.h>
main()
{
FILE *fp1,*fp2;
char ch;
fp1=fopen("demo.txt","r");
fp2=fopen("aaa.txt","w");
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
}
Output:
Contents copied into file
#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);
Output:
Enter rno,name:15 Ravi
Record saved in file
#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
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
#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);
}
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
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);
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
#include<stdio.h>
main()
{
char x[10],y[10];
rename(x,y);
printf("File renamed");
}
Output:
Enter old name, new name:demo.txt
aaa.txt
File renamed
#include<stdio.h>
main()
{
char x[20];
remove(x);
printf("File removed");
Output:
Enter the file name to be deleted:aaa.txt
File removed
Chapter-10
Graphics
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
getch();
}
Output:
x-axis max value=639
y-axis max value=479
#include<graphics.h>
#include<conio.h>
void main()
{
int gd,gm;
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:
#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:
#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:
#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:
#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:
#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:
#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:
#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:
#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:
#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:
#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: