CS4208-Programming in C Laboratory 2022-2023
C Program Using Simple Statement and Expressions
EX.No 1a Calculate Area and Circumference of Circle
Aim:
To write a C program to calculate Area and Circumference of Circle.
Algorithm:
Step 1: Start
Step 2: Declare variables radius, area, circum in float type
Step 3: Read radius of circle as radius
Step 4: Calculate area using the expression: area = 3.14 * radius * radius
Step 5: Calculate circumference using the expression: circum=2*3.14*radius
Step 6: Print area and circum
Step 7: Stop
Program:
#include<stdio.h>
int main()
{
float radius, area,circum;
printf("\nEnter the radius of Circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circum=2*3.14*radius;
printf("\nArea of Circle : %.2f", area);
printf("\nCircumference of Circle : %.2f", circum);
return (0);
}
Output:
Enter the radius of Circle : 5
Area of Circle : 78.50
Circumference of Circle : 31.40
Result:
Thus, the C program to calculate Area and Circumference of Circle was written
executed and the output was verified successfully.
EX.No 1b Simple Interest Calculation
Aim:
To write a C program to calculate simple interest.
Algorithm:
Step 1: Start
Step 2: Declare variables for Principal amount, Rate of interest, time
Step 3: Read Principal amount, Rate of interest, time
Step 4: Calculate interest amount using the expression amt=(p*r*t)/100
Step 5: Print Simple interest as amt
Step 6: Stop
St.Joseph’s Institute of Technology Page 1 of 39
,CS4208-Programming in C Laboratory 2022-2023
Program:
#include<stdio.h>
int main()
{
int p,r,t,amt;
printf("Enter Principle amount, Rate of interest & time: \n");
scanf("%d%d%d",&p,&r,&t);
amt=(p*r*t)/100;
printf("Simple interest = %d",amt);
return 0;
}
Output:
Enter Principle amount, Rate of interest & time:
20000
6
2
Simple interest = 2400
Result:
Thus, the C program to calculate Simple Interest was written executed and the
output was verified successfully.
St.Joseph’s Institute of Technology Page 2 of 39
,CS4208-Programming in C Laboratory 2022-2023
Scientific Problem-Solving using Decision Making and Looping
Ex No 2a Largest of Three number
Aim:
To write a C program to find the largest of three numbers using if...else if.
Algorithm:
Step 1. Read the values of x, y and z.
Step 2. If x is greater than y and x is greater than z then print x is greatest, otherwise
go to step 3.
Step 3. If y is greater than z then print y is greatest, otherwise go to step 4.
Step 4. display z is greatest.
Program:
#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter the values for x,y and z \n");
scanf("%d%d%d",&x,&y,&z);
if((x>y)&& (x>z))
printf(" %d is greatest",x);
else if (y>z)
printf ("%d is greatest",y);
else
printf("%d is greatest",z);
return 0;
}
Output:
Run1:
Enter the values for x, y and z
25
46
22
46 is greatest
Run2:
Enter the values for x, y and z
75
46
22
75 is greatest
St.Joseph’s Institute of Technology Page 3 of 39
, CS4208-Programming in C Laboratory 2022-2023
Ex NO 2b Leap Year or Not
Aim:
To write a C program to check whether the given year is leap year or not.
Algorithm:
Step 1. Get the input year from the user to check for leap year.
Step 2. If the year is evenly divisible by 4, go to step 3. Otherwise, go to step 6.
Step 3. If the year is evenly divisible by 100, go to step 4. Otherwise, go to step 5.
Step 4. If the year is evenly divisible by 400, go to step 5. Otherwise, go to step 6.
Step 5. The year is a leap year (it has 366 days).
Step 6. The year is not a leap year (it has 365 days).
Program:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
Output:
Run1:
Enter a year: 1900
1900 is not a leap year.
Run2:
Enter a year: 2004
2004 is a leap year.
St.Joseph’s Institute of Technology Page 4 of 39
C Program Using Simple Statement and Expressions
EX.No 1a Calculate Area and Circumference of Circle
Aim:
To write a C program to calculate Area and Circumference of Circle.
Algorithm:
Step 1: Start
Step 2: Declare variables radius, area, circum in float type
Step 3: Read radius of circle as radius
Step 4: Calculate area using the expression: area = 3.14 * radius * radius
Step 5: Calculate circumference using the expression: circum=2*3.14*radius
Step 6: Print area and circum
Step 7: Stop
Program:
#include<stdio.h>
int main()
{
float radius, area,circum;
printf("\nEnter the radius of Circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circum=2*3.14*radius;
printf("\nArea of Circle : %.2f", area);
printf("\nCircumference of Circle : %.2f", circum);
return (0);
}
Output:
Enter the radius of Circle : 5
Area of Circle : 78.50
Circumference of Circle : 31.40
Result:
Thus, the C program to calculate Area and Circumference of Circle was written
executed and the output was verified successfully.
EX.No 1b Simple Interest Calculation
Aim:
To write a C program to calculate simple interest.
Algorithm:
Step 1: Start
Step 2: Declare variables for Principal amount, Rate of interest, time
Step 3: Read Principal amount, Rate of interest, time
Step 4: Calculate interest amount using the expression amt=(p*r*t)/100
Step 5: Print Simple interest as amt
Step 6: Stop
St.Joseph’s Institute of Technology Page 1 of 39
,CS4208-Programming in C Laboratory 2022-2023
Program:
#include<stdio.h>
int main()
{
int p,r,t,amt;
printf("Enter Principle amount, Rate of interest & time: \n");
scanf("%d%d%d",&p,&r,&t);
amt=(p*r*t)/100;
printf("Simple interest = %d",amt);
return 0;
}
Output:
Enter Principle amount, Rate of interest & time:
20000
6
2
Simple interest = 2400
Result:
Thus, the C program to calculate Simple Interest was written executed and the
output was verified successfully.
St.Joseph’s Institute of Technology Page 2 of 39
,CS4208-Programming in C Laboratory 2022-2023
Scientific Problem-Solving using Decision Making and Looping
Ex No 2a Largest of Three number
Aim:
To write a C program to find the largest of three numbers using if...else if.
Algorithm:
Step 1. Read the values of x, y and z.
Step 2. If x is greater than y and x is greater than z then print x is greatest, otherwise
go to step 3.
Step 3. If y is greater than z then print y is greatest, otherwise go to step 4.
Step 4. display z is greatest.
Program:
#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter the values for x,y and z \n");
scanf("%d%d%d",&x,&y,&z);
if((x>y)&& (x>z))
printf(" %d is greatest",x);
else if (y>z)
printf ("%d is greatest",y);
else
printf("%d is greatest",z);
return 0;
}
Output:
Run1:
Enter the values for x, y and z
25
46
22
46 is greatest
Run2:
Enter the values for x, y and z
75
46
22
75 is greatest
St.Joseph’s Institute of Technology Page 3 of 39
, CS4208-Programming in C Laboratory 2022-2023
Ex NO 2b Leap Year or Not
Aim:
To write a C program to check whether the given year is leap year or not.
Algorithm:
Step 1. Get the input year from the user to check for leap year.
Step 2. If the year is evenly divisible by 4, go to step 3. Otherwise, go to step 6.
Step 3. If the year is evenly divisible by 100, go to step 4. Otherwise, go to step 5.
Step 4. If the year is evenly divisible by 400, go to step 5. Otherwise, go to step 6.
Step 5. The year is a leap year (it has 366 days).
Step 6. The year is not a leap year (it has 365 days).
Program:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
Output:
Run1:
Enter a year: 1900
1900 is not a leap year.
Run2:
Enter a year: 2004
2004 is a leap year.
St.Joseph’s Institute of Technology Page 4 of 39