Sunteți pe pagina 1din 120

1) Write a c program to find the sum of n natural numbers.

#include<stdio.h>
int main()
{
int n,i,sum=0;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
sum=sum+i;
}
printf("sum=%d",sum);
return 0;
}
Output:

Page 1 of 120
2) Write a c program to find the sum of square of n natural
numbers.
#include<stdio.h>
int main()
{
int n,i,sum=0;
scanf("%d",&n);
for (i=1;i<=n;i++) sum=sum+(i*i);
printf("sum=%d",sum);
return 0;
}
Output:

Page 2 of 120
3) Write a c program to find the sum of cube of n natural
numbers.
#include<stdio.h>
int main()
{
int n,i,sum=0;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
sum=sum+(i*i*i);
}
printf("sum=%d",sum);
return 0;
}

Output:

Page 3 of 120
4) Write an program to solve ax + b =0.

#include<stdio.h>
int main()
{
int a,b,i;
scanf("%d %d",&a,&b);
if (a!=0)
{
if (b==0)
printf("\nzero");
else if (b!=0)
{
int x=-b/a;
printf("%d",x);
}
}
return 0;
}

Output:

Page 4 of 120
5) Write a c program to find the distance travelled by a vehicle.

#include<stdio.h>
int main()
{
int u,a,t,d;
scanf("%d %d %d",&u,&a,&t);
d=u*t+0.5*(a*t*t);
printf("%d",d);
return 0;
}

Output:

Page 5 of 120
6) Write a c program to count the number of Circles (Ex. 6  1, 8
 2,...)
Inpu Output
t
6 1
3 0
60 2

#include<stdio.h>
int main()
{
int r,n,i,count=0;
scanf("%d",&n);
for (i=n;i>0;i=i/10)
{
r=i%10;
if (r==0||r==6||r==9)
count++;
if (r==8)
count=count+2;
printf("no.of zeroes=%d",count);
return 0;
}
Output:

Page 6 of 120
7) Write a c program to find L.C.M of two numbers.

#include<stdio.h>
int main()
{
int n1,n2,max;
scanf("%d %d",&n1,&n2);
if (n1>n2)
max=n1;
else
max=n2;
while (1)
{
if ((max%n1==0)&&(max%n2==0))
{
printf("%d",max);
break;
}
++max;
}
return 0;
}
Output:

Page 7 of 120
8) Write a c program to find G.C.D of two numbers.
#include<stdio.h>
int main()
{
int n1,n2,i,gcd;
scanf("%d %d",&n1,&n2);
for (i=1;i<=n1&&i<=n2;++i)
{
if (n1%i==0 && n2%i==0)
gcd=i;
}
printf("%d",gcd);
return 0;
}

Output:

Page 8 of 120
9) Write a c program to find LCM, GCD of two numbers by Using
LCM X GCD=a x b.
#include<stdio.h>
int main()
{
int a,b,gcd,n1,n2,p,lcm;
scanf("%d %d",&n1,&n2);
p=n1*n2;
while (n1!=n2)
{
if (n1>n2)
{
n1=n1-n2;
}
else
{
n2=n2-n1;
}
}
printf("%d",n1);
lcm=p/n1;;
printf(" %d",lcm);
return 0;
}
Output:

Page 9 of 120
10) Write a c program to print prime factors of a given
number.
#include<stdio.h>
int main()
{
int i,n;
scanf("%d",&n);
printf("1 ");
while (n%2==0)
{
printf("2 ");
n=n/2;
}
for (i=3;i<sqrt(n);i=i+2)
{
while (n%i==0)
{
printf("%d ",i);
n=n/i;
}
}
if (n>2)
printf("%d",n);
return 0;
}
Output:

Page 10 of 120
11) Write a c program to print the kth factor from all the
prime factors for a given numbers.
Inpu Output
t
20 3rd Prime number
21 2nd Prime Number

#include<stdio.h>
#include<math.h>
int main()
{
int n,k,i;
scanf("%d %d",&n,&k);
while (n%2==0)
{
k--;
n=n/2;
if (k==0)
printf("2");
}
for (i=3;i<=sqrt(n);i=i+2)
{
while (n%i==0)
{
if (k==1)
printf("%d",i);
k--;
n=n/i;
}
}
if (n>2&&k==1) printf("%d",n);
return 0;
}
Output:

Page 11 of 120
12) Write a c program to find all prime numbers from 1 to n
(sieve of Eratostenes).
Inpu Output
t
10 2357
6 235
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
int a[n];
for (i=0;i<n;i++)
{
a[i]=1;
}
for (i=2;i*i<=n;i++)
{ if (a[i]==1)
{ for (j=i*i;j<=n;j=j+i)
{
a[j]=0;
} } }
for (i=2;i<=n;i++)
{
if (a[i]==1) printf("\t %d",i);
}
return 0;
}
Output:

Page 12 of 120
13) Write a c program to find two distinct prime numbers (n
> 2) whose product will be equal to the given number. There
may be several combinations possible, print only 1 st pair. If it
is not possible in the product of two numbers then output
should be possible.
#include<stdio.h> Inpu Output
int main() t
{ int b,x,y,i,m,n,o=1,r=0; 15 3, 5
scanf("%d",&b); 39 3, 13
int p[100],q[100];
for (i=2;i<=b;i++)
{ y=1;
for (x=2;x<=i/2;x++)
{ if (i%x==0)
{ y=0;
break;
} }
if (y==1)
{ p[0]=2;
q[0]=2;
p[o]=i;
q[o]=i;
o++;
} }
for (m=0;m<=o;m++)
{
for (n=0;n<=o;n++)
{
if (p[m]*q[n]==b)
{
if (r==1) break;
printf("%d %d",p[m],p[n]);
r++;
break;
} } }
if (r==0)
printf("not possible");
return 0;
}
Output:

Page 13 of 120
14) Writea c program to find the nth term of series 3, 5, 33,
35, 53, 55, .... Given number n in the task to find the nth
number in given series.
Inpu Output
t
5 53
3 33

#include<stdio.h>
int main()
{
int arr[100];
int i,n=20,x;
arr[0]=3;
arr[1]=5;
for (i=2;i<n;i++)
{
if (i%2!=0)
arr[i]=arr[(i/2)-1]*10+5;
else
arr[i]=arr[(i/2)-1]*10+3;
}
scanf("%d",&x);
printf("\n %d",arr[x-1]);
return 0;
}
Output:

Page 14 of 120
15) Writea c program to find the nth term in series 1, 2, 11,
12, 21,...
Inpu Output
t
4 12
2 2
#include<stdio.h>
int main()
{
int arr[100];
int x,i,n=20;
arr[0]=1;
arr[1]=2;
for (i=2;i<n;i++)
{
if (i%2==0)
arr[i]=arr[(i/2)-1]*10+1;
else
arr[i]=arr[(i/2)-1]*10+2;
}
scanf("\n %d",&x);
printf("\n %d",arr[x-1]);
return 0;
}
Output:

Page 15 of 120
16) Write a c program to find the sum of series: 1*3 + 3*5 +
5*7+.....
Inpu Output
t
2 18
1 3
#include<stdio.h>
main()
{
int n,i,sum=0,j=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(j*(j+2));
j=j+2;
}
printf("%d",sum);
}

Output:

Page 16 of 120
17) Write a c program to find the leader element in an array
Input Output
15 16 3 2 6 1 4 6 16
4
3 9 8 17 6 10 10 17
#include<stdio.h>
int main()
{
int i,j,n,arr[100],leader;
scanf("%d",&n);
for (i=0;i<n;i++) scanf("%d",&arr[i]);
leader=arr[n-1];
printf(" %d",leader);
for (i=n-2;i>=0;i--)
{ if (arr[i]>leader)
{ leader=arr[i];
printf("\n %d",leader);
} }
return 0;
}
Output:

Page 17 of 120
Page 18 of 120
18) Write a c program to print the only odd occurring
element.
Input Output
12, 12, 14, 90, 14, 14, 90
14
1, 2, 3, 2, 3, 1, 3 3
#include<stdio.h>
int main()
{
int i,j,res=0,n,arr[100];
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for (i=0;i<n;i++)
{
res=res^arr[i];
}
printf("%d",res);
return 0;
}
Output:

Page 19 of 120
19) Write a c program to find whether the given number is
True or False.
Input Output
X=2, False
y=5 True
X=5, False
y=2 True
#include<stdio.h>
int main()
{
int x,y;
scanf("%d %d",&x,&y);
(x&y)?printf("true"):printf("false");
(x&&y)?printf("\ntrue"):printf("\nfalse");
return 0;
}
Output:

Page 20 of 120
20) Write a c program to find whether the given number is
odd or even.
Inpu Output
t
X=7 Odd
X=10 Even
#include<stdio.h>
int main()
{
int x,y;
scanf("%d %d",&x,&y);
(x&1)?printf("odd"):printf("even");
(y&1)?printf("\nodd"):printf("\neven");
return 0;
}
Output:

Page 21 of 120
21) Write a c program to find X-OR for 1 to n.
Inpu Output
t
5 1
3 0
100 100
#include<stdio.h>
int main()
{
int i,n,res=0,ar[100];
scanf(“%d”,&n);
for (i=1;i<=n;i++)
{
res=res^i;
}
printf("%d",res);
return 0;
}
Output:

Page 22 of 120
22) Write a c program to count the trailing 0’s in factorial of
a number
Inpu Output
t
5 1
20 4
#include<stdio.h>
int main()
{
int i,n,count;
scanf("%d",&n);
count=0;
for (i=5;n/i>=1;i=i*5)
{
count=count+n/i;
}
printf("no.. of trailing zeroes=%d",count);
return 0;

}
Output:

Page 23 of 120
23) Write an efficient program to count the number of 1’s
in binary representation of an integer
Inpu Output
t
1 1
2 1
3 2
#include<stdio.h>
int main()
{
int n,count;
count=0;
scanf("%d",&n);
while (n)
{
count=count+(n&1);
n=n>>1;

}
printf("%d",count);
return 0;
}
Output:

Page 24 of 120
24) Write a c program to print “yes” if that number can be
expressed in power of 2.
Inpu Output
t
8 Yes
6 No
#include<stdio.h>
#include<math.h>
int main()
{
int b,n,m;
scanf("%d",&n);
m=(n&(n-1));
if (m==0)
printf("yes");
else
printf("no");
return 0;
}
Output:

Page 25 of 120
25) Write a c program to print the number which doesn’t
appears thrice. Given an array, which contains every number
appears thrice except for one which occurs once.
Input Output
7,12 12 14 14 12 90 14 90
10,11 11 11 14 15 14 15 14 15 12
12
#include<stdio.h>
int main()
{
int x,sum,i,j,res=0,arr[100],n;
scanf("%d",&n);
for (i=0;i<n;i++) scanf("%d",&arr[i]);
for (i=0;i<8;i++)
{ sum=0;
x=1<<i;
for (j=0;j<n;j++)
{ if (arr[j]&x) sum++; }
if (sum%3) res|=x;
}
printf("%d",res);
return 0;
}
Output:

Page 26 of 120
26) Write an C program to find the number of leading 0’s in
binary representation of a given number.
Inpu Output
t
5 29
100 25
#include<stdio.h>
int main()
{
int n=32,x,y;
scanf("%d",&x);
y=x>>16;
if (y!=0) x=y;
y=x>>8;
if (y!=0)
{ n=n-8;
x=y;
}
y=x>>4;
if (y!=0)
{ n=n-4;
x=y;
}
y=x>>2;
if (y!=0)
{ n=n-2;
x=y;
}
y=x>>1;
if (y!=0 && y!=1) printf("%d",n-2);
else printf("%d",n-x);
return 0;
}
Output:

Page 27 of 120
27) Write a C program to check whether the given two
numbers are equal or not without using arithmetic operator.
Input Output
X=2
2 no’s are not equal
Y=6
X=10
2 no’s are equal
Y=10

#include<stdio.h>
int main()
{
int x,y,b;
scanf("%d %d",&x,&y);
b=x^y;
if (b==0)
printf("2 no’s are equal");
else
printf("2 no’s are not equal");
return 0;
}

Output:

Page 28 of 120
Page 29 of 120
28) Write a C program to check whether the binary
representation of given number is Palindrome or not.
Inpu Output
t
343 Not Palindrome
456 Not palindrome
#include<stdio.h>
int main()
{
int n,rev=0,temp;
scanf("%d",&n);
temp=n;
while (n>0)
{
rev<<=1;
if (n&1==1)
rev^=1;
n>>=1;

}
if (temp==rev)
printf("palindrome");
else
printf("not palindrome");
return 0;
}
Output:

Page 30 of 120
29) Write a C Program to arrange all 0’s,1’s and 2’s in the
increasing order (Dutch National Flag).
Input Output

8
00011222
01220120

6
000112
021001
#include<stdio.h>
int main()
{ int i,n,temp;
scanf("%d",&n);
int arr[n];
for (i=0;i<n;i++) scanf("%d",&arr[i]);
int low=0,high=n-1,mid=0;
while (mid<=high)
{ switch (arr[mid])
{
case 0: temp=arr[low];
arr[low]=arr[mid];
arr[mid]=temp;
mid++;
low++;
break;
case 1: mid++;
break;
case 2: temp=arr[mid];
arr[mid]=arr[high];
arr[high]=temp;
high--;
break;
} }
for (i=0;i<n;i++) printf("%d",arr[i]);
return 0;
}
Output:

Page 31 of 120
30) Write a C Program to find the maximum sum in a sub
array (KADANE’s algorithm)
Input Output
-4 1 3 -2 6 -1 5 12
3 6 -3 8 -2 14
#include<stdio.h>
int main()
{
int n,i;
scanf("%d",&n);
int arr[n];
for (i=0;i<n;i++) scanf("%d",&arr[i]);
int sum1=0,sum2=0;
for (i=0;i<n;i++)
{
sum1=sum1+arr[i];
if (sum1<0)
sum1=0;
if (sum1>sum2)
sum2=sum1;
}
printf("%d",sum2);
return 0;
}
Output:

Page 32 of 120
31) Write a C Program to find the reverse of an array.
Input Output
10 20 30
40 30 20 10
40
15 18 21 21 18 15
#include<stdio.h>
int main()
{
int temp,i,j,n,arr[100],brr[100];
scanf("%d",&n);
for (i=0;i<n;i++) scanf("%d",&arr[i]);
i=0;
j=n-1;
while (i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
for (i=0;i<n;i++) printf("%d",arr[i]);
return 0;
}
Output:

Page 33 of 120
32) Write a c program to take 2 inputs from users and print
addition of those numbers 2 times. Then take another 2 input
from user and print subtraction of those numbers
Input Output
3, 4 7 -1 7
6, 5 11 1 11
17,
27 7 27
10
#include<stdio.h>
void add(int,int);
int main()
{
int a,b;
scanf("%d %d",&a,&b);
add(a,b);
printf("\n %d",a-b);
add(a,b);
return 0;
}
void add(int x,int y)
{
printf("\n %d",x+y);
}
Output:

Page 34 of 120
33) Write a c program to find maximum of 2 numbers using
functions.
Input Output
a=2, b=6 6
a=23, b=19 23
#include<stdio.h>
int max(int,int);
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=max(a,b);
printf("%d",c);
return 0;
}

Page 35 of 120
int max(int x,int y)
{
if(x<y)
return y;
else
return x;
return 0;
}
Output:

Page 36 of 120
34) Write a C program to print Prime numbers between an
interval using functions.
Input Output
a=4,
5, 7
b=10
a=6,
7, 11, 13, 17, 19
b=20
#include<stdio.h>
void prime(int);
int main()
{ int a,b,i,res;
scanf("%d %d",&a,&b);
for (i=a+1;i<b;i++) prime(i);
return 0;
}
void prime(int n)
{
int flag=1,i=1;
int j;
for (j=2;j<=n/2;j++)
{
if (n%j==0)
flag=0;
}
if (flag==1)
{
printf("%d \n",n);
}
}
Output:

Page 37 of 120
35) Write a C program to determine whether givennumberis
Armstrong number or not using functions.
Inpu Output
t
153 Armstrong
40 Not an Armstrong
#include<stdio.h>
#include<math.h>
int arm(int,int);
int main()
{
int n,temp,count=0;
scanf("%d",&n);
temp=n;
while (n!=0)
{ n=n/10;
count++;
}
n=temp;
arm(n,count);
}
arm(int x,int y)
{ int t,c,r,sum=0;
t=x;
while (x!=0)
{
r=x%10;
x=x/10;
sum=sum+pow(r,y);
}
if (t==sum)
printf("armstrong");
else
printf("not an armstring");
return 0;
}
Output:

Page 38 of 120
36) Write a C program to print an element in given array
using functions.
Input Output
5, 1 2 3 4 5, 3 4
10, 0 1 2 3 8 5 4 8 11 100,
100
9
8, 5 10 15 20 25 30 35 40, 5 25
#include<stdio.h>
void dis(int);
int main()
{
int b,i,n;
scanf("%d",&b);
int arr[b];
for (i=0;i<b;i++) scanf("%d",&arr[i]);
scanf("%d",&n);
dis(arr[n]);
return 0;
}
void dis (int x)
{ printf("%d",x);
}
Output:

Page 39 of 120
37) Write a C program to print average of elements in an
given array using functions.
Input Output
5 12345 3.00
6 23.5 14.1 3.0 12.9 14.4
13.28
11.8
#include<stdio.h>
float average(float nat[],int );
int main()
{ float avg;
int n,i;
scanf("%d",&n);
float natu[n];
for (i=0;i<n;i++) scanf("%f",&natu[i]);
avg=average(natu,n);
printf("%.2f",avg);
return 0;
}
float average(float nat[],int x)
{ int i;
float avg,sum=0.0;
for (i=0;i<x;i++) sum=sum+nat[i];
avg=sum/x;
return avg;
}
Output:

Page 40 of 120
38) Write a C program to swap two numbers using functions
Input Output
A=10, B=20 A=20, B=10
A=30, B=20 A=20, B=30
#include<stdio.h>
int swap(int,int);
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
swap(a,b);
return 0;
}
int swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("\n %d %d",x,y);
}
Output:

Page 41 of 120
39) Write a C program to pass multi dimension array into
function x print the element in array.
Input Output
1 2
1234
3 4
11 22 33 11 22
44 33 44
#include<stdio.h>
void displayNumbers(int num[2][2]);
int main()
{
int num[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&num[i][j]);
displayNumbers(num);
}
void displayNumbers(int num[2][2])
{
int i,j;
for(i=0;i<2;i++)
{ printf("\n");
for(j=0;j<2;j++) printf("\t%d",num[i][j]);
} }
Output:

Page 42 of 120
40) Write a C program to find average of elements in an
multi dimension array using functions.
Input Output
1234 2.50
10 25 40
32.50
55
#include<stdio.h>
void displaynum(int num[2][2]);
int main()
{
int num[2][2],i,j;
for (i=0;i<2;i++)
{
for (j=0;j<2;j++) scanf("%d",&num[i][j]);
}
displaynum(num);
return 0;
}
void displaynum(int num[2][2])
{
int i,j;
float avg,sum=0;
for (i=0;i<2;i++)
{
for (j=0;j<2;j++) sum=sum+num[i][j];
}
avg=sum/4;
printf("%.2f",avg);
}
Output:

Page 43 of 120
41) Write a C program to print addition, Multiplication,
Transpose of given matrix using the function.
#include<stdio.h>
void tran(int a[10][10],int d[10][10],int r,int c)
{
int i,j;
for (i=0;i<r;i++)
{ for (j=0;j<c;j++)
{ d[i][j]=a[j][i];
} } }
void add(int a[10][10],int b[10][10],int d[10][10],int r,int c)
{
int i,j;
for (i=0;i<r;i++)
{ for (j=0;j<c;j++)
{ d[i][j]=a[i][j]+b[i][j];
} } }
void mul(int x[10][10],int y[10][10],int z[10][10],int m,int n,int p,int q)
{ int i,j,k;
for (i=0;i<m;i++)
for(j=0;j<q;j++)
{ z[i][j]=0;
for(k=0;k<n;k++)
z[i][j]+= x[i][k]*y[k][j];
} }
void read(int x[10][10], int m,int n)
{

Page 44 of 120
int i,j;
printf("Enter Matrix Value Row by Row\n");
for (i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
}

void print(int x[10][10], int m,int n)


{
int i,j;
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",x[i][j]);
printf("\n");
}
printf("\n");
}
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q;
printf("Enter the size of A Mtrix (Row and Col): \n");
scanf("%d%d",&m,&n);
printf("Enter the size of B Mtrix (Row and Col): \n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Multiplication Not Possible\n Please re-enter\n");
printf("correct size and try again .....\n");
}
else
{

Page 45 of 120
read(a,m,n);
read(b,m,n);
mul(a,b,c,m,n,p,q);
printf("A Matrix is :\n");
print(a,m,n);
printf("B Matrix is :\n");
print(b,m,n);
printf("C Matrix is :\n");
print(c,m,n);
add(a,b,c,p,q);
printf("addition");
print(c,p,q);
printf("transpose of a");
tran(a,c,m,n);
print(c,m,n);
printf("transpose of b");
tran(b,c,p,q);
print(c,p,q);
}
}
Output:

Page 46 of 120
Page 47 of 120
Page 48 of 120
42) Write a C program to convert a number from decimal to
binary.

#include<stdio.h>
int main()
{
int n,i,arr[500];
scanf("%d",&n);
for (i=0;n!=0;i++,n=n/2)
{
arr[i]=n%2;
}
for (i--;i>=0;i--)
{
printf("%d",arr[i]);
}
return 0;
}

Output:

Page 49 of 120
43) Write a C program to convert a number from decimal to
octal.
#include<stdio.h>
int main()
{
int n,i,arr[500];
scanf("%d",&n);
for (i=0;n!=0;i++,n=n/8)
{
arr[i]=n%8;
}
for (i--;i>=0;i--)
{
printf("%d",arr[i]);
}
return 0;
}

Output:

Page 50 of 120
44) Write a C program to convert a number from decimal to
hexa decimal.
#include<stdio.h>
int main()
{
int n,i,arr[500];
scanf("%d",&n);
for (i=0;n!=0;i++,n=n/16)
{
arr[i]=n%16;
}
for (i--;i>=0;i--)
{ switch (arr[i])
{ case 10:printf("A");break;
case 11:printf("B");break;
case 12:printf("C");break;
case 13:printf("D");break;
case 14:printf("E");break;
case 15:printf("F");break;
default:printf("%d",arr[i]);
} }
return 0;
}
Output:

Page 51 of 120
45) Write a C program to convert a number from hexa
decimal to decimal.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
char hex[17];
long long decimal, place;
int i = 0, val, len;
decimal = 0;
place = 1;
gets(hex);
len = strlen(hex);
len--;
for(i=0; hex[i]!='\0'; i++)
{
if(hex[i]>='0' && hex[i]<='9') val = hex[i] - 48;
else if(hex[i]>='a' && hex[i]<='f') val = hex[i] - 97 + 10;
else if(hex[i]>='A' && hex[i]<='F') val = hex[i] - 65 + 10;
decimal += val * pow(16, len);
len--;
}
printf("%ld", decimal);
return 0;
}
Output:

Page 52 of 120
46) Write a C program to convert a number from binary to
decimal.
#include<stdio.h>
int main()
{
int n,temp,d=0,base=1,r;
scanf("%d",&n);
temp=n;
while (n>0)
{
r=n%10;
d=d+r*base;
n=n/10;
base=base*2;
}
printf("%d",d);
return 0;
}
Output:

Page 53 of 120
47) Write a C program to convert a number from binary to
octal.
#include <stdio.h>
#include <math.h>
void main()
{ int n1, n,p=1, dec=0,i=1,j,d;
int oc=0,dn;
scanf("%d",&n);
n1=n;
for (j=n;j>0;j=j/10)
{ d = j % 10;
if(i==1) p=p*1;
else p=p*2;
dec=dec+(d*p);
i++;
}
dn=dec;
i=1;
for(j=dec;j>0;j=j/8)
{ oc=oc+(j % 8)*i;
i=i*10;
n=n/8;
}
printf(" %d ",oc);
}
Output:

Page 54 of 120
48) Write a C program to convert a number from binary to
hexa decimal.
#include<stdio.h>
int main()
{
long int binaryval, hexadecimalval = 0, i = 1, remainder;
printf("Enter the binary number: ");
scanf("%ld", &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("Equivalent hexadecimal value: %lX", hexadecimalval);
return 0;
}
Output:

Page 55 of 120
49) Write a C program to convert a number from hexa
decimal to binary.
#include <stdio.h>
#include <string.h>
int main()
{ char hex[17], bin[65] = "";
int i = 0;
printf("Enter any hexadecimal number: ");
gets(hex);
for(i=0; hex[i]!='\0'; i++)
{ switch(hex[i])
{
case '0': strcat(bin, "0000"); break;
case '1': strcat(bin, "0001"); break;
case '2': strcat(bin, "0010"); break;
case '3': strcat(bin, "0011"); break;
case '4': strcat(bin, "0100"); break;
case '5': strcat(bin, "0101"); break;
case '6': strcat(bin, "0110"); break;
case '7': strcat(bin, "0111"); break;
case '8': strcat(bin, "1000"); break;
case '9': strcat(bin, "1001"); break;
case 'a': case 'A': strcat(bin, "1010"); break;
case 'b': case 'B': strcat(bin, "1011"); break;
case 'c': case 'C': strcat(bin, "1100"); break;
case 'd': case 'D': strcat(bin, "1101"); break;
case 'e': case 'E': strcat(bin, "1110"); break;
case 'f': case 'F': strcat(bin, "1111"); break;
default: printf("Invalid hexadecimal input.");
} }
printf("%s", bin);
return 0;
}
Output:

Page 56 of 120
50) Write a C program to convert a number from hexa
decimal to octa.
#include <stdio.h>
int main()
{ char hex[17];
long long octal, bin, place;
int i = 0, rem, val;
gets(hex);
octal = 0ll;
bin = 0ll;
place = 0ll;
for(i=0; hex[i]!='\0'; i++)
{ bin = bin * place;
switch(hex[i])
{ case '0': bin += 0; break;
case '1': bin += 1; break;
case '2': bin += 10; break;
case '3': bin += 11; break;
case '4': bin += 100; break;
case '5': bin += 101; break;
case '6': bin += 110; break;
case '7': bin += 111; break;
case '8': bin += 1000; break;
case '9': bin += 1001; break;
case 'a': case 'A': bin += 1010; break;
case 'b': case 'B': bin += 1011; break;
case 'c': case 'C': bin += 1100; break;
case 'd': case 'D': bin += 1101; break;
case 'e': case 'E': bin += 1110; break;
case 'f': case 'F': bin += 1111; break;
default: printf("Invalid hexadecimal input.");
}
place = 10000;
}

Page 57 of 120
place = 1;
while(bin > 0)
{ rem = bin % 1000;
switch(rem)
{ case 0: val = 0; break;
case 1: val = 1; break;
case 10: val = 2; break;
case 11: val = 3; break;
case 100: val = 4; break;
case 101: val = 5; break;
case 110: val = 6; break;
case 111: val = 7; break;
}
octal = (val * place) + octal;
bin /= 1000;
place *= 10;
}
printf("Hexadecimal number = %s\n", hex);
printf("Octal number = %lld", octal);
return 0;
}
Output:

Page 58 of 120
51) Write a C program to convert a number from octal to
decimal.
#include<stdio.h>
#include<ma th.h>
int main()
{
int i,n,r,sum=0,temp;
scanf("%d",&n);
temp=n;
for (i=0;n!=0;n=n/10,i++)
{
sum=sum+(n%10*pow(8,i));
}
printf("\n %d",sum);
return 0;
}

Output:

Page 59 of 120
52) Write a C program to convert a number from octal to
binary.
#include<stdio.h>
#include<math.h>
int main()
{ int n,temp,p=1,dec=0,i=1,j,d,binn=0;
scanf("%d",&n);
temp=n;
for (j=n;j>0;j=j/10)
{ d=j%10;
if (i==1)
p=p*1;
else
p=p*8;
dec=dec+(d*p);
i++;
}
i=1;
for (j=dec;j>0;j=j/2)
{
binn=binn+(dec%2)*i;
i=i*10;
dec=dec/2;
}
printf("%d",binn);
return 0;
}
Output:

Page 60 of 120
53) Write a C program to convert a number from octal to
hexa decimal.

#include <stdio.h>
int main()
{ int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};
long long octal, tempOctal, binary, place;
char hex[65] = "";
int rem;
place = 1;
binary = 0;
printf("Enter any octal number: ");
scanf("%lld", &octal);
tempOctal = octal;
while(tempOctal > 0)
{
rem = tempOctal % 10;
binary = (OCTALVALUES[rem] * place) + binary;
tempOctal /= 10;
place *= 1000;
}
while(binary > 0)
{
rem = binary % 10000;
switch(rem)
{
case 0: strcat(hex, "0"); break;
case 1: strcat(hex, "1"); break;
case 10: strcat(hex, "2"); break;
case 11: strcat(hex, "3"); break;
case 100: strcat(hex, "4"); break;
case 101: strcat(hex, "5"); break;
case 110: strcat(hex, "6"); break;
case 111: strcat(hex, "7"); break;
case 1000: strcat(hex, "8"); break;

Page 61 of 120
case 1001: strcat(hex, "9"); break;
case 1010: strcat(hex, "A"); break;
case 1011: strcat(hex, "B"); break;
case 1100: strcat(hex, "C"); break;
case 1101: strcat(hex, "D"); break;
case 1110: strcat(hex, "E"); break;
case 1111: strcat(hex, "F"); break;
}
binary /= 10000;
}
strrev(hex);
printf(" %s", hex);
return 0;
}
Output:

Page 62 of 120
54) Given a sorted array with possible duplicated in the
array. The Task is to find indexes of 1st and last occurrence of
x in a given array.
#include<stdio.h>
int main()
{
int s,count=0,n,i;
scanf("%d",&n);
int x, arr[n];
for (i=0;i<n;i++) scanf("%d",&arr[i]);
scanf("%d",&x);
for (i=0;i<n;i++)
{ if (arr[i]==x)
{ count++;
if (count==1) printf("%d",i);
s=i;
} }
printf("%d",s);
return 0;
}
Output:

Page 63 of 120
55) Write a c program to rotate a given matrix in clock wise
degrees (90).
#include<stdio.h>
int main()
{ int i,j,temp,n;
scanf("%d",&n);
int arr[n][n];
for (i=0;i<n;i++)
{ for (j=0;j<n;j++) scanf("%d",&arr[i][j]);
}
for (i=0;i<n/2;i++)
{ for (j=i;j<n-i-1;j++)
{ temp=arr[i][j];
arr[i][j]=arr[n-1-j][i];
arr[n-1-j][i]=arr[n-1-i][n-1-j];
arr[n-1-i][n-1-j]=arr[j][n-1-i];
arr[j][n-1-i]=temp;
} }
for (i=0;i<n;i++)
{ for (j=0;j<n;j++) printf("\t %d",arr[i][j]);
printf("\n");
}
return 0;
}
Output:

Page 64 of 120
56) Write a c program to rotate given matrix into anti
clockwise in degrees(90).
#include<stdio.h>
int main()
{ int i,j,temp,n;
scanf("%d",&n);
int arr[n][n];
for (i=0;i<n;i++)
{ for (j=0;j<n;j++) scanf("%d",&arr[i][j]);
}
for (i=0;i<n/2;i++)
{ for (j=i;j<n-i-1;j++)
{ temp=arr[j][n-1-i];
arr[j][n-1-i]=arr[n-1-i][n-1-j];
arr[n-1-i][n-1-j]=arr[n-1-j][i];
arr[n-1-j][i]=arr[i][j];
arr[i][j]=temp;
} }
for (i=0;i<n;i++)
{ for (j=0;j<n;j++) printf("\t %d",arr[i][j]);
printf("\n");
}
return 0;
}
Output:

Page 65 of 120
57) Write a c program to rearrange elements in the array.
The pattern of rearrangement is as follows: 1st minimum
element, 1st maximum element, 2nd min element, 2nd maximum
element.
#include<stdio.h>
int main()
{ int n,i;
scanf("%d",&n);
int arr[n];
for (i=0;i<n;i++) scanf("%d",&arr[i]);
int j;
if (n%2==0)
{ for (i=0,j=n-1;i<n/2,j>=n/2;i++,j--)
{ printf("\t %d",arr[i]);
printf("\t %d",arr[j]);
} }
if (n%2!=0)
{ for (i=0,j=n-1;i<=(n/2),j>=(n/2);i++,j--)
{ if (i==(n/2)) printf("\t %d",arr[i]);
else
{ printf("\t %d",arr[i]);
printf("\t %d",arr[j]);
} } }
return 0;
}
Output:

Page 66 of 120
58) Write a C program to find the product of n special
numbers they have an constraint. The constraint is:
a. Number contains only 1s and 0s.
b. Number starts with 1.
c. And also a number contains only 1.
#include<stdio.h>
int main()
{ int n,i;
scanf("%d",&n);
int arr[n];
int c,x,z,zeroes=0,prod=1;
for (i=0;i<n;i++)
{ scanf("%d",&x);
c=x;
z=0;
while (c%10==0)
{ z++;
c=c/10;
}
if (c==1) zeroes=zeroes+z;
else prod=x;
}
printf("%d",prod);
for (i=0;i<zeroes;i++) printf("0");
return 0;
}
Output:

Page 67 of 120
59) Write a C program to explain call by value.

#include<stdio.h>
swap(int,int);
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("\n before swapping %d %d",a,b);
swap(a,b);
printf("After swapping %d %d\n",a,b);
return 0;
}
swap (int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("\n after swapping %d %d",x,y);
}
Output:

Page 68 of 120
60) WriteaCprogram to explaincall by reference.
#include<stdio.h>
swap(int *,int *);
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("\n before swapping %d %d",a,b);
swap(&a,&b);
printf("\n After swapping %d %d\n",a,b);
return 0;
}
swap (int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("\n after swapping %d %d",*x,*y);
}

Output:

Page 69 of 120
61) Write a C program to find factorial of a given number
using recursion.
#include<stdio.h>
int rec(int);
int main()
{
int n;
scanf("%d",&n);
int c=rec(n);
printf("factorial=%d",c);
return 0;
}
int rec(int x)
{
if (x==1)
return 1;
else
return x*rec(x-1);
}
Output:

Page 70 of 120
62) Write a C program to print nth Fibonacci series.
#include<stdio.h>
int rec(int);
int main()
{
int n,f;
scanf("%d",&n);
int i;
for (i=0;i<n;i++)
{
Printf(“\t%d”,rec(i));
}
return 0;
}
rec (int x)
{
if (x==0)
return 0;
if (x==1||x==2)
return 1;
else
return rec(x-1)+rec(x-2);
}
Output:

Page 71 of 120
63) Given a string of character & numbers. Find out the
frequency of each number which is hidden in the string .
#include<stdio.h>
int main()
{
char str[100];
scanf("%s",str);
int a[10],i;
for (i=0;i<10;i++)
{
a[i]=0;
}
i=0;
while(str[i])
{
if (str[i]>='0'&&str[i]<='9')

a[str[i]-'0']++;
i++;
}
for (i=0;i<10;i++) printf("\t %d",a[i]);
return 0;
}
Output:

Page 72 of 120
64) Given an array of n elements, the task is to complete the
program which will print 1 if the triplet exist in array whose
sum is 0.
#include<stdio.h>
int main()
{ int n, count=0,i;
scanf("%d",&n);
int a[n];
for (i=0;i<n;i++) scanf("%d",&a[i]);
int f=0;
for (i=0;i<n;i++)
{ int l=i+1;
int r=n-1;
int x=a[i];
while (l<r)
{ if (x+a[l]+a[r]==0)
{ printf("\n %d %d %d",x,a[l],a[r]);
l++;
r--;
f=1;
count++;
}
else if (x+a[l]+a[r]<0) l++;
else r--;
} }
if (f==0) printf("0");
else printf("\n1");
printf("\n number of triplets=%d",count);
return 0;
}
Output:

Page 73 of 120
65) Given an matrix of mxn which is 0 s and 1s. If an element
is 0. Write a c program to set its entry rows and columns to 0
and print the modified array.
#include<stdio.h>
void arr(int a,int b,int m,int n)
{ int c[m][n],i,j;
for (i=0;i<m;i++) for (j=0;j<n;j++) c[i][j]=1;
for (i=0;i<m;i++) for (j=0;j<n;j++) if (i==a||j==b) c[i][j]=0;
for (i=0;i<m;i++)
{ printf("\n");
for (j=0;j<n;j++) printf("\t %d",c[i][j]);
} }
int main()
{ int m,n;
scanf("%d %d",&m,&n);
int a[m][n],i,j,k,l;
for (i=0;i<m;i++)
for (j=0;j<n;j++) scanf("%d",&a[i][j]);
for (i=0;i<m;i++)
{ for (j=0;j<n;j++)
{ if (a[i][j]==0)
{ l=i;k=j;
arr(l,k,m,n);
break;
} } }
return 0;
}
Output:

Page 74 of 120
66) Write a c program with a given array“A”and a number
“X” check for a pairof array with sum as x.
#include<stdio.h>
int main()
{
int i,j,n,x;
scanf("%d %d",&n,&x);
int a[n];
for (i=0;i<n;i++) scanf("%d",&a[i]);
for (i=0;i<n;i++)
{ for (j=0;j<i;j++)
{ if (a[i]+a[j]==x)
printf("\n %d %d",a[i],a[j]);
}
}
return 0;
}
Output:

Page 75 of 120
67) Given a sorted array with possibleduplicate element the
taskin to find index of 1st and last occurrence.
#include<stdio.h>
int main()
{ int n,x,num;
scanf("%d",&n);
int count=0;
int i,a[n];
for (i=0;i<n;i++) scanf("%d",&a[i]);
scanf("%d",&x);
for (i=0;i<n;i++)
{ if (a[i]==x)
{ count++;
if (count==1) printf("first occurence=%d",i);
num=i;
} }
printf("last occurence=%d",num);
return 0;
}
Output:

Page 76 of 120
68) Given a mxn square matrix.Write a c program to return
an array of its antidiagonal elements.
#include<stdio.h>
int main()
{ int n,x,b;
scanf("%d",&n);
int a[n][n],i,j;
for (i=0;i<n;i++) for (j=0;j<n;j++) scanf("%d",&a[i][j]);
x=0;
b=n-1;
while (x<=2*b)
{ printf("\n");
for (i=0;i<n;i++) for (j=0;j<n;j++) if (i+j==x)
printf("%d\t",a[i][j]);
x++;
}
return 0;
}
Output:

Page 77 of 120
69) Given an integer array. Find if an integer P exists in the
array such that the number of integers greater than p in the
array equal to p. If such integer is found return 1 else return 0.
#include<stdio.h>
int main()
{ int found=0,p,n,count=0;
scanf("%d",&n);
scanf("%d",&p);
int i;
int a[n];
for (i=0;i<n;i++)
{ scanf("%d",&a[i]);
if (a[i]==p) found=1;
}
if (found==1) for (i=0;i<n;i++) if (a[i]>p) count++;
if (count==p) printf("\n 1");
else printf("\n 0");
if (found==0) printf("\n 0");
return 0;
}
Output:

Page 78 of 120
70) Write a c program to print sum of elements in an given
array using pointers and arguments.
#include<stdio.h>
void sum(int *,int);
int main()
{ int n;
scanf("%d",&n);
int a[n], i;
for (i=0;i<n;i++) scanf("%d",&a[i]);
sum(a,n);
return 0;
}
void sum(int a[100],int x)
{ int sum=0;
int i, * p[x];
for (i=0;i<x;i++) p[i]=&a[i];
for (i=0;i<x;i++) sum=sum+*p[i];
printf("%d",sum);
}
Output:

Page 79 of 120
71) Given a unsorted array find the maximum difference
between successive elements in sorted form.
#include<stdio.h>
int main()
{
int n, i, j, temp;
scanf("%d",&n);
int a[n];
for (i=0;i<n;i++) scanf("%d",&a[i]);
for (i=0;i<n;i++)
{ for (j=i+1;j<n;j++)
{ if (a[i]>a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
int c=a[n-1]-a[n-2];
printf("max difference=%d",c);
return 0;
}
Output:

Page 80 of 120
72) Write a c program to print sum of elements in an array
using pointers.
#include<stdio.h>
int main()
{ int n,i;
int* p[100];
scanf("%d",&n);
int a[n];
for (i=0;i<n;i++)
{ scanf("%d",&a[i]);
p[i]=&a[i];
}
int sum=0;
for (i=0;i<n;i++) sum=sum+(*p[i]);
printf("%d",sum);
return 0;
}
Output:

Page 81 of 120
73) Write a c program to read elements in given array and
print the reverse of the array.
#include<stdio.h>
int main()
{ int n,temp;
int* p[10];
scanf("%d",&n);
int i,a[n];
for (i=0;i<n;i++)
{ scanf("%d",&a[i]);
p[i]=&a[i];
}
i=0;
int j=n-1;
while (i<j)
{ temp=*p[i];
*p[i]=*p[j];
*p[j]=temp;
i++;
j--;
}
for (i=0;i<n;i++) printf("%d",*p[i]);
return 0;
}
Output:

Page 82 of 120
74) Write a c program to find the length of given string using
pointers.

#include<stdio.h>
int main()
{
char str[100];
scanf("%s",&str);
int i=0;
char *p;
p=str;
while (*p!='\0')
{
i++;
p++;
}
printf("%d",i);
return 0;
}
Output:

Page 83 of 120
75) Write a c program to print the respective number of a
given alphabet.

#include<stdio.h>
#include<string.h>
int main()
{
int ans=0,i;
char str[10];
scanf("%s",str);
for (i=0;i<strlen(str);i++)
{
ans=(ans*26)+(str[i]-64);
}
printf("%d",ans);
return 0;
}
Output:

Page 84 of 120
76) Write a c program to print respective alphabet of a given
number.
#include<stdio.h>
int main()
{ int i,n;
scanf("%d",&n);
char str[100];
int count=0;
for (i=0;n>0;i++)
{ if (n%26!=0)
{ str[i]=n%26+64;
count++;
}
else
{ str[i]='Z';
n--;
count++;
}
n=n/26;
}
for (i=count-1;i>=0;i--) printf("%c",str[i]);
return 0;
}
Output:

Page 85 of 120
77) Write a c program to sort given elements in ascending
order.

#include<stdio.h>
int main()
{ int n,i,j,temp;
scanf("%d",&n);
int a[n];
for (i=0;i<n;i++) scanf("%d",&a[i]);
for (i=0;i<n;i++)
{ for (j=i+1;j<n;j++)
{ if (a[i]>a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
for (i=0;i<n;i++) printf("\t %d",a[i]);
return 0;
}

Output:

Page 86 of 120
78) Write a c program to sort given elements in descending
order.
#include<stdio.h>
int main()
{ int n,i,j,temp;
scanf("%d",&n);
int a[n];
for (i=0;i<n;i++) scanf("%d",&a[i]);
for (i=0;i<n;i++)
{ for (j=i+1;j<n;j++)
{ if (a[i]<a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
for (i=0;i<n;i++) printf("\t %d",a[i]);
return 0;
}
Output:

Page 87 of 120
79) Write a c program to print all distinct elements of given
array print total number of distinct elements in it.
#include<stdio.h>
void dis(int n,int a[100],int b[100])
{ int j,i,c1=0,n1=0;
for (i=0;i<n;i++)
{ c1=0;
for (j=0;j<n;j++)
{ if (a[i]==b[j])
c1++;
}
if (c1==0)
{ printf("\t%d",a[i]);
n1++;
} } }
int main()
{ int i,n,j;
scanf("%d",&n);
int c1=0,c2=0,n1=0,n2=0;
int a[n],b[n];
for (i=0;i<n;i++) scanf("%d",&a[i]);
for (i=0;i<n;i++) scanf("%d",&b[i]);
dis(n,a,b);
dis(n,b,a);
return 0;
}

Output:

Page 88 of 120
80) Write a c program to merge two sorted array.
#include<stdio.h>
void merge(int a[],int m,int b[],int n,int c[])
{ int i,j,k;
j=k=0;
for (i=0;i<m+n;) if (j<m && k<n) if (a[j]<b[k])
{ c[i]=a[j];
j++;
} else
{ c[i]=b[k];
k++;
} i++;
else if (j==m)
{ for (;i<m+n;)
{ c[i]=b[k];
k++;
i++;
} }
else
{ for (;i<m+n;)
{ c[i]=a[j];
j++; i++;
} } }
int main()
{ int i,m,n;
scanf("%d %d",&m,&n);
int a[m],b[n],c[n];
for (i=0;i<m;i++) scanf("%d",&a[i]);
for (i=0;i<n;i++) scanf("%d",&b[i]);
merge (a,m,b,n,c);
for (i=0;i<m+n;i++) printf("\t %d",c[i]);
return 0;
}
Output:

Page 89 of 120
81) Write a c program to find number of squares inscribed in
a rectangle provided square can cover the area more than
surface area of rectangle.
#include<stdio.h>
int main()
{
int l,b,a;
scanf("%d %d %d",&l,&b,&a);
int c,d;
c=l/a;
if (l%a>0)
c++;
d=b/a;
if (d%a>0)
d++;
printf("\n number of squares=%d",c*d);
return 0;
}
Output:

Page 90 of 120
82) Write a c program to print days in a week using enum.

#include<stdio.h>
typedef enum
{
mon,tue,wed,thu=100,fri,sat
}day;
int main()
{
day d;
d=tue;
printf("%d",d);
return 0;
}
Output:

Page 91 of 120
83) Write a c program to print address of a student using
nested structures.
#include<stdio.h>
typedef struct
{
char hno[10];
int street;
char colony[100];
}add;
typedef struct
{
char place[100];
int pincode;
add a;
}address;
main()
{ address s;
scanf("%s",s.a.hno); scanf("%d",&s.a.street);
scanf("%s",s.a.colony); scanf("%s",s.place);
scanf("%d",&s.pincode); printf("\n---address of student----");
printf("\nH.no : %s",s.a.hno);
printf("\nRoad no. : %d",s.a.street);
printf("\n%s",s.a.colony);
printf("\n%s",s.place); printf("\n%d",s.pincode);
}
Output:

Page 92 of 120
84) Add Two Distances (in inch-feet) System Using
Structures
#include<stdio.h>
typedef struct
{
int inch;
int feet;
}dist;
int main()
{
dist d1,d2,d3;
printf("\nenter distance 1 in feets ,inches:");
scanf("%d %d",&d1.feet,&d1.inch);
printf("\nenter distance 2 feets,inches:");
scanf("%d %d",&d2.feet,&d2.inch);
d3.inch=d1.inch+d2.inch;
d3.feet=d1.feet+d2.feet;
d3.feet=d3.feet+(d3.inch/12);
d3.inch=d3.inch%12;
printf("\n%d feets %d inches",d3.feet,d3.inch);
return 0;
}
Output:

Page 93 of 120
85) Calculate Difference Between Two Time Periods using
structures
#include<stdio.h>
typedef struct
{
int hrs;
int mins;
int secs;
}time;
int main()
{
time t1,t2,t3;
printf("\nenter time 1:");
scanf("%d %d %d",&t1.hrs,&t1.mins,&t1.secs);
printf("\nenter time 2:");
scanf("%d %d %d",&t2.hrs,&t2.mins,&t2.secs);
t3.hrs=t1.hrs-t2.hrs;
t3.mins=t1.mins-t2.mins;
t3.secs=t1.secs-t2.secs;
printf("\ndifference between 2 time periods=\n%d:%d:
%d",t3.hrs,t3.mins,t3.secs);
return 0;
}
Output:

Page 94 of 120
86) Write a c program to create a text file.
#include<stdio.h>
int main()
{
int n=10;
char str[100]="MANOJ";
FILE *f;
f=fopen("D://manoj.txt", "w");
if (f==NULL) printf("unable to create a file");
fprintf(f,"%d",n);
fprintf(f, "\n%s",str);
fclose(f);
return 0;
}
Output:

Page 95 of 120
87) Write a c program to read a text file.
#include<stdio.h>
int main()
{
int n;
char str[100]="chaitanya";
FILE *p;
p=fopen("D://manoj.txt","r");
if (p==NULL)
{
printf("unable to read a file");
}
fscanf(p,"%d",&n);
printf("%d",n);
fscanf(p,"%s",str);
printf("\n%s",str);
fclose(p);
return 0;
}
Output:

Page 96 of 120
88) Write a c program to append a text file.
#include<stdio.h>
int main()
{
int n=1000;
char str[100]="M1ANOJ";
FILE *p;
p=fopen("D://manoj.txt","a");
fprintf(p,"\n%d",n);
fprintf(p,"\n%s",str);
fclose(p);
return 0;
}
Output:

Page 97 of 120
89) Write a c program to create a binary file.
#include<stdio.h>
int main()
{
FILE *a,*b;
int n=100;
char ch="a";
a=fopen("D:\\manoj.txt", "wb");
fwrite(&n,sizeof(int),1,a);
fclose(a);
return 0;
}
Output:

Page 98 of 120
90) Write a c program to read a binary file.
#include<stdio.h>
int main()
{
FILE *a,*b;
int n=100;
char ch="a";
b=fopen("D:\\manoj.txt","rb");
fread(&n,sizeof(int),1,b);
printf("%d",n);
fclose(b);
return 0;
}
Output:

Page 99 of 120
91) Write a c program to append a binary file.
#include<stdio.h>
int main()
{
FILE *a,*b,*c;
int n,h;
scanf("%d",&h);
a=fopen("D:\\manoj.txt","ab");
b=fopen("D:\\manoj.txt","rb");

fwrite(&h,sizeof(int),1,a);
fclose(a);
fread(&n,sizeof(int),1,b);
printf("%d",n);
fclose(b);

return 0;
}
Output:

Page 100 of 120


92) Write a c program to copy one file to another.

#include<stdio.h>
int main()
{ char ch;
FILE *a,*b;
a=fopen("D://manoj.txt","r");
b=fopen("D://chaitu.txt","w");
if (a==NULL) printf("\n unable to read");
if (b==NULL) printf("\n 2unable to read");
while ((ch=fgetc(a))!=EOF)
{
fputc(ch,b);
}
printf("\n copied");
fclose(a);
fclose(b);
return 0;
}

Page 101 of 120


93) Write a c program to print number of words, lines, and
characters in a file.

Page 102 of 120


94) Write a c program to encrypt a file
#include<stdio.h>
int main()
{
FILE *p,*q;
p=fopen("D:\\dev c programs;\\mains\\file.c","r");
q=fopen("C:\\Users\\vbrao\\Desktop\\manoj.txt","w");
if (p==NULL)
{
printf("\nunable to read");
}
char ch;
while((ch=fgetc(p))!=EOF)
{
ch=ch+5;
putc(ch,q);
}
fclose(p);
fclose(q);
return 0;
}

Page 103 of 120


95) Write a c program to decrypt a file.
#include<stdio.h>
int main()
{ FILE *p,*q;
p=fopen("C:\\Users\\vbrao\\Desktop\\manoj.txt","r");
q=fopen("C:\\Users\\vbrao\\Desktop\\mano.txt","w");
if (p==NULL) printf("\nunable to read");
char ch;
while((ch=fgetc(p))!=EOF)
{ ch=ch-5;
putc(ch,q);
}
fclose(p);
fclose(q);
return 0;
}
Output:

Page 104 of 120


96) Write a c program to convert lower case letters to upper
case letters in a file.
#include<stdio.h>
int main()
{
FILE *p,*q;
p=fopen("C:\\Users\\vbrao\\Desktop\\manoj.txt","r");
q=fopen("C:\\Users\\vbrao\\Desktop\\mano.txt","w");
if (p==NULL)
{
printf("\nunable to read");
}
char ch;
while((ch=fgetc(p))!=EOF)
{
if (ch>=97&&ch<=122)
ch=ch-32;
putc(ch,q);
}
fclose(p);
fclose(q);
return 0;
}
Output:

Page 105 of 120


97) Write a c program to print the source code of the file.
#include<stdio.h>
int main()
{ FILE *p,*q;
p=fopen("D:\\dev c programs;\\mains\\file.c","r");
q=fopen("C:\\Users\\vbrao\\Desktop\\manoj.txt","w");
if (p==NULL) printf("\nunable to read");
char ch;
while(ch!=EOF)
{ ch=fgetc(p);
putc(ch,q);
}
fclose(p);
fclose(q);
return 0;
}
Output:

Page 106 of 120


98) Write a c program to accept 10 student details and store
the information in a file.
#include<stdio.h>
typedef struct
{ int hno;
char name[100];
int total;
}stud;
int main()
{ stud s[10];
int i,h;
printf("\nenter number of students:");
scanf("%d",&h);
for (i=0;i<h;i++)
{ printf("\nenter hallticket number");
scanf("%d",&s[i].hno);
printf("\nenter name");
scanf("%s",s[i].name);
printf("\nenter total marks");
scanf("%d ",&s[i].total);
}
FILE *p;
int tot;
float avg,per;
char grade;
p=fopen("D://student.txt","w");
for (i=0;i<h;i++)
{ fprintf(p,"\nname of students:%s",s[i].name);
fprintf(p,"\nhall ticket number:%d",s[i].hno);
fprintf(p,"\ntotal marks:");
fprintf(p,"\nmarks=%d",s[i].total);
avg=s[i].total/5;
per=(s[i].total*100)/150;

Page 107 of 120


fprintf(p,"\n percentage=%.2f",per);
if (per>=90)
fprintf(p,"\nGRADE=A");
else if ((per>=80)&&(per<90))
fprintf(p,"\nGRADE=B");
else if ((per>=70)&&(per<80))
fprintf(p,"\nGRADE=C");
else if ((per>=60)&&(per<70))
fprintf(p,"\nGRADE=D");
else if ((per>=50)&&(per<60))
fprintf(p,"\nGRADE=E");
else if(per<50)
fprintf(p,"\nGRADE=F");
if (per>50)
fprintf(p,"\npass");
else
fprintf(p,"\nfail");
fprintf(p,"\n");
}
fclose(p);
FILE *v;
int x,c=0;
printf("\nenter hall ticket number to search");
scanf("%d",&x);
v=fopen("D://student.txt","r");
for (i=0;i<h;i++)
{ if (s[i].hno==x)
{ fscanf(v,"%s",s[i].name);
fscanf(v,"%d",&s[i].hno);
fscanf(v,"%f",per);
if (per>50)
printf("\npass");
else
printf("\nfail");
c++;
} }

Page 108 of 120


if (c==0)
printf("\nsearch fails");
fclose(v);
return 0;
}
Output:

Page 109 of 120


Page 110 of 120
99) Write a c program to accept 10 electricity customer
details and store the bill information in a file.
#include<stdio.h>
typedef struct
{
char nam[100];
int mno;
int punits,cunits;
}cust;
int main()
{
cust c[100];
int v,n,i,h;
float d;
printf("\n enter number of customers");
scanf("%d",&h);
for (i=0;i<h;i++)
{
printf("\nenter name:");
scanf("%s",c[i].nam);
printf("\nenter meter number:");
scanf("%d",&c[i].mno);
printf("\nenter previous units:");
scanf("%d",&c[i].punits);
printf("\n enter number of current units:");
scanf("%d",&c[i].cunits);
}
FILE *p;
p=fopen("D://electicity.txt","w");
for (i=0;i<h;i++)
{
fprintf(p,"\nname=%s",c[i].nam);
fprintf(p,"\nmeter number=%d",c[i].mno);

Page 111 of 120


fprintf(p,"\nprevious units=%d",c[i].punits);
fprintf(p,"\npresent units=%d",c[i].cunits);
v=c[i].cunits-c[i].punits;
if (v<0)
{
d=10;
fprintf(p,"\nbill=Rs.%.2f",d);
}
else if (v>=0&&v<=50)
{
d=v*2.00;
fprintf(p,"\nbill=Rs.%.2f",d);
}
else if (v>50&&v<=200)
{
d=v*2.50;
fprintf(p,"\nbill=Rs.%.2f",d);
}
else if (v>200&&v<=400)
{
d=v*3.50;
fprintf(p,"\nbill=Rs.%.2f",d);
}
else if (v>400&&v<=550)
{
d=v*4.50;
fprintf(p,"\nbill=Rs.%.2f",d);
}
else if (v>550)
{
d=v*5.00;
fprintf(p,"\nbill=Rs.%.2f",d);
}

Page 112 of 120


fprintf(p,"\n");

}
fclose(p);
FILE *m;
m=fopen("D://electicity.txt","r");
for (i=0;i<h;i++)
{
fscanf(m,"%s",c[i].nam);
fscanf(m,"%d",&c[i].mno);
fscanf(m,"%d",&c[i].punits);
fscanf(m,"%d",&c[i].cunits);
fscanf(m,"%.2f",&d);
}
char x[100];
int l=0;
int u,j;
printf("\nenter meter number to search:");
scanf("%d",&u);
for (j=0;j<h;j++)
{
if (u==c[j].mno)
{
printf("\nmeter number=%d",c[j].mno);
printf("\nbill=Rs.%.2f",d);
l++;
}
}
if (l==0)
printf("\nsearch failed");
fclose(m);
return 0;
}

Page 113 of 120


Output:

Page 114 of 120


Page 115 of 120
100) Write a c program to create memory for integer,
character and float variables at runtime.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *i;
char* c;
float* f;
i=(int*)malloc(sizeof(int)*1);
c=(char*)malloc(sizeof(char)*1);
f=(float*)malloc(sizeof(float)*1);
scanf("%d",i);
scanf(" %c",c);
scanf("%f",f);
printf("%d \n %c \n %.2f",*i,*c,*f);
free(i);
free(c);
free(f);
return 0;
}
Output:

Page 116 of 120


101) Write a c program to input and print text using dynamic
array memory allocation.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int *text;
int n;
scanf("%d",&n);
text=(char*)malloc(sizeof(char)*n);
scanf("%s",text);
printf("%s",text);
free(text);
return 0;
}
Output:

Page 117 of 120


102) Write a c program to read one dimensional array and
print sum of all elements using dynamic array memory
allocation.
#include<stdio.h>
#include<stdlib.h>
int main()
{ int n,i,sum=0;
int *a;
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n);
for (i=0;i<n;i++)
{ scanf("%d",a+i);
sum=sum+*(a+i);
}
printf("\n%d",sum);
for (i=0;i<n;i++) printf("n%d",*(a+i));
return 0;
}
Output:

Page 118 of 120


103) Write a c program to print two dimensional array using
dynamic array memory allocation.
#include<stdio.h>
int main()
{ int **a;
int m,n;
scanf("%d %d",&m,&n);
a=(int**)malloc(sizeof(int*)*m);
int i,j;
for (i=0;i<m;i++) a[i]=(int*)malloc(n*sizeof(int));
for (i=0;i<m;i++)
for (j=0;j<n;j++) scanf("%d",&a[i][j]);
for (i=0;i<m;i++)
{
printf("\n");
for (j=0;j<n;j++) printf("\t%d",a[i][j]);
}
return 0;
}
Output:

Page 119 of 120


104) Write a c program to print the values in a array by
decrementing the pointer using dynamic array memory
allocation.
#include<stdio.h>
int main()
{ int *p,i,n,a[100],x;
scanf("%d",&n);
p=(int*)malloc(sizeof(int)*n);
for (i=0;i<n;++i)
{ scanf("%d",&a[i]);
p=&a[i];
p++;
}
p=p-1;
for (i=0;i<n;i++)
{ printf("%d ",*p);
p--;
}
return 0;
}
Output:

Page 120 of 120

S-ar putea să vă placă și