LECTURE - 04
Write a program that calculates the area and the perimeter of a rectangle
where the length and the width of the rectangle are provided by the user as
inputs.
#include<stdio.h>
int main()
{
float length,width,area,perimeter;
printf("Input length of the Rectangle:");
scanf("%f",&length);
printf("Input width of the Rectangle:");
scanf("%f",&width);
area=length*width;
perimeter=2*(length+width);
printf("The Area of Rectangle:%f",area);
printf("\nThe Perimeter of Rectangle:%f",perimeter);
return 0;
}
Write a program that calculates the area of a triangle where the base and
height of the triangle are provided by the user as inputs.
#include<stdio.h>
int main()
{
float base,height,area,perimeter;
printf("Input base of the Triangle:");
scanf("%f",&base);
printf("Input height of the Triangle:");
scanf("%f",&height);
area=0.5*(base*height);
printf("The Area of Triangle:%f",area);
return 0;
}
Write a program that calculates the area of a circle and the perimeter of a
circle where the radius of the circle is provided by the user as input.
#include<stdio.h>
int main()
{
float radius,area,perimeter;
float const PI=3.1416;
printf("Input radius of the Circle:");
scanf("%f",&radius);
area=PI*radius*radius;
perimeter=2*PI*radius;
printf("The Area of Circle:%f",area);
printf("\nThe Perimeter of Circle:%f",perimeter);
return 0;
}
Write a program that calculates the volume and surface area of a flat washer
where the height, outer diameter (d1) and the inner diameter (d2) are
provided by the user as inputs.
#include <stdio.h>
#include <math.h>
int main()
{
float d1, d2, height, radius1, radius2, volume, surface_area;
float const PI = 3.1416;
printf("Enter the outer diameter: ");
scanf("%f", &d1);
printf("Enter the inner diameter: ");
scanf("%f", &d2);
printf("Enter the height: ");
scanf("%f", &height);
radius1 = d1 / 2;
radius2 = d2 / 2;
volume = PI * height * (radius1 * radius1 - radius2 * radius2);
surface_area = 2 * PI * (radius1 + radius2) * height + PI * (radius1 * radius1 -
radius2 * radius2);
printf("\nVolume: %.2f", volume);
printf("\nSurface Area: %.2f", surface_area);
return 0;
}
Write a program that accepts a character(small letter) as input from the user
and (a) converts it into uppercase letter and (b) shows it’s binary equivalent.
#include<stdio.h>
#include<math.h>
int main()
{
char ch,large;
printf("Input a Lowercase Character:");
scanf("%c",&ch);
large=ch-32;
printf("Uppercase Character:%c",large);
printf("\nBinary Equivalent of %d: ", ch);
int value = ch;
int divisor = 128;
while (divisor > 0)
{
printf("%d", (value / divisor) % 2);
divisor /= 2;
}
printf("\n");
return 0;
}
LECTURE-5
Write a C program that prompts the user to input three integer values and
find the greatest and smallest of those three values.
#include <stdio.h>
int main()
{
int a,b,c;
printf("Input a:");
scanf("%d",&a);
printf("Input b:");
scanf("%d",&b);
printf("Input c:");
scanf("%d",&c);
if(a>b && a>c)
printf("%d is Greatest",a);
else if(b>c && b>a)
printf("%d is Greatest",b);
else if(c>a && c>b)
printf("%d is Greatest",c);
if(a<b && a<c)
printf("\n%d is Smallest",a);
else if(b<c && b<a)
printf("\n%d is Smallest",b);
else if(c<a && c<b)
printf("\n%d is Smallest",c);
return 0;
}
Write a program that determines a student’s grade. The program
will read three scores and determine the grade.
#include <stdio.h>
int main()
{
float num1,num2,num3;
printf("Input three marks:");
scanf("%f%f%f",&num1,&num2,&num3);
float avg=(num1+num2+num3)/3;
printf("The Average:%f\n",avg);
if(avg>=90 && avg<=100)
printf("Grade = A");
else if(avg>=70 && avg<90)
printf("Grade = B");
else if(avg>=50 && avg<70)
printf("Grade = C");
else if(avg<50 && avg <=0)
printf("Grade = F");
return 0;
}
Calculate Tax
#include <stdio.h>
int main()
{
float salary;
printf("Enter your Salary:");
scanf("%f",&salary);
if(salary>=0 && salary<15000)
printf("Your Tax amount is:%.2f\n",salary*0.15);
else if(salary>=15000 && salary<30000)
printf("Your Tax amount is:%.2f\n",2250+(salary-15000)*0.18);
else if(salary>=30000 && salary<50000)
printf("Your Tax amount is:%.2f\n",5400+(salary-30000)*0.22);
else if(salary>=50000 && salary<80000)
printf("Your Tax amount is:%.2f\n",11000+(salary-50000)*0.27);
else if(salary>=80000 && salary<=150000)
printf("Your Tax amount is:%.2f\n",21600+(salary-80000)*0.33);
else
printf("Your Input is Invalid");
return 0;
}
Determine if a year (provided as input) is a Leap-year or not.
#include <stdio.h>
int main()
{
int year;
printf("Enter Year:");
scanf("%d",&year);
if((year%400==0)||(year%4==0 && year %100!=0))
{
printf("%d is a Leap Year",year);
}
else
{
printf("%d is Not a Leap Year",year);
}
return 0;
}
Write a program to compute the real roots of a quadratic equation of the
form ax2+bx+c=0, a≠0.
#include <stdio.h>
#include<math.h>
int main()
{
float a,b,c,dis,root1,root2;
printf("Input three numbers a,b,c:");
scanf("%f%f%f",&a,&b,&c);
dis=pow(b,2)-4*a*c;
root1=(-b+sqrt(dis))/2*a;
root2=(-b-sqrt(dis))/2*a;
if(dis>0)
{
printf("The two real roots are:%.3f and %.3f",root1,root2);
}
else if(dis==0)
{
printf("There is only one root and the root is:%.3f",root1);
}
else
{
printf("No real roots exist");
}
return 0;
}
LECTURE-7
1. Calculate the sum of the following series, where n is provided as user
input 1+2+3+4+5+…….+ n
#include<stdio.h>
void main()
{
int i,num,sum=0;
printf("Input n:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum=sum+i;
}
printf("The sum:%d",sum);
}
2. Write a program that calculates the factorial of a positive integer n
provided as user input.
#include<stdio.h>
void main()
{
int i,num,fact=1,sum=0;
printf("Input n:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
fact*=i;
sum=sum+fact;
}
printf("Factorial of %d is:%d",num,fact);
}
3. Write a program which calculates a^x, a and x are user inputs.
#include <stdio.h>
#include <math.h>
int main()
{
int a,x,result;
printf("Enter the base: ");
scanf("%d", &a);
printf("Enter the exponent: ");
scanf("%d", &x);
result = pow(a, x);
printf("%d to the power of %d = %d\n", a, x, result);
return 0;
}
4. Calculate the sum of Series.
#include <stdio.h>
#include <math.h>
void main() {
float n,x,sum=1,term=1;
printf("Enter n: ");
scanf("%f",&n);
printf("Enter x: ");
scanf("%f",&x);
for (int i=1;i<=n;i++) {
term*= x/i;
sum+=term;
}
printf("The Sum:%.3f",sum);
}
#include <stdio.h>
void main() {
int row,column,n;
printf("Input size of Pattern:");
scanf("%d",&n);
for(row=n;row>=1;row--) {
for(column=1;column<=row;column++) {
printf("*");
}
printf("\n");
}
}
#include<stdio.h>
int main()
{
int row,column,N;
printf("Input the size of Pattern:");
scanf("%d",&N);
for(row=1; row<=N; row++) {
for(column=1; column<=N-row; column++) {
printf(" ");
}
for(column=1; column<=2*row-1; column++) {
printf("*");
}
printf("\n");
}
}
#include<stdio.h>
int main()
{
int row,column,N;
printf("Input the size of Pattern:");
scanf("%d",&N);
for(row=N-1; row>=1; row--)
{
for(column=1; column<=N-row; column++)
{
printf(" ");
}
for(column=1; column<=2*row-1; column++)
{
printf("*");
}
printf("\n");
}
}
#include <stdio.h>
int main()
{
int row, column, N;
printf("Input the size of Pattern: ");
scanf("%d", &N);
for (row = 1; row <= N; row++) {
for (column = 1; column <= N - row; column++) {
printf("*");
}
for (column = 1; column <= 2 * row - 1; column++) {
if (column == 1 || column == 2 * row - 1) {
printf("*");
} else {
printf(" ");
}
}
for (column = 1; column <= N - row; column++) {
printf("*");
}
printf("\n");
}
for (row = N - 1; row >= 1; row--) {
for (column = 1; column <= N - row; column++) {
printf("*");
}
for (column = 1; column <= 2 * row - 1; column++) {
if (column == 1 || column == 2 * row - 1) {
printf("*");
} else {
printf(" ");
}
}
for (column = 1; column <= N - row; column++) {
printf("*");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int row, column,N;
printf("Input the size of Pattern: ");
scanf("%d",&N);
for(row=1; row<=N; row++) {
for(column=1;column<=N-row;column++) {
printf(" ");
}
for(column=1;column<=2*row-1;column++) {
if(column==1 || column==2*row-1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
for(row=N-1; row>=1; row--) {
for(column=1; column<=N-row; column++) {
printf(" ");
}
for(column=1; column<=2*row-1; column++) {
if(column==1 || column==2*row-1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int row,column,N,space,num=1;
printf("Input N:");
scanf("%d",&N);
space=N-1;
for(row=1; row<=N; row++)
{
for(column=1; column<=row; column++)
{
printf("*");
}
printf("\n");
}
for(row = N; row >= 1; row--)
{
for(column = 1; column <= row-1; column++)
{
printf("*");
}
printf("\n");
}