(Grade XII) Part 2: Structural Program
A.
Sequential Programs
1.
Write a C program to input the length and breadth of a rectangle and calculate
its area and perimeter.
#include <stdio.h>
int main()
{
float l, b, area,
perimeter;
printf("Enter
length and breadth: ");
scanf("%f%f", &l, &b);
area = l * b;
perimeter = 2 * (l
+ b);
printf("Area
= %.2f\n", area);
printf("Perimeter = %.2f", perimeter);
return 0;
}
2. Write a C program to input the
radius of a circle and calculate its area and circumference.
#include <stdio.h>
int main()
{
float r, area,
circumference;
printf("Enter
radius: ");
scanf("%f", &r);
area = 3.14 * r *
r;
circumference = 2
* 3.14 * r;
printf("Area
= %.2f\n", area);
printf("Circumference = %.2f", circumference);
return 0;
}
3.Write a C program to input
principal, rate and time and calculate simple interest.
#include <stdio.h>
int main()
{
float p, r, t, si;
printf("Enter
principal, rate and time: ");
scanf("%f%f%f", &p, &r, &t);
si = (p * r * t) /
100;
printf("Simple Interest = %.2f", si);
return 0;
}
4. Write a C program to input
temperature in Celsius and convert it into Fahrenheit.
#include <stdio.h>
int main()
{
float c, f;
printf("Enter
temperature in Celsius: ");
scanf("%f", &c);
f = (c * 9 / 5) +
32;
printf("Temperature in Fahrenheit = %.2f", f);
return 0;
}
5. Write a C program to input marks
of three subjects and calculate total and average marks.
#include <stdio.h>
int main()
{
float a, b, c,
total, average;
printf("Enter
marks of three subjects: ");
scanf("%f%f%f", &a, &b, &c);
total = a + b + c;
average = total /
3;
printf("Total
= %.2f\n", total);
printf("Average = %.2f", average);
return 0;
}
B. Conditional Programs
6. Write a C program to input a
number and check whether it is positive, negative or zero.
#include <stdio.h>
int main()
{
int n;
printf("Enter
a number: ");
scanf("%d", &n);
if(n > 0)
printf("Positive number");
else if(n < 0)
printf("Negative number");
else
printf("Zero");
return 0;
}
7. Write a C program to input a
number and check whether it is even or odd.
#include <stdio.h>
int main()
{
int n;
printf("Enter
a number: ");
scanf("%d", &n);
if(n % 2 == 0)
printf("Even number");
else
printf("Odd number");
return 0;
}
8. Write a C program to input three
numbers and find the greatest number.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter
three numbers: ");
scanf("%d%d%d",
&a, &b, &c);
if(a > b
&& a > c)
printf("%d is greatest", a);
else if(b > a
&& b > c)
printf("%d is greatest", b);
else
printf("%d is greatest", c);
return 0;
}
9. Write a C program to input marks
and display the grade according to the following criteria:
- 80–100: Distinction
- 60–79: First Division
- 45–59: Second Division
- 32–44: Third Division
- Below 32: Fail
#include <stdio.h>
int main()
{
int marks;
printf("Enter
marks: ");
scanf("%d", &marks);
if(marks >= 80)
printf("Distinction");
else if(marks
>= 60)
printf("First Division");
else if(marks
>= 45)
printf("Second Division");
else if(marks
>= 32)
printf("Third Division");
else
printf("Fail");
return 0;
}
10. Write a C program to input a year and check
whether it 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("Leap year");
else
printf("Not a leap year");
return 0;
}
C. Loop Programs
11. Write a C program to display
numbers from 1 to 10 using a for loop.
#include <stdio.h>
int main()
{
int i;
for(i = 1; i <=
10; i++)
{
printf("%d\n", i);
}
return 0;
}
12. Write a C program to input a
number and calculate its factorial using a loop.
#include <stdio.h>
int main()
{
int n, i, fact =
1;
printf("Enter
a number: ");
scanf("%d",
&n);
for(i = 1; i <=
n; i++)
{
fact = fact *
i;
}
printf("Factorial = %d", fact);
return 0;
}
13. Write a C program to input a number and
display its multiplication table up to 10.
#include <stdio.h>
int main()
{
int n, i;
printf("Enter
a number: ");
scanf("%d", &n);
for(i = 1; i <=
10; i++)
{
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
14. Write a C program to input a number and
calculate the sum of its digits using a loop.
#include <stdio.h>
int main()
{
int n, digit, sum
= 0;
printf("Enter
a number: ");
scanf("%d", &n);
while(n != 0)
{
digit = n %
10;
sum = sum +
digit;
n = n / 10;
}
printf("Sum
of digits = %d", sum);
return 0;
}
15. Write a C program to input a number and check
whether it is a palindrome number or not.
#include <stdio.h>
int main()
{
int n, original,
reverse = 0, digit;
printf("Enter
a number: ");
scanf("%d", &n);
original = n;
while(n != 0)
{
digit = n %
10;
reverse =
reverse * 10 + digit;
n = n / 10;
}
if(original ==
reverse)
printf("Palindrome number");
else
printf("Not a palindrome number");
return 0;
}