C ASS
C ASS
Problem Statement:
Write a program in C to read two numbers and produce the sum and product of those numbers and show the result
separately.
Input:
Output:
Flow Chart:
Algorithm:
1. Start
3. Read the first number into num1 and second number into num2
1|Page
4. Calculate sum = num1 + num2
8. End
Source Code:
#include <stdio.h>
int main()
// Getting Input
scanf("%d", &num1);
scanf("%d", &num2);
// Printing output
return 0;
Output:
2|Page
Assignment-2 Date: 14/05/2025
Problem Statement:
Write a program in C to read two numbers and print the greater number, if both the numbers are same then print
“EQUAL”.
Input:
Output:
Flow Chart:
Algorithm:
1. Start
3|Page
4. Compare the two numbers:
Else:
Print "EQUAL"
5. End
Source Code:
#include <stdio.h>
int main() {
int num1, num2;
// Input two numbers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Compare and print result
if(num1 > num2) {
printf("%d is greater than %d\n", num1, num2);
}
else if(num2 > num1) {
printf("%d is greater than %d\n", num2, num1);
}
else {
printf("EQUAL\n");
}
return 0;
}
Output:
Enter first number: 15
15 is greater than 10
4|Page
Assignment-3 Date: 14/05/2025
Problem Statement:
Write a program in C multiple numbers say n and print the greatest and the third greatest.
Input:
num and n
Output:
Flow Chart:
5|Page
6|Page
Algorithm:
1. Start
2. Declare three variables: first (to store the greatest number), second (to store the second greatest number),
third (to store the third greatest number).
Initialize all to the smallest possible integer (INT_MIN).
4. If n < 3, print an error message: "Enter at least three numbers" And terminate the program...
Source Code:
#include <stdio.h>
#include <limits.h>
int main() {
int n, num;
scanf("%d", &n);
if (n < 3) {
7|Page
return 1;
// comparing numbers
scanf("%d", &num);
third = second;
second = first;
first = num;
third = second;
second = num;
third = num;
return 0;
Output:
Enter 6 numbers:
12
45
89
45
33
Greatest number: 89
8|Page
Assignment-4 Date: 14/05/2025
Problem Statement:
Write a program in C to read n numbers and print the even and odd numbers up to n.
Input:
Value of n
Output:
Flow Chart:
9|Page
Algorithm:
1. Start
3. Initialize a counter i = 1.
o Increment i by 1 (i++).
6. Stop
Source Code:
#include <stdio.h>
int main() {
int n, i, num;
scanf("%d", &n);
scanf("%d", &num);
if (num % 2 == 0) {
} else {
} }
return 0;}
Output:
Enter 3 numbers: 10 13 15
Even: 10
Odd: 13
Odd: 15
10 | P a g e
Assignment-5 Date: 14/05/2025
Problem Statement:
Input:
A number
Output:
Flow Chart:
11 | P a g e
Algorithm:
1. Start
2. Input the integer number num.
3. Store the original value of num in a variable original.
4. Initialize a variable reversed to 0.
5. Repeat while num is not equal to 0:
7. End
Source Code:
#include <stdio.h>
int main() {
scanf("%d", &num);
original = num;
while (num != 0) {
num /= 10; }
if (original == reversed) {
} else {
return 0; }
Output:
1221 is a palindrome.
12 | P a g e
Assignment-6 Date: 14/05/2025
Problem Statement:
Write a program in C to read a number and print the sum of n natural numbers.
Input:
Value of n
Output:
Algorithm:
1. Start
2. Declare integer variables: n, i, sum.
3. Initialize sum to 0.
4. Prompt the user to enter a positive integer n.
5. Read the value of n.
6. If n is less than or equal to 0:
a. Print an error message: "Please enter a positive integer greater than 0."
b. Terminate the program.
7. Otherwise, perform the following steps:
a. Set i = 1
b. Repeat steps c–d while i <= n
c. Add i to sum
d. Increment i by 1
8. Print the result: "The sum of the first n natural numbers is: sum"
9. End
Source Code:
#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
if (n <= 0) {
return 1;
13 | P a g e
//Calculate the sum using a loop
sum += i;
return 0;
Output:
14 | P a g e
Assignment-7 Date: 14/05/2025
Problem Statement:
Input:
Value of n
Output:
Factor of n
Algorithm:
1. Start
2. Declare an integer variable n.
3. Prompt the user to enter a positive integer.
4. Read the value of n.
5. If n <= 0, then:
a. Print an error message
b. Terminate the program
6. Print a message: "Factors of n are:"
7. For each integer i from 1 to n, do:
a. If n % i == 0, then:
i. Print i
8. End
Source Code:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n <= 0) {
return 1;
15 | P a g e
// Loop to find factors
if (n % i == 0) {
printf("\n");
return 0;
Output:
Factors of 12 are:
1 2 3 4 6 12
16 | P a g e
Assignment-8 Date: 14/05/2025
Problem Statement:
Input:
Value of n
Output:
First 10 multiples of n
Algorithm:
1. Start
2. Declare an integer variable n.
3. Prompt the user to enter a number.
4. Read the value of n.
5. Print a message: "The first 10 multiples of n are:"
6. Repeat steps 7–8 for i from 1 to 10:
7. Multiply n * i
9. End
Source Code:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
return 0;
17 | P a g e
Output:
Enter a number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
18 | P a g e
Assignment-9 Date: 14/05/2025
Problem Statement:
Input:
Value of n
Output:
Algorithm:
1. Start
2. Declare integer variables: n, i, and a flag variable isPrime.
3. Prompt the user to enter a positive integer.
4. Read the value of n.
5. If n <= 1, then:
a. Print: "n is neither PRIME nor COMPOSITE."
b. End the program.
6. Set isPrime = 1 (assume the number is prime).
7. For each i from 2 to n / 2, do:
8. If n % i == 0, then:
9. If isPrime == 1, then:
11. Else:
13. End
Source Code:
#include <stdio.h>
int main() {
scanf("%d", &n);
if (n <= 1) {
19 | P a g e
printf("%d is neither PRIME nor COMPOSITE.\n", n);
return 0;
if (n % i == 0) {
break;
// Output result
if (isPrime)
else
return 0;
Output:
7 is PRIME.
12 is COMPOSITE.
20 | P a g e
Assignment-10 Date: 14/05/2025
Problem Statement:
Input:
Output:
Algorithm:
1. Start
2. Declare variables: temperature (float),converted (float),choice (int)
3. Display the menu: "1. Convert Celsius to Fahrenheit","2. Convert Fahrenheit to Celsius"
4. Prompt the user to enter their choice (1 or 2).
5. Read the value of choice.
6. If choice == 1, then:
Read temperature.
Read temperature.
8. Else:
9. End
Source Code:
#include <stdio.h>
int main() {
int choice;
// Display options
21 | P a g e
printf("1. Convert Celsius to Fahrenheit\n");
scanf("%d", &choice);
if (choice == 1) {
// Celsius to Fahrenheit
scanf("%f", &temperature);
} else if (choice == 2) {
// Fahrenheit to Celsius
scanf("%f", &temperature);
} else {
return 0;
Output:
22 | P a g e
Assignment-11 Date: 14/05/2025
Problem Statement:
Write a program in C to generate and print the Fibonacci sequence up to a specified number of terms.
Input:
Value of n
Output:
Algorithm:
1) Start
2) Declare integer variables: n (to store the number of terms), a, b, next (to store Fibonacci terms)
3) Prompt the user to enter the number of terms for the Fibonacci sequence.
4) Read the value of n.
5) If n <= 0, then:
6) If n == 1, then:
7) If n > 1, then:
a) Calculate next = a + b
b) Print next
8) End
Source Code:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a = 0, b = 1;
23 | P a g e
// Check for valid input
if (n <= 0) {
} else if (n == 1) {
} else {
int next = a + b;
a = b;
b = next;
printf("\n");
return 0;
Output:
0 1 1 2 3 5 8 13 21 34
24 | P a g e
Assignment-12 Date: 14/05/2025
Problem Statement:
Write a program in C to determine and print the sum of the following harmonic series for a given value of n:
1+1/2+1/3+……..1/n.
Input:
value of n
Output:
Algorithm:
1. Start
3. Initialize sum= 0.
6. In each iteration, calculate the reciprocal of i (i.e., 1/i) and add it to sum.
8. End
Source Code:
#include <stdio.h>
int main()
int n;
scanf("%d", &n);
25 | P a g e
printf("Sum of the harmonic series up to 1/%d is: %lf\n", n, sum);
return 0;
Output:
26 | P a g e
Assignment-13 Date: 14/05/2025
Problem Statement:
Write a program in C that reads a floating-point number and then displays the right most digits of integral part of the
number.
Input:
Output:
Algorithm:
1) Start
2) Declare variables: number (float), integralPart (int)
3) Prompt the user to enter a floating-point number.
4) Read the value of number.
5) Extract the integral part of number by casting it to an integer (integralPart = (int)number).
6) Calculate the rightmost digit of the integral part using integralPart % 10.
7) Print the rightmost digit.
8) End
Source Code:
#include <stdio.h>
#include <math.h>
int main() {
float number;
int integralPart;
scanf("%f", &number);
printf("The rightmost digit of the integral part is: %d\n", integralPart % 10);
return 0;
Output:
27 | P a g e
Assignment-14 Date: 14/05/2025
Problem Statement:
Write a program in C to accept the length and breadth in meters and calculate the area and perimeter and determine
if it is a rectangle or a square based on the inputs given.
Input:
Length, breadth
Output:
Algorithm:
1) Start
2) Input the length (in meters)
3) Input the breadth (in meters)
4) Calculate area using the formula:
area = length × breadth
5) Calculate perimeter using the formula:
perimeter = 2 × (length + breadth)
6) Display the area
7) Display the perimeter
8) If length is equal to breadth
→ Display: "It is a Square"
9) Else
→ Display: "It is a Rectangle"
10) End
Source Code:
#include <stdio.h>
int main() {
scanf("%f", &length);
scanf("%f", &breadth);
// Display results
28 | P a g e
printf("Perimeter = %.2f meters\n", perimeter);
// Determine shape
if (length == breadth) {
printf("It is a Square.\n");
} else {
printf("It is a Rectangle.\n");
return 0;
Output:
It is a Square.
29 | P a g e
Assignment-15 Date: 14/05/2025
Problem Statement:
Write a program in C to accept an input and determine if the input entered is a number or alphabet or a special
character.
Input:
Value of ch
Output:
Algorithm:
1. Start
5. 5.If 'A' <= ch <= 'Z' or 'a' <= ch <= 'z', then: Display "It is an ALPHABET."
6. 6.Else if '0' <= ch <= '9', then: Display "It is a NUMBER."
7. 7.Else: Display "It is a SPECIAL CHARACTER."
8. 8.End
Source Code:
#include <stdio.h>
int main()
char ch;
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("It is an ALPHABET.\n");
30 | P a g e
printf("It is a NUMBER.\n");
else
return 0;
Output:
Enter a character: A
It is an ALPHABET.
31 | P a g e
Assignment-16 Date: 14/05/2025
Problem Statement:
Input:
value of n
Output:
Desired pattern
Part-1:
Algorithm:
1) Start
2) Input the number of rows n
3) Repeat for each row i from 1 to n
4) End
Source Code:
#include <stdio.h>
int main() {
scanf("%d", &rows);
32 | P a g e
for (i = 1; i <= rows; i++) {
printf(" ");
printf("%d", j);
printf("%d", j);
printf("\n");
return 0;
Output:
33 | P a g e
Part-2:
Algorithm:
1) Start
2) Input the number of rows n
3) Repeat for each row i from n down to 1:
b. Print i stars *
4) End
Source Code:
#include <stdio.h>
int main() {
scanf("%d", &rows);
printf(" ");
// Print stars
printf("*");
34 | P a g e
// Move to next line
printf("\n");
return 0;
Output:
*****
****
***
**
Algorithm:
1) Start
2) Input the number of rows n
3) For each row i from n down to 1, do:
4) End
Source Code:
#include <stdio.h>
int main() {
scanf("%d", &rows);
printf(" ");
printf("* ");
printf("\n");
return 0;
Output:
*****
****
***
**
36 | P a g e