Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Variables, Datatypes, Operators, Expressions and IO Formatting

1. C Program to Display a message:


#include<stdio.h>

void main()
{
printf("Hello World!!!");
}

2. C Program to Input a character from User and display its ASCII value:
#include<stdio.h>

void main()
{
char ch;

printf("Enter a character : ");


scanf("%c",&ch);

printf("The ASCII value of %c is %d",ch,ch);


}

3. C Program to Print an Integer:


#include<stdio.h>

void main()
{
int num;

printf("Enter a number : ");


scanf("%d",&num);

printf("You entered %d",num);


}

4. C Program to Interactively ask for name and age from the user i.e. user interaction and respond with: Hello
name, next year you will be next_age where next_age is age+1:
#include<stdio.h>

void main()
{
char name[20];
int age;

printf("Enter your name: ");


scanf("%s",name);
printf("Enter your age: ");
scanf("%d",&age);

printf("Hello %s, next year you will be %d",name,age+1);


}

5. C Program to Add Two Numbers:


#include<stdio.h>

void main()
{
float a, b, sum;
printf("Enter two numbers : ");
scanf("%f%f",&a, &b);

sum=a+b;

printf("Sum of the two numbers is %0.2f",sum);


}

6. C Program to Input marks of a student, Calculate Total, Average and Percentage Marks:
#include<stdio.h>

void main()
{
int m1,m2,m3;
float sum,avg,perc;

printf("Enter marks in 3 subjects(out of 50) : ");


scanf("%d%d%d",&m1,&m2,&m3);

sum=m1+m2+m3;
avg=sum/3;
perc=(sum/150)*100;

printf("Total = %0.0f\nAverage = %0.2f\nPercentage = %0.2f",sum,avg,perc);


}

7. C Program to Convert Centigrade to Fahrenheit:


#include<stdio.h>

void main()
{
float c, f;

printf("Enter temperature in Celsius : ");


scanf("%f",&c);

f=(c * 9/5) + 32;

printf("\nTemperature in Fahrenheit = %f",f);


}

8. C Program to Evaluate the expression y=ax2+bx+c:


#include<stdio.h>

void main()
{
int a,b,c,x,y;

printf("Enter the values of a b c and x : ");


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

y=(a*x*x)+(b*x)+c;

printf("\nValue of y = %d", y);


}

9. C Program to Calculate the simple interest:


#include<stdio.h>

void main()
{
int p,n,r;
float si;

printf("Enter Principal Amount, No. of years, Rate of Interest: ");


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

si = (p*n*r)/100.0;

printf("\nSimple Interest : %.2f\n\n", si);


}

10. C Program to Interchange or Swap two Numbers:


#include<stdio.h>

void main()
{
int a, b, temp;

printf("Enter value of A and B : \n");


printf("A = ");
scanf("%d",&a);
printf("B = ");
scanf("%d",&b);

temp=a;
a=b;
b=temp;

printf("\nA = %d\nB = %d",a,b);


}

Decision making/Branching control statements


1. Perform Addition, Subtraction, Multiplication and Division of two floating-point numbers based on user’s
choice(Calculator):
#include<stdio.h>

void main()
{
float a,b,c;
int choice;

printf("Enter 2 numbers: ");


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

printf("\nMENU:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n");
printf("Enter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: c=a+b; printf("Sum of %.2f and %.2f is %.2f",a,b,c); break;
case 2: c=a-b; printf("Difference of %.2f and %.2f is %.2f",a,b,c); break;
case 3: c=a*b; printf("Product of %.2f and %.2f is %.2f",a,b,c); break;
case 4: c=a/b; printf("Quotient of %.2f and %.2f is %.2f",a,b,c); break;
default: printf("Invalid choice...Enter valid choice 1 to 4"); break;
}
}

2. Calculate the area of a 1)circle 2)triangle 3)cylinder 4)sphere 5)rectangle 6)square based on user’s choice:
#include <stdio.h>
void main()
{
int ch;
float side, base, length, breadth, height, area, radius;

printf("Enter the choice : ");


scanf("%d", &ch);

switch(ch)
{
case 1:
printf("Enter the radius : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
printf("Area of Circle = %.2f", area);
break;
case 2:
printf("Enter the base and height : ");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of Triangle = %.2f", area);
break;
case 3:
printf("Enter the radius and height: ");
scanf("%f %f", &radius,&height);
area = 2*3.14*radius*(radius+height);
printf("Area of Cylinder = %.2f", area);
break;
case 4:
printf("Enter the radius : ");
scanf("%f", &radius);
area = 4* 3.14 * radius * radius;
printf("Area of Sphere = %.2f", area);
break;
case 5:
printf("Enter the breadth and length : ");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of Rectangle = %.2f", area);
break;
case 6:
printf("Enter the side : ");
scanf("%f", &side);
area = side * side;
printf("Area of Square=%.2f", area);
break;
default:
printf("Invalid entry. Enter 1-6 \n");
break;
}
}
3. Calculate Grade of Student:
#include<stdio.h>

void main()
{
int m1,m2,m3;
float sum,avg;
char grade;

printf("Enter marks in 3 subjects : ");


scanf("%d%d%d",&m1,&m2,&m3);

sum=m1+m2+m3;
avg=sum/3;

printf("Total = %0.0f\nAverage = %0.2f\n",sum,avg);

if(avg>80)
grade='A';
else if(avg>60 && avg<=80)
grade='B';
else if(avg>40 && avg<=60)
grade='C';
else
grade='F';

printf("Your Grade is %c",grade);


}

4. Check whether an input number is Even or Odd:


#include<stdio.h>

void main()
{
int num;

printf("Enter a number : ");


scanf("%d",&num);

if(num%2==0)
printf("This is an even number");
else
printf("This is an odd number");
}

5. Check whether an input character is an Alphabet or a digit:


#include<stdio.h>

void main()
{
char ch;

printf("Enter a character: ");


scanf("%c",&ch);

if((ch>='a'&& ch<='z') || (ch>='A' && ch<='Z'))


printf("%c is an alphabet",ch);
else if(ch>='0'&& ch<='9')
printf("%c is a digit",ch);
else
printf("%c is not an alphabet and a digit",ch);
}
6. Check whether an input character is Vowel or Consonant:
#include<stdio.h>

void main()
{
char ch;

printf("Enter an alphabet : ");


scanf("%c",&ch);

if(ch=='a' || ch=='A' || ch=='e' || ch=='E' ||


ch=='i' || ch=='I' || ch=='o' || ch=='O' ||
ch=='u' || ch=='U')
{
printf("This is a vowel");
}
else
{
printf("This is a consonant");
}
}

7. Check whether an input is Leap Year or Not:


#include <stdio.h>

void main()
{
int year;

printf("Enter a year : ");


scanf("%d",&year);

if(year%4 == 0)
{
if (year%100 == 0 && year%400 == 0)
printf("%d is a leap year", year);
else if (year%100 != 0)
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
}
else
printf("%d is not a leap year", year);
}

8. Find Largest and Smallest of Two Numbers:


#include<stdio.h>

void main()
{
int a, b, big,small;

printf("Enter two numbers : ");


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

if(a>b)
big=a;
else
big=b;

printf("Biggest of the two numbers is %d",big);


if(a<b)
small=a;
else
small=b;

printf("Smallest of the two numbers is %d",small);


}

9. Find Largest and Smallest of Three Numbers:


#include<stdio.h>

void main()
{
int a, b, c, big,small;

printf("Enter three numbers : ");


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

if(a>b && a>c)


big=a;
else if(b>c)
big=b;
else
big=c;

printf("Biggest of the three numbers is %d",big);

if(a<b && a<c)


small=a;
else if(b<c)
small=b;
else
small=c;

printf("Smallest of the three numbers is %d",small);


}

10. Find Largest of Two Numbers and Three Numbers using conditional operator:
#include<stdio.h>

void main()
{
int a, b, c, big;

printf("Enter two numbers : ");


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

big=(a>b)?a:b;

printf("Biggest of the two numbers is %d",big);

printf("Enter three numbers : ");


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

big=(a>b && a>c)?a:(b>c)?b:c;

printf("Biggest of the three numbers is %d",big);


}
Find the determinant d=b2-4ac : (Assume a and c are integers ; b and d are floating point numbers)
#include<stdio.h>

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

printf("Enter the value of a b c : ");


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

d=(b*b)-(4*a*c);

printf("Determinant value = %0.2f",d);


}

USING FUNCTIONS:
#include<stdio.h>

float calcDet(int aval,float bval,int cval);

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

printf("Enter the value of a b c : ");


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

d=calcDet(a,b,c);

printf("Determinant value = %0.2f",d);


}

float calcDet(int aval,float bval,int cval)


{
float det;

det=(bval*bval)-(4*aval*cval);

return det;
}

USING POINTERS:
#include<stdio.h>

void main()
{
int a,c;
float b,d;
int *pa=&a,*pc=&c;
float *pb=&b,*pd=&d;

printf("Enter the value of a b c : ");


scanf("%d %f %d",pa,pb,pc);

*pd=(*pb * *pb)-(4* *pa * *pc);

printf("Determinant value = %0.2f",*pd);


}
Looping control statements
1. Sum and arithmetic mean i.e. average of 1st n natural numbers:
#include<stdio.h>
void main()
{
int i,s=0,n;

printf("Enter a number : ");


scanf("%d",&n);

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


{
s=s+i;
}

printf("Sum of first %d natural numbers is %d\n",n,s);


printf("Average of first %d natural numbers is %.2f\n",n,s/(float)n);
}

2. Sum of squares of first n odd natural numbers:


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

printf("Enter a number : ");


scanf("%d",&n);

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


{
if(i%2==1)
s=s+(i*i);
}

printf("Sum of squares of first %d odd natural numbers is %d\n",n,s);


}

3. Find the factorial of a number:


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

printf("Enter a number : ");


scanf("%d",&n);

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


{
f=f*i;
}

printf("Factorial of %d is %d",n,f);
}

4. Print the first n terms of the Fibonacci series:


#include<stdio.h>

void main()
{
int a=0, b=1, sum, n,i;
printf("Enter the number of terms: ");
scanf("%d",&n);

printf("Fibonacci Series : %d %d ",a, b); // first two term


n=n-2;

for(i=0;i<n;i++)
{
sum=a+b;
printf("%d ",sum);
a=b;
b=sum;
}
}

5. Print the digits of a number in different lines:


#include<stdio.h>

void main()
{
int n, digit;

printf("Enter a Number :");


scanf("%d",&n);

printf("The digits of %d are:\n",n);


while(n>0)
{
digit=n%10;
printf("%d\n",digit);
n=n/10;
}
}

6. Add Digits of Number:


#include<stdio.h>

void main()
{
int n, digit,sum=0;

printf("Enter a Number :");


scanf("%d",&n);

printf("The sum of the digits of %d is: ",n);


while(n>0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
printf("%d",sum);
}

7. Check whether a number is an Armstrong number or not:


#include<stdio.h>

void main()
{
int n, digit,sum=0,num;

printf("Enter a Number :");


scanf("%d",&n);
num=n;
while(n>0)
{
digit=n%10;
sum=sum+(digit*digit*digit);
n=n/10;
}

if(num==sum)
printf("%d is an Armstrong number",num);
else
printf("%d is not an Armstrong number",num);
}

8. Find the reverse of a number:


#include<stdio.h>

void main()
{
int n, digit,sum=0,num;

printf("Enter a Number :");


scanf("%d",&n);

num=n;
while(n>0)
{
digit=n%10;
sum=(sum*10)+digit;
n=n/10;
}

printf("The reverse of %d is %d\n",num,sum);


}

9. Check whether a number is a palindrome or not:


#include<stdio.h>

void main()
{
int n, digit,sum=0,num;

printf("Enter a Number :");


scanf("%d",&n);

num=n;
while(n>0)
{
digit=n%10;
sum=(sum*10)+digit;
n=n/10;
}

if(num==sum)
printf("%d is a palindrome",num);
else
printf("%d is not a palindrome",num);
}

10. Print multiplication Table of Number:


#include<stdio.h>

void main()
{
int n,limit,i;
printf("Enter a number : ");
scanf("%d",&n);

printf("Enter the limit : ");


scanf("%d",&limit);

printf("\nTable of %d is \n",n);

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


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

11. Print the pattern 1


1 2
123
1234
12345
#include<stdio.h>

void main()
{
int i, j;

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


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

12. Print the pattern *


* *
***
****
*****
#include<stdio.h>

void main()
{
int i, j;

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


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

13. Print the Floyd’s triangle:


#include<stdio.h>

void main()
{
int n, i, j, k=1;
printf("Enter the number of lines: ");
scanf("%d",&n);

printf("\nFloyd's Triangle :\n");


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

14. Compute the GCD i.e. HCF and LCM of 2 numbers:


#include<stdio.h>

void main()
{
int a, b,x,y,temp, gcd, lcm;
printf("Enter two numbers : ");
scanf("%d%d",&a, &b);

x=a;
y=b;
while(y!=0)
{
temp=y;
y=x%y;
x=temp;
}

gcd=x;
lcm=(a*b)/gcd;
printf("GCD = %d\n",gcd);
printf("LCM = %d",lcm);
}

15. Check whether a number is Prime or Not:


#include<stdio.h>

void main()
{
int num,i,count=0;

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

for(i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
}

if(count==0)
printf("This is a prime number");
else
printf("This is not a prime number");
}
1-D arrays
1. Input n numbers (marks), display the elements of the array in reverse order and also calculate and display
the sum and arithmetic mean i.e. average of the marks:
#include<stdio.h>

void main()
{
int m[50],n,i,total=0;
float avg;

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


scanf("%d",&n);

printf("Enter marks obtained in the subjects :\n");


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

avg=total/(float)n;

printf("\n\nThe marks are :\n");


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

printf("\n\nThe reverse of the marks are :\n");


for(i=n-1;i>=0;i--)
{
printf("%d\t",m[i]);
}
printf("\n\nTotal = %d\nAverage Marks = %.2f\n\n",total,avg);
}

2. Count Occurrence of Positive, Zero and Negative Numbers:


#include<stdio.h>
void main()
{
int p=0, n=0, z=0, arr[10], i,num;
printf("Enter the number of elements : ");
scanf("%d",&num);

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


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

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


{
if(arr[i]>0)
p++;
else if(arr[i]==0)
z++;
else
n++;
}

printf("Positive Numbers count = %d\n",p);


printf("Negative Numbers count = %d\n",n);
printf("Zero count = %d\n",z);
}
3. Find the largest and smallest element of the array:
#include<stdio.h>

void main()
{
int max,min,a[50], n, i;

printf("Enter number of elements in the array : ");


scanf("%d",&n);

printf("Enter array elements :\n");


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

max=a[0];
for(i=1; i<n; i++)
{
if(a[i]>max)
{
max=a[i];
}
}
printf("\nLargest Number = %d\n",max);

min=a[0];
for(i=1; i<n; i++)
{
if(a[i]<min)
{
min=a[i];
}
}
printf("\nSmallest Number = %d\n",min);
}

4. Search whether an element is present in the array or not (linear search):


#include<stdio.h>

void main()
{
int a[10], i, num, n, c=0;

printf("Enter the number of elements in the array : ");


scanf("%d",&n);

printf("Enter the array elements :\n");


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

printf("Enter the number to be searched : ");


scanf("%d",&num);

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


{
if(a[i]==num)
{
c=1;
break;
}
}
if(c==0)
{
printf("Number not found..!!\n");
}
else
{
printf("%d found at position %d\n",num, i+1);
}
}

5. Sort the array in ascending and descending order:


#include<stdio.h>

void main()
{
int n, a[50], i, j, temp;

printf("Enter number of elements in array : ");


scanf("%d",&n);

printf("Enter Array Elements :\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;
}
}
}

printf("Sorted array in ascending order is :\n");


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

2-D arrays
1. Add and subtract two m*n matrices:
#include<stdio.h>

void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10];
int i,j,m,n;

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


scanf("%d",&m);
printf("Enter the number of columns : ");
scanf("%d",&n);
printf("Enter the 1st matrix :\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("Enter the 2nd matrix :\n");


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

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}

printf("The sum of the 2 matrices is :\n");


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

printf("The difference of the 2 matrices is :\n");


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

2. Multiply an m*n with an n*p matrix:


#include<stdio.h>

void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,p,q,r;

printf("Enter the number of rows of 1st matrix : ");


scanf("%d",&p);
printf("Enter the number of columns of 1st = rows of 2nd matrix : ");
scanf("%d",&q);
printf("Enter the number of columns of 2nd matrix : ");
scanf("%d",&r);

printf("Enter the 1st matrix :\n");


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

printf("Enter the 2nd matrix:\n");


for(i=0;i<q;i++)
for(j=0;j<r;j++)
scanf("%d",&b[i][j]);
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
c[i][j]=0;
for(k=0;k<q;k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}

printf("The product of the 2 matrices is :\n");


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

3. Transpose an m*n matrix:


#include<stdio.h>

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

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


scanf("%d",&m);
printf("Enter the number of columns : ");
scanf("%d",&n);

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


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

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
b[i][j]=a[j][i];
}
}

printf("The transpose of the matrix is :\n");


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

4. Calculate the sum of the diagonal elements of an n*n matrix:


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

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


scanf("%d",&n);

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


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

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum = sum + a[i][j];
}
}
printf("The sum is %d\n",sum);
}

5. Check whether an n*n matrix is an identity matrix or not:


#include<stdio.h>

void main()
{
int a[10][10];
int i,j,n,c=0;

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


scanf("%d",&n);

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


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

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j && a[i][j]!=1)
{
c=1;
break;
}
else if(i!=j && a[i][j]!=0)
{
c=1;
break;
}
}
}

if(c==1)
printf("Matrix is not an identity matrix");
else
printf("Matrix is an identity matrix");
}

You might also like