Programs
Programs
Programs
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;
void main()
{
int 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;
void main()
{
float a, b, sum;
printf("Enter two numbers : ");
scanf("%f%f",&a, &b);
sum=a+b;
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;
sum=m1+m2+m3;
avg=sum/3;
perc=(sum/150)*100;
void main()
{
float c, f;
void main()
{
int a,b,c,x,y;
y=(a*x*x)+(b*x)+c;
void main()
{
int p,n,r;
float si;
si = (p*n*r)/100.0;
void main()
{
int a, b, temp;
temp=a;
a=b;
b=temp;
void main()
{
float a,b,c;
int choice;
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;
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;
sum=m1+m2+m3;
avg=sum/3;
if(avg>80)
grade='A';
else if(avg>60 && avg<=80)
grade='B';
else if(avg>40 && avg<=60)
grade='C';
else
grade='F';
void main()
{
int num;
if(num%2==0)
printf("This is an even number");
else
printf("This is an odd number");
}
void main()
{
char ch;
void main()
{
char ch;
void main()
{
int 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);
}
void main()
{
int a, b, big,small;
if(a>b)
big=a;
else
big=b;
void main()
{
int a, b, c, big,small;
10. Find Largest of Two Numbers and Three Numbers using conditional operator:
#include<stdio.h>
void main()
{
int a, b, c, big;
big=(a>b)?a:b;
void main()
{
int a,c;
float b,d;
d=(b*b)-(4*a*c);
USING FUNCTIONS:
#include<stdio.h>
void main()
{
int a,c;
float b,d;
d=calcDet(a,b,c);
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("Factorial of %d is %d",n,f);
}
void main()
{
int a=0, b=1, sum, n,i;
printf("Enter the number of terms: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=a+b;
printf("%d ",sum);
a=b;
b=sum;
}
}
void main()
{
int n, digit;
void main()
{
int n, digit,sum=0;
void main()
{
int n, digit,sum=0,num;
if(num==sum)
printf("%d is an Armstrong number",num);
else
printf("%d is not an Armstrong number",num);
}
void main()
{
int n, digit,sum=0,num;
num=n;
while(n>0)
{
digit=n%10;
sum=(sum*10)+digit;
n=n/10;
}
void main()
{
int n, digit,sum=0,num;
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);
}
void main()
{
int n,limit,i;
printf("Enter a number : ");
scanf("%d",&n);
printf("\nTable of %d is \n",n);
void main()
{
int i, j;
void main()
{
int i, j;
void main()
{
int n, i, j, k=1;
printf("Enter the number of lines: ");
scanf("%d",&n);
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);
}
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;
avg=total/(float)n;
void main()
{
int max,min,a[50], n, 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);
}
void main()
{
int a[10], i, num, n, c=0;
void main()
{
int n, a[50], i, j, temp;
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;
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];
}
}
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,p,q,r;
void main()
{
int a[10][10],b[10][10];
int i,j,m,n;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
b[i][j]=a[j][i];
}
}
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);
}
void main()
{
int a[10][10];
int i,j,n,c=0;
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");
}