0% found this document useful (0 votes)
14 views53 pages

C Programming Problem Sheet

The document contains a series of C programming exercises, each demonstrating different programming concepts such as input/output, arithmetic operations, control structures (if statements, switch cases), and loops. Each exercise includes a brief description followed by the corresponding C code. Topics covered range from basic programs like printing messages and calculating sums to more complex tasks like solving quadratic equations and generating Fibonacci series.

Uploaded by

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

C Programming Problem Sheet

The document contains a series of C programming exercises, each demonstrating different programming concepts such as input/output, arithmetic operations, control structures (if statements, switch cases), and loops. Each exercise includes a brief description followed by the corresponding C code. Topics covered range from basic programs like printing messages and calculating sums to more complex tasks like solving quadratic equations and generating Fibonacci series.

Uploaded by

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

INTEGER, FLOAT

01. A program to print “This is my first program”.


#include<stdio.h>
#include<stdlib.h>
void main(){
printf("This is my first program");
}
02. A program to add the age of father and son.
#include<stdio.h>
#include<stdlib.h>
void main(){
int x,y,z;
printf("Enter Father's Age: ");
scanf("%d", &x);
printf("Enter Son's Age: ");
scanf("%d", &y);
z=x+y;
printf("Total Age is %d", z);
}
03. A program to convert US dollar to BD taka.
#include<stdio.h>
#include<stdlib.h>
void main(){
float x,y;
printf("Enter US dollar value: ");
scanf("%f", &x);
y=x*79;
printf("The amount of BD taka is %.2f", y);
}
04. A program to calculate the sum of five subjects and find average mark.
#include<stdio.h>
#include<stdlib.h>
void main(){
float s1, s2, s3, s4, s5, sum,avg;
printf("Enter marks of 5 subjects :\n");
scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5);
sum=s1+s2+s3+s4+s5;
printf("Sum:%.2f", sum);
avg=sum/5;
printf("\nAverage:%.2f", avg);
}
05. A program to calculate the area of a circle.
#include<stdio.h>
#include<stdlib.h>
void main(){
float r, area;
printf("Enter radius:");
scanf("%f", &r);
area=3.1416*r*r;
printf("Area=%.2f", area);
}
06. A program to calculate the area of a triangle.
#include<stdio.h>
#include<stdlib.h>
void main(){
float b,h,area;
printf("Enter the values:\n");
printf("Base=");
scanf("%f", &b);
printf("Height=");
scanf("%f", &h);
area=.5*b*h;
printf("Area=%.2f", area);
}

07. A program to calculate the volume and surface area of a cylinder.


#include<stdio.h>
#include<stdlib.h>
void main(){
float r,h,volume,area;
printf("Enter the values:\n");
printf("Rarius=");
scanf("%f", &r);
printf("Height=");
scanf("%f", &h);
volume=3.1416*r*r*h;
area=2*3.1416*r*h;
printf("Volume=%.2f\nArea=%.2f", volume,area);
}

08. A program to calculate net pay.


#include<stdio.h>
#include<stdlib.h>
void main(){
float x,y;
printf("Enter the basic salary: ");
scanf("%f", &x);
y=x+(x*0.4)+1500+200-(x*0.1)-40-100;
printf("Net Pay=%.2f", y);
}
09. A program to distribute property among wife, three daughters and two
sons.
#include<stdio.h>
#include<stdlib.h>
void main(){
float x,w,d,s;
printf("Enter the total property= ");
scanf("%f", &x);
w=x*0.2;
d=x*0.1;
s=x*0.25;
printf("Wife=%.2f\nDaughter=%.2f per head\nSon=%.2f per head", w,d,s);
}
10. A program to swap two numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c;
printf("Enter the values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
c=a;
a=b;
b=c;
printf("After Swapping:\n");
printf("a=%d\nb=%d", a,b);
}

10. A program to swap two numbers.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b;
printf("Enter the values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
a=a+b;
b=a-b;
a=a-b;
printf("After Swapping:\n");
printf("a=%d\nb=%d", a,b);
}

11. A program to compute quotient and reminder.


#include<stdio.h>
#include<stdlib.h>
void main(){
int x, d, q, r;
printf("Enter dividend:");
scanf("%d", &x);
printf("Enter divisor:");
scanf("%d", &d);
q=x/d;
r=x%d;
printf("Quotient=%d\n", q);
printf("Remainder=%d", r);
}

12. A program to convert seconds into minutes and seconds.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c;
printf("Total seconds:");
scanf("%d", &a);
b=a/60;
c=a%60;
printf("Minute=%d\nSeconds=%d", b,c);
}

13. A program to convert seconds into Hour, minutes and seconds.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c,d,e;
printf("Total seconds:");
scanf("%d", &a);
b=a/3600;
c=a%3600;
d=c/60;
e=c%60;
printf("Hour=%d\nMinute=%d\nSeconds=%d", b,d,e);
}
14. A program to convert days into months and days.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c;
printf("Total days:");
scanf("%d", &a);
b=a/30;
c=a%30;
printf("Months=%d\nDays=%d", b,c);
}

15. A program to convert days into year, months and days.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c,d,e;
printf("Total seconds:");
scanf("%d", &a);
b=a/365;
c=a%365;
d=c/30;
e=c%30;
printf("Year=%d\nMonths=%d\nDays=%d", b,d,e);
}

CHARACTER
16. A program to find the ASCII value of a character.
#include<stdio.h>
#include<stdlib.h>
void main(){
char a;
printf("Enter the character: ");
scanf("%c", &a);
printf("ASCII value:%d", a);
}
17. A program to find the character of a ASCII value.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a;
printf("Enter the ASCII value: ");
scanf("%d", &a);
printf("Character:%c", a);
}
IF STATEMENT
18. A program to find largest number among three numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c;
printf("Enter the values:\n");
scanf("%d%d%d", &a,&b,&c);
if(a>b && a>c)
printf("Greatest Value=%d", a);
else if(b>a && b>c)
printf("Greatest Value=%d", b);
else
printf("Greatest Value=%d", c);
}
18. A program to find largest number among three numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c;
printf("Enter the values:\n");
scanf("%d%d%d", &a,&b,&c);
if(a>b){
if(a>c)
printf("Greatest Value=%d", a);
else
printf("Greatest Value=%d", c);
}
else{
if(b>c)
printf("Greatest Value=%d", b);
else
printf("Greatest Value=%d", c);
}
}
18. A program to find largest number among three numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,c,x;
printf("Enter the values:\n");
scanf("%d%d%d", &a,&b,&c);
x=a;
if(b>x)
x=b;
if(c>x)
x=c;
printf("Greatest Value=%d", x);
}

19. A program to check whether a character is alphabet or not.


#include<stdio.h>
#include<stdlib.h>
void main(){
char c;
printf("Enter the character:");
scanf("%c",&c);
if( (c>=65 && c<=90) || (c>=97 && c<=122))
printf("Alphabet");
else
printf("Not alphabet");
}

20. A program to check whether a character is lowercase or uppercase.


#include<stdio.h>
#include<stdlib.h>
void main(){
char c;
printf("Enter the character:");
scanf("%c",&c);
if(c>=97 && c<=122)
printf("Lowercase");
else if(c>=65 && c<=90)
printf("Uppercase");
else
printf("Not alphabet");
}

21. A program to calculate the house rent.


#include<stdio.h>
#include<stdlib.h>
void main(){
float x,y;
printf("Enter the basic salary:");
scanf("%f", &x);
if(x>=15000){
y=x*0.4;
if(y<7500)
printf("House Rent=7500.00 TK");
else
printf("House Rent=%.2f TK", y);
}
else if(x>=8000){
y=x*0.5;
if(y<5500)
printf("House Rent=5500.00 TK");
else
printf("House Rent=%.2f TK", y);
}
else{
y=x*0.6;
if(y<2500)
printf("House Rent=2500.00 TK");
else
printf("House Rent=%.2f TK", y);
}
}

22. A program to solve quadratic equation.


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main(){
float a,b,c,d,x1,x2;
printf("Enter the values:\n");
printf("a=");
scanf("%f", &a);
printf("b=");
scanf("%f", &b);
printf("c=");
scanf("%f", &c);
d=(b*b)-4*a*c;
if(d<0)
printf("Imaginary");
else{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("x1=%.2f\nx2=%.2f", x1,x2);
}
}
23. A program to print the grade of a given mark.
#include<stdio.h>
#include<stdlib.h>
void main(){
int x;
printf("Enter the marks:");
scanf("%d", &x);
if(x>100)
printf("Error");
else if(x>=80)
printf("A+");
else if(x>=75)
printf("A");
else if(x>=70)
printf("A-");
else if(x>=65)
printf("B+");
else if(x>=60)
printf("B");
else if(x>=55)
printf("B-");

else if(x>=50)
printf("C+");
else if(x>=45)
printf("C");
else if(x>=40)
printf("D");
else
printf("F");
}
SWITCH
24. A program to add, subtract, multiply or divide using switch.
#include<stdio.h>
#include<stdlib.h>
void main(){
float a,b,result;
int x;
printf("Enter the two values:\n");
scanf("%f%f", &a,&b);
printf("Enter 1 to add\n 2 to subtract\n 3 to multiply\n 4 to divide\n");
scanf("%d", &x);
switch(x){
case 1:
result=a+b;
printf("Result=%.2f", result);
break;
case 2:
result=a-b;
printf("Result=%.2f", result);
break;
case 3:
result=a*b;
printf("Result=%.2f", result);
break;
case 4:
result=a/b;
printf("Result=%.2f", result);
break;
default:
printf("Error");
}
}

24. A program to make a simple calculator using switch.


#include<stdio.h>
#include<stdlib.h>
void main(){
float a,b,result;
char x;
printf("Enter + to add\n - to subtract\n * to multiply\n / to divide\n");
scanf("%c", &x);
printf("Enter the two values:\n");
scanf("%f%f", &a,&b);
switch(x){
case '+':
result=a+b;
printf("Result=%.2f", result);
break;
case '-':
result=a-b;
printf("Result=%.2f", result);
break;
case '*':
result=a*b;
printf("Result=%.2f", result);
break;
case '/':
result=a/b;
printf("Result=%.2f", result);
break;
default:
printf("Error");
}
}

LOOP
25. A program to print number from 1 to N natural numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n;
printf("Enter the value of N:");
scanf("%d", &n);
for(i=1;i<=n;i++) {
printf("%5d", i);
}
}
26. A program to calculate the sum from 1 to N natural numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n,sum=0;
printf("Enter the value of N:");
scanf("%d", &n);
for(i=1;i<=n;i++){
sum=sum+i;
}
printf("SUM=%d", sum);
}
27. A program to print number from M to N natural numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,m,n;
printf("Enter the values:\n");
printf("M=");
scanf("%d", &m);
printf("N=");
scanf("%d", &n);
for(i=m;i<=n;i++){
printf("%5d", i);
}
}
28. A program to calculate sum from M to N natural numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,m,n,sum=0;
printf("Enter the values:\n");
printf("M=");
scanf("%d", &m);
printf("N=");
scanf("%d", &n);
for(i=m;i<=n;i++){
sum=sum+i;
}
printf("SUM=%d", sum);
}
29. A program to generate multiplication table.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n, i;
printf("Enter the value:");
scanf("%d",&n);
for(i=1; i<=10; ++i){
printf("%d * %d = %d \n", n, i, n*i);
}
}
30. A program to print 1 0 1 0 1 0 1 0 .........Nth term.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n;
printf("Enter the value of N:");
scanf("%d", &n);
for(i=1;i<=n;i++){
if(i%2==1)
printf("1 ");
if(i%2==0)
printf("0 ");
}
}
31. A program to print the Fibonacci series to Nth term.[0 1 1 2 3
5……Nth]
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n,x=0,y=1;
printf("Enter the value of N:");
scanf("%d", &n);
printf("Fibonacci Series:");
for(i=1;i<=n;i++){
printf("%5d", x);
y=x+y;
x=y-x;
}
}
31. A program to print the Fibonacci series to Nth term.[0 1 1 2 3
5……Nth]
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n,x=0,y=1,k;
printf("Enter the total number of terms:");
scanf("%d", &n);
printf("Fibonacci Series:");
for(i=1;i<=n;i++){
printf("%5d", x);
k=x;
x=y;
y=k+x;
}
}
32. A program to calculate the sum of Fibonacci series to Nth term.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n,x=0,y=1,k,sum=0;
printf("Enter the value of N:");
scanf("%d", &n);
for(i=1;i<=n;i++){
sum=sum+x;
k=x;
x=y;
y=k+x;
}
printf("SUM=%d", sum);
}
33. A program to calculate the factorial of a given number.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n,fact=1;
printf("Enter the value of N:");
scanf("%d", &n);
for(i=1;i<=n;i++){
fact=fact*i;
}
printf("Factorial=%d", fact);
}
34. A program to calculate the power of a given number.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,a,b,result=1;
printf("Enter the values:\n");
printf("Base=");
scanf("%d", &a);
printf("Power=");
scanf("%d", &b);
for(i=1;i<=b;i++){
result=result*a;
}
printf("Result=%d", result);
}
34. A program to calculate the power of a given number.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main(){
int i,a,b,result;
printf("Enter the values:\n");
printf("Base=");
scanf("%d", &a);
printf("Power=");
scanf("%d", &b);
result=pow(a,b);
printf("Result=%d", result);
}
35. A program to find the factors/divisors of a given number.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,n;
printf("Enter the value of N:");
scanf("%d", &n);
printf("Factors/Divisors:");
for(i=1;i<=n;i++){
if(n%i==0)
printf("%5d", i);
}
}

36. A program to find the multiples of a given number to given range.


#include<stdio.h>
#include<stdlib.h>
void main(){
int i,m,n;
printf("Enter the values:\n");
printf("Value=");
scanf("%d", &n);
printf("Range=");
scanf("%d", &m);
printf("Multiples:");
for(i=n;i<=m;i++){
if(i%n==0)
printf("%5d", i);
}
}

37. A program to find the G.C.D. of two given numbers.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,i,small;
printf("Enter the two values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
if(a<b)
small=a;
else
small=b;
for(i=small;i>=1;i--){
if(a%i==0 && b%i==0){
printf("G.C.D.=%d", i);
break;
}
}
}
38. A program to find the L.C.M. of two given numbers.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,i,big;
printf("Enter the two values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
if(a>b)
big=a;
else
big=b;
for(i=big;i<=a*b;i++){
if(i%a==0 && i%b==0){
printf("L.C.D.=%d", i);
break;
}
}
}
39. A program to calculate nPr.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,r,i,a=1,b=1,result;
printf("Enter the value of n and r:\n");
printf("n=");
scanf("%d", &n);
printf("r=");
scanf("%d", &r);
for(i=1;i<=n;i++)
a=a*i;
for(i=1;i<=n-r;i++)
b=b*i;
result=a/b;
printf("Result=%d", result);
}
40. A program to calculate nCr.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,r,i,a=1,b=1,c=1,result;
printf("Enter the value of n and r:\n");
printf("n=");
scanf("%d", &n);
printf("r=");
scanf("%d", &r);
for(i=1;i<=n;i++)
a=a*i;
for(i=1;i<=r;i++)
b=b*i;
for(i=1;i<=n-r;i++)
c=c*i;
result=a/(b*c);
printf("Result=%d", result);
}
41. A program to count number of digits of a given number.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,c=0;
printf("Enter the number:");
scanf("%d", &n);
while(n!=0){
c++;
n=n/10;
}
printf("Total number of digits=%d", c);
}
42. A program to find the sum of digits of a given number.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,r,sum=0;
printf("Enter the number:");
scanf("%d", &n);
while(n!=0){
r=n%10;
sum=sum+r;
n=n/10;
}
printf("SUM=%d", sum);
}
43. A program to reverse a given number.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,r,m=0;
printf("Enter the number:");
scanf("%d", &n);
while(n!=0){
r=n%10;
m=(m*10)+r;
n=n/10;
}
printf("Reversed Number=%d", m);
}
44. A program to check whether a number is even or odd.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n;
printf("Enter the number:");
scanf("%d", &n);
if(n%2==0)
printf("EVEN");
else
printf("ODD");
}
45. A program to check whether a number is prime or not prime.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i;
printf("Enter the number:");
scanf("%d", &n);
for(i=2;i<n;i++){
if(n%i==0) {
printf("Not Prime");
break;
}
}
if(i==n)
printf("Prime");
}
45. A program to check whether a number is prime or not prime.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,c=0;
printf("Enter the number:");
scanf("%d", &n);
for(i=2;i<n;i++){
if(n%i==0){
c=1;
break;
}
}
if(c==0)
printf("Prime");
else
printf("Not Prime");
}
46. A program to find prime numbers to a given range.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,k;
printf("Enter the range:");
scanf("%d", &n);
printf("Prime Numbers: ");
for(k=2;k<=n;k++){
for(i=2;i<k;i++){
if(k%i==0)
break;
}
if(i==k)
printf("%5d", k);
}
}
47. A program to calculate the sum of prime numbers to a given range.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,k,sum=0;
printf("Enter the range:");
scanf("%d", &n);
printf("Prime Numbers: ");
for(k=2;k<=n;k++){
for(i=2;i<k;i++){
if(k%i==0)
break;
}
if(i==k){
printf("%5d", k);
sum=sum+k;
}
}
printf("\nSUM=%d", sum);
}
48. A program to find prime factors of a given number.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,k;
printf("Enter the number:");
scanf("%d", &n);
printf("Prime Factors:");
for(k=2;k<=n;k++){
if(n%k==0){
for(i=2;i<k;i++){
if(k%i==0)
break;
}
if(i==k)
printf("%5d", k);
}
}
}
49. A program to check whether a number is perfect or not.[6=1+2+3(factors)]
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,sum=0;
printf("Enter the number:");
scanf("%d", &n);
for(i=1;i<n;i++){
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("Perfect");
else
printf("Not Perfect");
}
50. A program to check whether a number is Armstrong or
not.[153=13+53+33]
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,temp,r,i,sum=0;
printf("Enter the number:");
scanf("%d", &n);
temp=n;
while(n!=0){
r=n%10;
n=n/10;
sum=sum+r*r*r;
}
if(sum==temp)
printf("Armstrong");
else
printf("Not Armstrong");
}
51. A program to check whether a number is strong or not.[145=1!+4!+5!]
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,temp,r,i,fact,sum=0;
printf("Enter the number:");
scanf("%d", &n);
temp=n;
while(n!=0){
r=n%10;
n=n/10;
fact=1;
for(i=1;i<=r;i++)
fact=fact*i;
sum=sum+fact;
}
if(sum==temp)
printf("Strong");
else
printf("Not Strong");
}
52. A program to check a number palindrome or not.[131=131(reversed)]
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,temp,r,sum=0;
printf("Enter the number:");
scanf("%d", &n);
temp=n;
while(n!=0){
r=n%10;
n=n/10;
sum=sum*10+r;
}
if(sum==temp)
printf("Palindrome");
else
printf("Not Palindrome");
}
SERIES
53. A program to calculate the sum of series: 1 + 2 + 4 + 8 +............+Nth term.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,x=1,sum=0;
printf("Enter the value:\n");
scanf("%d", &n);
for(i=1;i<=n;i++){
sum=sum+x;
x=x*2;
}
printf("SUM=%d", sum);
}
54. A program to calculate the sum of series: 1 + 3 + 9 + 27 +..........+Nth term.
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,x=1,sum=0;
printf("Enter the value:\n");
scanf("%d", &n);
for(i=1;i<=n;i++){
sum=sum+x;
x=x*3;
}
printf("SUM=%d", sum);
}
55. A program to calculate the sum of series: 1 - 2 + 3 – 4 + 5 – 6 +........... ±N
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,sum=0;
printf("Enter the number:");
scanf("%d", &n);
for(i=1;i<=n;i++){
if(i%2==1)
sum=sum+i;
else
sum=sum-i;
}
printf("SUM=%d", sum);
}
56. A program to calculate the sum of series: 1 + 1/2 + 1/3 + 1/4 +..........+ 1/N.
#include<stdio.h>
#include<stdlib.h>
void main(){
float n,i,sum=0;
printf("Enter the value:\n");
scanf("%f", &n);
for(i=1;i<=n;i++){
sum=sum+(1/i);
}
printf("SUM=%.2f", sum);
}
57. A program to calculate the sum of series: 1/2+2/3+3/4+4/5
+...........+N/(N+1).
#include<stdio.h>
#include<stdlib.h>
void main(){
float n,i,sum=0;
printf("Enter the value:\n");
scanf("%f", &n);
for(i=1;i<=n;i++){
sum=sum+i/(i+1);
}
printf("SUM=%.2f", sum);
}
58. A program to calculate the sum of series: 12 + 22 + 32 + 42 +...........+N2
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,sum=0;
printf("Enter the number:");
scanf("%d", &n);
for(i=1;i<=n;i++){
sum=sum+(i*i);
}
printf("SUM=%d", sum);
}
59. A program to calculate the sum of series: 12 + 32 + 52 + 72 +...........+ (2N-1)2
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,sum=0;
printf("Enter the number:");
scanf("%d", &n);
for(i=1;i<=n;i++){
sum=sum+(2*i-1)*(2*i-1);
}
printf("SUM=%d", sum);
}
60. A program to calculate the sum of series: 1 + 22 + 33 + 44 +...............+NN
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,j,x,sum=0;
printf("Enter the value:\n");
scanf("%d", &n);
for(i=1;i<=n;i++){
x=1;
for(j=1;j<=i;j++)
x=x*i;
sum=sum+k;
}
printf("SUM=%d", sum);
}
61. A program to calculate the sum of series: 1 + x + x2 + x3 + x4 +..........+ xN
#include<stdio.h>
#include<stdlib.h>
void main(){
int x,n,i,sum=1,fact=1;
printf("Enter the values:\n");
printf("x=");
scanf("%d", &x);
printf("n=");
scanf("%d", &n);
for(i=1;i<=n;i++){
fact=fact*x;
sum=sum+fact;
}
printf("SUM=%d", sum);
}
62. A program to calculate the sum of series: 1.22+2.32+3.42 +...........+N.(N+1)2
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,sum=0;
printf("Enter the number:");
scanf("%d", &n);
for(i=1;i<=n;i++){
sum=sum+i*(i+1)*(i+1);
}
printf("SUM=%d", sum);
}
63. A program to calculate the sum of series: 1! + 2! + 3! + 4! +............+N!
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,j,sum=0,fact;
printf("Enter the number:");
scanf("%d", &n);
for(i=1;i<=n;i++){
fact=1;
for(j=1;j<=i;j++)
fact=fact*j;
sum=sum+fact;
}
printf("SUM=%d", sum);
}
64. A program to calculate the sum of series: 12.2!+22.3! +32.4!
+........+N2.(N+1)!
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,x,y=1,sum=0;
printf("Enter the value of N:");
scanf("%d", &n);
for(i=1;i<=n;i++){
x=i*i;
y=y*(i+1);
sum=sum+x*y;
}
printf("SUM=%d", sum);
}
65. A program to calculate the sum of series: 1 +x +x2/2! +x3/3! +..........+xN/N!
#include<stdio.h>
#include<stdlib.h>
void main(){
float x,n,i,j,k,sum=1,fact=1;
printf("Enter the values:\n");
printf("x=");
scanf("%f", &x);
printf("n=");
scanf("%f", &n);
for(i=1;i<=n;i++){
fact=fact*x;
k=1;
for(j=1;j<=i;j++)
k=k*j;
sum=sum+fact/k;
}
printf("SUM=%.2f", sum);
}
PATTERN
66. 1 1 1 1 1
22222
33333
44444
55555
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
printf("%5d", i);
}
printf("\n");
}
}
67. 1
22
333
4444
55555
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%5d", i);
}
printf("\n");
}
}
68. 1
12
123
1234
12345
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%5d", j);
}
printf("\n");
}
}
69. 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 (Floyd’s Triangle)
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j,x=1;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%5d", x);
x++;
}
printf("\n");
}
}

70. 1
00
111
0000
11111
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%5d", i%2);
}
printf("\n");
}
}
71. *
**
***
****
*****
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
}

72. * * * * *
****
***
**
*
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j;
for(i=5;i>=1;i--){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
}
73. *
**
***
****
*****
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j,k;
for(i=1;i<=5;i++){
for(k=5;k>i;k++)
printf(" ");
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
}
74. * * * * *
****
***
**
*
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j,k;
for(i=1;i<=5;i++){
for(k=1;k<i;k--)
printf(" ");
for(j=i;j<=5;j--)
printf("*");
printf("\n");
}
}
75. *
***
*****
*******
*********
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j,k;
for(i=1;i<=5;i++){
for(k=5;k>i;k++)
printf(" ");
for(j=1;j<=(2*i)-1;j++)
printf("*");
printf("\n");
}
}
76. * * * * * * * * *
*******
*****
***
*
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j,k;
for(i=5;i>=1;i--){
for(k=1;k>i;k++)
printf(" ");
for(j=1;j<=(2*i)-1;j++)
printf("*");
printf("\n");
}
}
77. *
***
*****
*******
*********
*******
*****
***
*
#include<stdio.h>
#include<stdlib.h>
void main(){
int i,j,k;
for(i=1;i<=5;i++){
for(k=i;k<5;k++)
printf(" ");
for(j=1;j<=(2*i)-1;j++)
printf("*");
printf("\n");
}
for(i=4;i>=1;i--){
for(k=i;k<5;k++)
printf(" ");
for(j=1;j<=(i*2)-1;j++)
printf("*");
printf("\n");
}
}
ARRAY
78. A program to find the largest element of an array.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
x=a[0];
for(i=1;i<n;i++){
if(a[i]>x)
x=a[i];
}
printf("The greatest element=%d", x);
}

79. A program to find the smallest element of an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
x=a[0];
for(i=1;i<n;i++){
if(a[i]<x)
x=a[i];
}
printf("The smallest element=%d", x);
}

80. A program to find the location of an element in an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the element to search=");
scanf("%d", &x);
for(i=0;i<n;i++){
if(a[i]==x) {
printf("Location=%d", i);
break;
}
}
if(i==n)
printf("The element is not found");

81. A program to insert a new element in the first position of an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the new elements:");
scanf("%d", &x);
for(i=n-1;i>=0;i--){
a[i+1]=a[i];
}
a[0]=x;
n=n+1;
printf("After Insertion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}

82. A program to insert a new element at the end position of an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the new elements:");
scanf("%d", &x);
a[n]=x;
n=n+1;
printf("After Insertion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}

83. A program to insert a new element in the Kth location in an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x,k;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the values:\n");
printf("Position=");
scanf("%d", &k);
printf("New Element=");
scanf("%d", &x);
for(i=n-1;i>=k;i--){
a[i+1]=a[i];
}
a[k-1]=x;
n=n+1;
printf("After Insertion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}

84. A program to insert a new element after a specific element of an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x,k,loc;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the values:\n");
printf("New Element=");
scanf("%d", &x);
printf("Specific Element=");
scanf("%d", &k);
for(i=0;i<n;i++){
if(a[i]==k){
loc=i;
break;
}
}
for(i=n-1;i>=loc+1;i--){
a[i+1]=a[i];
}
a[loc+1]=x;
n=n+1;
printf("After Insertion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}
85. A program to insert a new element before a specific element of an array.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x,k,loc;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}

printf("Enter the values:\n");


printf("New Element =");
scanf("%d", &x);
printf("Specific Element=");
scanf("%d", &k);
for(i=0;i<n;i++){
if(a[i]==k){
loc=i;
break;
}
}
for(i=n-1;i>=loc;i--){
a[i+1]=a[i];
}
a[loc]=x;
n=n+1;
printf("After Insertion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}

86. A program to delete the first element of an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
for(i=0;i<n-1;i++){
a[i]=a[i+1];
}
n=n-1;
printf("After Deletion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}
87. A program to delete the last element of an array.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
n=n-1;
printf("After Deletion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}

88. A program to delete the element in Kth location in an array.


#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,k;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the position to delete:");
scanf("%d", &k);
for(i=k+1;i<n;i++){
a[i-1]=a[i];
}
n=n-1;
printf("After Deletion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}
89. A program to delete a specific element of an array.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,x,k;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter the specific element to delete=");
scanf("%d", &x);
for(i=0;i<n;i++){
if(a[i]==x){
k=i;
break;
}
}
for(i=k;i<=n-1;i++){
a[i]=a[i+1];
}
n=n-1;
printf("After Deletion:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}

90. A program to sort the elements of an array in ascending order (Bubble


Sort).
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[50],n,i,j,k;
printf("Enter the total number of elements:");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++) {
if(a[j]>a[j+1]) {
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
printf("After Sorted:\n");
for(i=0;i<n;i++){
printf("%5d", a[i]);
}
}
MATRIX
91. A program to add two matrices.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[10][10],b[10][10],c[10][10],i,j,row,col;
printf("Enter the size of the matrices:\n");
printf("Number of rows=");
scanf("%d", &row);
printf("Number of columns=");
scanf("%d", &col);
printf("Enter the elements of first matrix:\n");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for(i=0;i<row;i++) {
for(j=0;j<col;j++){
scanf("%d", &b[i][j]);
}
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
printf("RESULT:\n");
for(i=0;i<row;i++){
printf("|");
for(j=0;j<col;j++){
printf("%5d", c[i][j]);
}
printf(" |\n");
}
}
92. A program to subtract of two matrices.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[10][10],b[10][10],c[10][10],i,j,row,col;
printf("Enter the size of the matrices:\n");
printf("Number of rows=");
scanf("%d", &row);
printf("Number of columns=");
scanf("%d", &col);
printf("Enter the elements of first matrix:\n");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d", &b[i][j]);
}
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
c[i][j]=a[i][j]-b[i][j];
}
}
printf("RESULT:\n");
for(i=0;i<row;i++){
printf("|");
for(j=0;j<col;j++){
printf("%5d", c[i][j]);
}
printf(" |\n");
}
}
93. A program to calculate the sum of diagonal elements of a matrix.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[10][10],i,j,size,sum=0;
printf("Number of rows/columns of the matrix=");
scanf("%d", &size);
printf("Enter the elements of the matrix:\n");
for(i=0;i<size;i++){
for(j=0;j<size;j++){
scanf("%d", &a[i][j]);
}
}
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(i==j){
sum=sum+a[i][j];
}
}
}
printf("SUM=%d", sum);
}
94. A program to find the transpose of a matrix.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[10][10],transpose[10][10],row,col,i,j;
printf("Enter the size of the matrix:\n");
printf("Number of rows=");
scanf("%d", &row);
printf("Number of columns=");
scanf("%d", &col);

printf("Enter the elements of the matrix:\n");


for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d", &a[i][j]);
}
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
transpose[j][i]=a[i][j];
}
}
printf("Transpose Matrix:\n");
for(i=0;i<col;i++){
printf("|");
for(j=0;j<row;j++){
printf("%5d", transpose[i][j]);
}
printf(" |\n");
}
}
95. A program to multiply two matrices with size (M x N) and (N x P) .
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,sum;
printf("Enter the value of M, N & P:\n");
printf("M=");
scanf("%d", &m);
printf("N=");
scanf("%d", &n);
printf("P=");
scanf("%d", &p);
printf("Enter the elements of first matrix:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for(i=0;i<n;i++){
for(j=0;j<p;j++){
scanf("%d", &b[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<p;j++){
sum=0;
for(k=0;k<n;k++){
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
printf("RESULT:\n");
for(i=0;i<m;i++){
printf("|");
for(j=0;j<p;j++){
printf("%5d", c[i][j]);
}
printf(" |\n");
}
}
STRING
101. A program to find the length of a string without using strlen.[x=strlen(a)]
#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50];
int i;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++);
printf("Length=%d", i);
}

102. A program to copy a string without using strcpy.[strcpy(b,a)]


#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50],b[50];
int i;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++){
b[i]=a[i];
}
b[i]='\0';
printf("The second string:");
puts(b);
}

103. A program to concatenate two strings without using strcat.[strcat(a,b)]


#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50],b[50];
int i,j,k;
printf("Enter first string:");
gets(a);
printf("Enter second string:");
gets(b);
for(i=0;a[i]!='\0';i++);
for(j=0;b[j]!='\0';j++){
a[i]=b[j];
i++;
}
a[i]='\0';
printf("The final string:");
puts(a);
}

104. A program to compare two strings without using strcmp.[strcmp(a,b)]


#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50],b[50];
int i,c=0;
printf("Enter first string:");
gets(a);
printf("Enter second string:");
gets(b);
for(i=0;a[i]!='\0' || b[i]!='\0';i++){
if(a[i]!=b[i]){
c=1;
break;
}
}
if(c==0)
printf("Same");
else
printf("Not Same");
}
105. A program to reverse a string.
#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50],b[50];
int i,j;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++);
j=0;
for(i=i-1;i>=0;i--){
b[j]=a[i];
j++;
}
b[j]='\0';
printf("Enter reversed string:");
puts(b);
}
106. A program to count the number of lowercase & uppercase letters of a
string.
#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50];
int i,x=0,y=0;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++){
if(a[i]>=97 && a[i]<=122)
x++;
if(a[i]>=65 && a[i]<=90)
y++;
}
printf("Total Lowercase:%d", x);
printf("\nTotal Uppercase:%d", y);
}
107. A program to convert lowercase letter to uppercase letter in a string.
#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50];
int i;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++){
if(a[i]>=97 && a[i]<=122)
a[i]=a[i]-32;
}
printf("Uppercase:");
puts(a);
}
108. A program to convert uppercase letter to lowercase letter in a string.
#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50];
int i;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++){
if(a[i]>=65 && a[i]<=90)
a[i]=a[i]+32;
}
printf("Lowercase:");
puts(a);
}
109. A program to sort the letters of a string.
#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50],k;
int n,i,j;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++);
n=i;
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++){
if(a[j]>a[j+1]){
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}

printf("Sorted string:");
puts(a);
}
110. A program to swap two strings.
#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50],b[50],c[50];
int i;
printf("Enter first string:");
gets(a);
printf("Enter second string:");
gets(b);
for(i=0;a[i]!='\0';i++)
c[i]=a[i];
c[i]='\0';
for(i=0;b[i]!='\0';i++)
a[i]=b[i];
a[i]='\0';
for(i=0;c[i]!='\0';i++)
b[i]=c[i];
b[i]='\0';
printf("After Swapping:\nFirst String:");
puts(a);
printf("\bSecond String:");
puts(b);
}

111. A program to check whether a string is palindrome or not.


#include<stdio.h>
#include<stdlib.h>
void main(){
char a[50],b[50],c=0;
int i,j;
printf("Enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++);
j=0;
for(i=i-1;i>=0;i--){
b[j]=a[i];
j++;
}
b[j]='\0';
for(i=0;a[i]!='\0' || b[i]!='\0';i++){
if(a[i]!=b[i]){
c=1;
break;
}
}
if(c==0)
printf("Palindrome");
else
printf("Not Palindrome");
}

POINTER
112. A program to read variable in terms of pointer.
#include<stdio.h>
#include<stdlib.h>
void main(){
int x=5,*y;
y=&x;
printf("Value=%d", *y);
}
113. A program to read variable in terms of pointer of pointer.
#include<stdio.h>
#include<stdlib.h>
void main(){
int x=5,*y,**z;
y=&x;
z=&y;
printf("Value=%d", **z);
}
FUNCTION
114. A program to find the G.C.D. of two given numbers using function.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,result;
printf("Enter the two values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
result=gcd(a,b);
printf("G.C.D.=%d", result);
}

int gcd(int a,int b){


int i,small;
if(a<b)
small=a;
else
small=b;
for(i=small;i>=1;i--){
if(a%i==0 && b%i==0)
break;
}
return i;
}

115. A program to swap two numbers using pointer (Call by Reference).


#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b;
printf("Enter the values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
swap(&a,&b);
printf("After Swapping:\n");
printf("a=%d\nb=%d", a,b);
}

void swap(int *a,int *b){


*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
FUNCTION
114. A program to find the G.C.D. of two given numbers using function.
#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b,result;
printf("Enter the two values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
result=gcd(a,b);
printf("G.C.D.=%d", result);
}

int gcd(int a,int b){


int i,small;
if(a<b)
small=a;
else
small=b;
for(i=small;i>=1;i--){
if(a%i==0 && b%i==0)
break;
}
return i;
}

115. A program to swap two numbers using pointer (Call by Reference).


#include<stdio.h>
#include<stdlib.h>
void main(){
int a,b;
printf("Enter the values:\n");
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
swap(&a,&b);
printf("After Swapping:\n");
printf("a=%d\nb=%d", a,b);
}
void swap(int *a,int *b){
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}

STRUCTURE
119. A program to read and print roll, name and cgpa of 60 students using
structure.
#include<stdio.h>
#include<stdlib.h>

struct stu {
int roll;
char name[50];
float cgpa;
};

void main(){
struct stu a[60];
int i;
for(i=0;i<60;i++){
printf("Enter roll, name and cgpa for student no. %d:\n", i+1);
scanf("%d%s%f", &a[i].roll, &a[i].name, &a[i].cgpa);
}
for(i=0;i<60;i++)
printf("%d\t%s\t%.2f\n", a[i].roll, a[i].name, a[i].cgpa);
}
120. A program to sort the list of student according to CGPA using structure.
#include<stdio.h>
#include<stdlib.h>

struct stu {
int roll;
char name[50];
float cgpa;
};

void main(){
int n,i,j;
struct stu a[100],k;
printf("Enter the total number of students:");
scanf("%d", &n);
for(i=0;i<n;i++){
printf("Enter roll, name and cgpa for student no. %d:\n", i+1);
scanf("%d%s%f", &a[i].roll, &a[i].name, &a[i].cgpa);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++){
if(a[j].cgpa>a[j+1].cgpa){
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
printf("According to CGPA:\n");
for(i=0;i<n;i++)
printf("%d\t%s\t%.2f\n", a[i].roll, a[i].name, a[i].cgpa);
}

FILE
121. A program to open a file and write some text and close it.
#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp;
fp=fopen("[Link]","w");
printf("Khulna University of Engineering & Technology");
fprintf(fp, "Khulna University of Engineering & Technology");
fclose(fp);
}

122. A program to open a file and write 1 to 10 and close it.


#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp;
int i;
fp=fopen("[Link]/csv/doc","w");
for(i=1;i<=10;i++){
printf("%d\n", i);
fprintf(fp, "%d\n", i);
}
fclose(fp);
}
123. A program to open a file and write some text in append mode and close
it.
#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp;
fp=fopen("[Link]","a");
printf("Khulna University of Engineering & Technology");
fprintf(fp, "Khulna University of Engineering & Technology");
fclose(fp);
}

124. A program to open a file and read number and print it on output screen.
#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp;
int n;
fp=fopen("[Link]","r");
fscanf(fp, "%d", &n);
printf("%d", n);
fclose(fp);
}
125. A program to open a file and read array and print it on output screen.
#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp;
int i,n;
fp=fopen("[Link]","r");
for(i=1;i<=10;i++){
fscanf(fp, "%d", &n);
printf("%d\n", n);
}
fclose(fp);
}

126. A program to open a file and read the file and print it on output screen.
#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp;
char ch;
fp=fopen("[Link]","r");
if(fp==NULL)
printf("The file doesn't exist");
else {
while(ch!=EOF){
ch=fgetc(fp);
printf("%c", ch);
}
}
fclose(fp);
}

127. A program to read from a file and write to another file.


#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp1,*fp2;
int i,n;
fp1=fopen("[Link]","r");
fp2=fopen("[Link]","w");
for(i=1;i<=5;i++){
fscanf(fp1, "%d", &n);
fprintf(fp2, "%d\n", n);
}
fclose(fp1);
fclose(fp2);
}

You might also like