0% found this document useful (0 votes)
3 views36 pages

C ASS

The document contains a series of programming assignments in C, each with a problem statement, input/output specifications, algorithms, and source code. Assignments cover various topics including arithmetic operations, comparisons, finding greatest numbers, checking for palindromes, calculating sums, finding factors, and determining prime or composite numbers. Each assignment is structured with clear instructions and example outputs.

Uploaded by

pratyay.ent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views36 pages

C ASS

The document contains a series of programming assignments in C, each with a problem statement, input/output specifications, algorithms, and source code. Assignments cover various topics including arithmetic operations, comparisons, finding greatest numbers, checking for palindromes, calculating sums, finding factors, and determining prime or composite numbers. Each assignment is structured with clear instructions and example outputs.

Uploaded by

pratyay.ent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Assignment-1 Date: 14/05/2025

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:

num1 and num2

Output:

Sum and product of num1 and num2

Flow Chart:

Algorithm:

1. Start

2. Declare variables: num1, num2, sum, product

3. Read the first number into num1 and second number into num2

1|Page
4. Calculate sum = num1 + num2

5. Calculate product = num1 * num2

6. Display the sum of num1 and num2

7. Display the product of num1 and num2

8. End

Source Code:

#include <stdio.h>

int main()

int num1, num2, sum, product;

// Getting Input

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

// Performing Sum and Product

sum = num1 + num2;

product = num1 * num2;

// Printing output

printf("Sum of %d and %d is: %d\n", num1, num2, sum);

printf("Product of %d and %d is: %d\n", num1, num2, product);

return 0;

Output:

Enter first number: 4

Enter second number: 5

Sum of 4 and 5 is: 9

Product of 4 and 5 is: 20

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:

num1 and num2

Output:

Greater between num1 and num2 else EQUAL

Flow Chart:

Algorithm:

1. Start

2. Input the first number (`num1`)

3. Input the second number (`num2`)

3|Page
4. Compare the two numbers:

If (num1 > num2):

 Print (num1 is greater than num2)

Else If (num2 > num1):

 Print (num2 is greater than num1)

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

Enter second number: 10

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:

Greatest and third greatest among num

Flow Chart:

(refer to the next page)

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).

3. Ask the user to enter the total number of elements (n).

4. If n < 3, print an error message: "Enter at least three numbers" And terminate the program...

5. Prompt the user to input n numbers one by one.

6. Loop through each number:

 Read the input into variable num.


 Compare num with first:

 If num > first:

 Move second to third

 Move first to second

 Set first = num

o Else if num > second:

 Move second to third

 Set second = num

o Else if num > third:

 Set third = num

7. After the loop ends:

 Print first as the greatest number.


 Print third as the third greatest number.
8. End

Source Code:

#include <stdio.h>

#include <limits.h>

int main() {

int n, num;

int first = INT_MIN, second = INT_MIN, third = INT_MIN;

printf("Enter the number of elements (n >= 3): ");

scanf("%d", &n);

if (n < 3) {

printf("At least 3 numbers are required.\n");

7|Page
return 1;

printf("Enter %d numbers:\n", n);

// comparing numbers

for (int i = 0; i < n; i++) {

scanf("%d", &num);

if (num > first) {

third = second;

second = first;

first = num;

} else if (num > second) {

third = second;

second = num;

} else if (num > third) {

third = num;

printf("Greatest number: %d\n", first);

printf("Third greatest number: %d\n", third);

return 0;

Output:

Enter the number of elements (n >= 3): 6

Enter 6 numbers:

12

45

89

45

33

Greatest number: 89

Third greatest number: 33

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:

Even and Odd numbers upto n

Flow Chart:

9|Page
Algorithm:

1. Start

2. Input the value of n (total numbers to read).

3. Initialize a counter i = 1.

4. Loop while i <= n:

o Input a number num.

o Check if num is even or odd:

 If num % 2 == 0, print "Even: num".

 Else, print "Odd: num".

o Increment i by 1 (i++).

5. End Loop when i > n.

6. Stop

Source Code:

#include <stdio.h>

int main() {

int n, i, num;

printf("Enter the value of n: ");

scanf("%d", &n);

printf("Enter %d numbers:", n);

for (i = 1; i <= n; i++) {

scanf("%d", &num);

if (num % 2 == 0) {

printf("Even: %d\n", num);

} else {

printf("Odd: %d\n", num);

} }

return 0;}

Output:

Enter the value of n: 3

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:

Write a program in C to check if a given number is a palindrome.

Input:

A number

Output:

Check for Palindrome

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:

 a. Get the last digit: remainder = num % 10

 b. Update reversed number: reversed = reversed * 10 + remainder

 c. Remove last digit from num: num = num / 10

6. Compare original with reversed:

 a. If they are equal, print "Palindrome".

 b. Otherwise, print "Not a palindrome".

7. End

Source Code:

#include <stdio.h>

int main() {

int num, reversed = 0, remainder, original;

printf("Enter an integer: ");

scanf("%d", &num);

original = num;

// Reverse the number

while (num != 0) {

remainder = num % 10;

reversed = reversed * 10 + remainder;

num /= 10; }

// Check if original and reversed are the same

if (original == reversed) {

printf("%d is a palindrome.\n", original);

} else {

printf("%d is not a palindrome.\n", original); }

return 0; }

Output:

Enter an integer: 1221

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:

Sum of natural numbers up to n

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;

// Ask user to enter a number

printf("Enter a positive integer: ");

scanf("%d", &n);

// Check if the number is positive

if (n <= 0) {

printf("Please enter a positive integer greater than 0.\n");

return 1;

13 | P a g e
//Calculate the sum using a loop

for (int i = 1; i <= n; ++i) {

sum += i;

// Output the result

printf("The sum of the first %d natural numbers is: %d\n", n, sum);

return 0;

Output:

Enter a positive integer: 5

The sum of the first 5 natural numbers is: 15

14 | P a g e
Assignment-7 Date: 14/05/2025

Problem Statement:

Write a program in C to read a number n and print factor of n.

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;

// Ask the user to enter a number

printf("Enter a positive integer: ");

scanf("%d", &n);

// Check if the input is positive

if (n <= 0) {

printf("Please enter a positive integer greater than 0.\n");

return 1;

printf("Factors of %d are:\n", n);

15 | P a g e
// Loop to find factors

for (int i = 1; i <= n; i++) {

if (n % i == 0) {

printf("%d ", i);

printf("\n");

return 0;

Output:

Enter a positive integer: 12

Factors of 12 are:

1 2 3 4 6 12

16 | P a g e
Assignment-8 Date: 14/05/2025

Problem Statement:

Write a program in C to read a number n and print first 10 multiples of n.

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

8. Print the result in the format: n x i = result

9. End

Source Code:

#include <stdio.h>

int main() {

int n;

// Ask the user to enter a number

printf("Enter a number: ");

scanf("%d", &n);

// Print the first 10 multiples of n

printf("The first 10 multiples of %d are:\n", n);

for (int i = 1; i <= 10; i++) {

printf("%d x %d = %d\n", n, i, n * i);

return 0;

17 | P a g e
Output:

Enter a number: 5

The first 10 multiples of 5 are:

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:

Write a program in C to read a number n and print if n is “PRIME” or “COMPOSITE”.

Input:

Value of n

Output:

Result whether n is Prime or Composite

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:

a. Set isPrime = 0 (not prime)

b. Break the loop

9. If isPrime == 1, then:

10. Print: "n is PRIME"

11. Else:

12. Print: "n is COMPOSITE"

13. End

Source Code:

#include <stdio.h>

int main() {

int n, isPrime = 1; // Assume n is prime

// Ask the user to enter a number

printf("Enter a positive integer: ");

scanf("%d", &n);

// Handle special cases

if (n <= 1) {
19 | P a g e
printf("%d is neither PRIME nor COMPOSITE.\n", n);

return 0;

// Check for factors from 2 to sqrt(n)

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {

isPrime = 0; // Not prime

break;

// Output result

if (isPrime)

printf("%d is PRIME.\n", n);

else

printf("%d is COMPOSITE.\n", n);

return 0;

Output:

Enter a positive integer: 7

7 is PRIME.

Enter a positive integer: 12

12 is COMPOSITE.

Enter a positive integer: 1

1 is neither PRIME nor COMPOSITE.

20 | P a g e
Assignment-10 Date: 14/05/2025

Problem Statement:

Write a program in C convert the temperature given in Celsius to Fahrenheit or vice-versa.

Input:

Choice of unit and temperature

Output:

Temperature in another unit

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:

 Prompt user to enter temperature in Celsius.

 Read temperature.

 Calculate converted = (temperature * 9 / 5) + 32.

 Print converted temperature in Fahrenheit.

7. Else if choice == 2, then:

 Prompt user to enter temperature in Fahrenheit.

 Read temperature.

 Calculate converted = (temperature - 32) * 5 / 9.

 Print converted temperature in Celsius.

8. Else:

 Print "Invalid choice."

9. End

Source Code:

#include <stdio.h>

int main() {

float temperature, converted;

int choice;

// Display options

printf("Temperature Conversion Menu:\n");

21 | P a g e
printf("1. Convert Celsius to Fahrenheit\n");

printf("2. Convert Fahrenheit to Celsius\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

if (choice == 1) {

// Celsius to Fahrenheit

printf("Enter temperature in Celsius: ");

scanf("%f", &temperature);

converted = (temperature * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit\n", temperature, converted);

} else if (choice == 2) {

// Fahrenheit to Celsius

printf("Enter temperature in Fahrenheit: ");

scanf("%f", &temperature);

converted = (temperature - 32) * 5 / 9;

printf("%.2f Fahrenheit = %.2f Celsius\n", temperature, converted);

} else {

printf("Invalid choice. Please enter 1 or 2.\n");

return 0;

Output:

Temperature Conversion Menu:

1. Convert Celsius to Fahrenheit

2. Convert Fahrenheit to Celsius

Enter your choice (1 or 2): 1

Enter temperature in Celsius: 25

25.00 Celsius = 77.00 Fahrenheit

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:

Fibonacci sequence up to n terms

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:

 Print: "Please enter a positive integer."

 End the program.

6) If n == 1, then:

 Print the first Fibonacci number (0).

7) If n > 1, then:

 Print the first two Fibonacci numbers (0 and 1).

 For i = 3 to n (inclusive), do the following:

a) Calculate next = a + b

b) Print next

c) Set a = b and b = next (shift values for the next iteration).

8) End

Source Code:

#include <stdio.h>

int main() {

int n;

// Ask the user for the number of terms

printf("Enter the number of terms for Fibonacci sequence: ");

scanf("%d", &n);

// First two Fibonacci numbers

int a = 0, b = 1;

23 | P a g e
// Check for valid input

if (n <= 0) {

printf("Please enter a positive integer.\n");

} else if (n == 1) {

printf("Fibonacci sequence up to %d term: %d\n", n, a);

} else {

printf("Fibonacci sequence up to %d terms: \n", n);

printf("%d %d ", a, b); // Print first two numbers

// Generate and print the rest of the sequence

for (int i = 3; i <= n; i++) {

int next = a + b;

printf("%d ", next);

a = b;

b = next;

printf("\n");

return 0;

Output:

Enter the number of terms for Fibonacci sequence: 10

Fibonacci sequence up to 10 terms:

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:

Sum of the harmonic series of n

Algorithm:

1. Start

2. Declare variables: n, sum.

3. Initialize sum= 0.

4. Read the value of n.

5. For Loop: Iterate through values of i from 1 to n:

6. In each iteration, calculate the reciprocal of i (i.e., 1/i) and add it to sum.

7. Print the value of sum.

8. End

Source Code:

#include <stdio.h>

int main()

int n;

double sum = 0.0;

// Getting the user input for n

printf("Enter the value of n: ");

scanf("%d", &n);

// Calculating the sum of the harmonic series

for (int i = 1; i <= n; i++)

sum += 1.0 / i; // Adding 1/i to the sum

// Printing the sum

25 | P a g e
printf("Sum of the harmonic series up to 1/%d is: %lf\n", n, sum);

return 0;

Output:

Enter the value of n: 5

Sum of the harmonic series up to 1/5 is: 2.283333

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:

Floating point number

Output:

Rightmost digit of the integral part of that floating point number

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;

// Read a floating-point number

printf("Enter a floating-point number: ");

scanf("%f", &number);

// Extract the integral part of the number

integralPart = (int) number; // Casting to int removes the fractional part

// Print the rightmost digits of the integral part

printf("The rightmost digit of the integral part is: %d\n", integralPart % 10);

return 0;

Output:

Enter a floating-point number: 1234.567

The rightmost digit of the integral part is: 4

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:

Result whether quadrilateral is rectangle or square

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() {

float length, breadth, area, perimeter;

// Input length and breadth

printf("Enter the length (in meters): ");

scanf("%f", &length);

printf("Enter the breadth (in meters): ");

scanf("%f", &breadth);

// Calculate area and perimeter

area = length * breadth;

perimeter = 2 * (length + breadth);

// Display results

printf("\nArea = %.2f square meters\n", area);

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:

Enter the length (in meters): 5

Enter the breadth (in meters): 5

Area = 25.00 square meters

Perimeter = 20.00 meters

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:

Display whether it is Alphabet, Number or Special Character

Algorithm:

1. Start

2. Declare a variable ch to store the input character.

3. Read the character and store it in ch.

4. Check the type of the character using ASCII value conditions:

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;

printf("Enter a character: "); // getting a character input

scanf("%c", &ch);

// Checking the type of character

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

printf("It is an ALPHABET.\n");

else if (ch >= '0' && ch <= '9')

30 | P a g e
printf("It is a NUMBER.\n");

else

printf("It is a SPECIAL CHARACTER.\n");

return 0;

Output:

Enter a character: A

It is an ALPHABET.

31 | P a g e
Assignment-16 Date: 14/05/2025

Problem Statement:

Write a program in C to print below patterns.

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

 a. Print n - i spaces (to center the pyramid)

 b. Print increasing numbers from 1 to i

 c. Print decreasing numbers from i - 1 down to 1

 d. Move to the next line

4) End

Source Code:

#include <stdio.h>

int main() {

int i, j, space, rows;

// Ask user for number of rows

printf("Enter the number of rows: ");

scanf("%d", &rows);

32 | P a g e
for (i = 1; i <= rows; i++) {

// Print leading spaces

for (space = 1; space <= rows - i; space++) {

printf(" ");

// Print increasing numbers

for (j = 1; j <= i; j++) {

printf("%d", j);

// Print decreasing numbers

for (j = i - 1; j >= 1; j--) {

printf("%d", j);

// Move to next line

printf("\n");

return 0;

Output:

Enter the number of rows: 5

33 | P a g e
Part-2:

Inverted Left Half Pyramid:

Algorithm:

1) Start
2) Input the number of rows n
3) Repeat for each row i from n down to 1:

 a. Print n - i spaces (to shift the stars to the right)

 b. Print i stars *

 c. Move to the next line

4) End

Source Code:

#include <stdio.h>

int main() {

int i, j, space, rows;

// Ask user for number of rows

printf("Enter the number of rows: ");

scanf("%d", &rows);

// Loop for each row

for (i = rows; i >= 1; i--) {

// Print leading spaces

for (space = 1; space <= rows - i; space++) {

printf(" ");

// Print stars

for (j = 1; j <= i; j++) {

printf("*");

34 | P a g e
// Move to next line

printf("\n");

return 0;

Output:

Enter the number of rows: 5

*****

****

***

**

Inverted Full Pyramid:

Algorithm:

1) Start
2) Input the number of rows n
3) For each row i from n down to 1, do:

 a. Print (n - i) spaces to create left padding

 b. Print i stars (*) separated by a space for pyramid shape

 c. Move to the next line

4) End

Source Code:

#include <stdio.h>

int main() {

int i, j, space, rows;

// Input number of rows

printf("Enter the number of rows: ");

scanf("%d", &rows);

// Loop through rows in reverse

for (i = rows; i >= 1; i--) {


35 | P a g e
// Print leading spaces

for (space = 0; space < rows - i; space++) {

printf(" ");

// Print stars (with space for better pyramid look)

for (j = 1; j <= i; j++) {

printf("* ");

// Move to next line

printf("\n");

return 0;

Output:

Enter the number of rows: 5

*****

****

***

**

36 | P a g e

You might also like