0% found this document useful (0 votes)
16 views37 pages

Problem Solving Techniques in Programming

The document outlines various problem-solving techniques and programming concepts divided into four units, covering topics such as algorithm development, structured programming, statistical operations, and modular programming with arrays. Each unit includes specific programming tasks, algorithms, and C code examples for practical implementation. The document serves as a comprehensive guide for learning and applying programming skills through problem-solving exercises.
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)
16 views37 pages

Problem Solving Techniques in Programming

The document outlines various problem-solving techniques and programming concepts divided into four units, covering topics such as algorithm development, structured programming, statistical operations, and modular programming with arrays. Each unit includes specific programming tasks, algorithms, and C code examples for practical implementation. The document serves as a comprehensive guide for learning and applying programming skills through problem-solving exercises.
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

Problem Solving Techniques Unit Topic’s

UNIT I: Problem Solving and Algorithm Development


1. Convert Celsius to Fahrenheit
2. Sort Three Numbers
3. Display Number, Square, Cube
4. Display Odd Numbers
5. Multiplication Tables
6. Pattern of $
7. Decreasing Number Pattern
8. Arithmetic Progression
9. Geometric Progression
10. Fibonacci Sequence

UNIT II: Structured Programming Concepts


1. Convert Fahrenheit to Celsius
2. Sort Three Numbers Using If-Else
3. Table of Squares and Cubes
4. Odd Numbers Using While Loop
5. Multiplication Table Using Do-While
6. Pattern of $
7. Decreasing Number Pattern
8. Arithmetic Progression
9. Geometric Progression
10. Fibonacci Sequence Using While Loop

UNIT III: Numbers and Basic Statistical Operations


1. Extract Digits Left to Right
2. Check Palindrome
3. Check Prime
4. Factorial
5. Decimal to Binary
6. Armstrong Number
7. Sum Using Sentinel-Controlled Loop
8. Max, Min, Average
9. BMI and Category
10. Number in Words

UNIT IV: Modular Programming and Arrays


1. Circular Prime
2. Max of 8 Numbers
3. Mean, Range, Mode
4. Median
5. String Length and Reversal
6. Matrix Operations
7. Recursive Digit Counter 8a. Recursive Factorial 8b. Recursive Digit Display 8c. Recursive Power
Calculation
Problem-Solving Techniques
Long Question’s and Answer’s

UNIT I: Problem-Solving and Algorithm Development

Question: Write a program to calculate the area and circumference of a circle for a given radius.
The program should validate that the radius is a positive number.

Algorithm:
1. Start
2. Input radius
3. If radius ≤ 0, display error and exit
4. Calculate area = π × radius²
5. Calculate circumference = 2 × π × radius
6. Display area and circumference
7. End
C Code:
#include <stdio.h>
#include <conio.h>
#define PI 3.14159

void main() {
float radius, area, circumference;

clrscr(); // Optional: clears screen in Turbo C


printf("Enter the radius of the circle: ");
scanf("%f", &radius);

if (radius <= 0) {
printf("Invalid radius. Must be positive.\n");
getch();
return;
}

area = PI * radius * radius;


circumference = 2 * PI * radius;

printf("Area = %.2f\n", area);


printf("Circumference = %.2f\n", circumference);

getch();
}
Output:
Enter the radius of the circle: 5
Area = 78.54
Circumference = 31.42

Question: Write a program to determine whether a triangle is equilateral, isosceles, or scalene


based on the lengths of its sides.

Algorithm:
1. Start
2. Input three sides: a, b, c
3. If any side is ≤ 0 or violates triangle inequality, display error
4. If a == b == c → Equilateral
5. Else if a == b or b == c or a == c → Isosceles
6. Else → Scalene
7. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int a, b, c;

clrscr();
printf("Enter three sides of a triangle: ");
scanf("%d %d %d", &a, &b, &c);

if (a <= 0 || b <= 0 || c <= 0 || (a + b <= c) || (a + c <= b) || (b + c <= a)) {


printf("Invalid triangle sides.\n");
} else if (a == b && b == c) {
printf("Equilateral Triangle\n");
} else if (a == b || b == c || a == c) {
printf("Isosceles Triangle\n");
} else {
printf("Scalene Triangle\n");
}
getch();
}

Output:
Enter three sides of a triangle: 5 5 5
Equilateral Triangle

Question: Write a program to find the largest of three numbers using nested if-else statements.
Algorithm:
1. Start
2. Input three numbers: a, b, c
3. If a > b
o If a > c → a is largest
o Else → c is largest
4. Else
o If b > c → b is largest
o Else → c is largest
5. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int a, b, c;

clrscr();
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a > b) {
if (a > c)
printf("Largest number is: %d\n", a);
else
printf("Largest number is: %d\n", c);
} else {
if (b > c)
printf("Largest number is: %d\n", b);
else
printf("Largest number is: %d\n", c);
}

getch();
}

Output:
Enter three numbers: 12 45 33
Largest number is: 45

Question: Write a program to check whether a given year is a leap year.

Algorithm:
1. Start
2. Input year
3. If (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
o Leap year
4. Else
o Not a leap year
5. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int year;

clrscr();
printf("Enter a year: ");
scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

getch();
}

Output:
Enter a year: 2024
2024 is a leap year.

Question: Write a program to calculate simple interest given principal, rate, and time.

Algorithm:
1. Start
2. Input principal, rate, time
3. SI = (P × R × T) / 100
4. Display SI
5. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
float principal, rate, time, si;

clrscr();
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time in years: ");
scanf("%f", &time);

si = (principal * rate * time) / 100;


printf("Simple Interest = %.2f\n", si);

getch();
}

Output:
Enter principal amount: 10000
Enter rate of interest: 5
Enter time in years: 2
Simple Interest = 1000.00

Question: Write a program that takes a positive integer n and displays numbers from 1 to n
along with their squares and cubes in a tabular format.

Algorithm:
1. Start
2. Input n
3. Loop from 1 to n
4. For each number, calculate square and cube
5. Display in tabular format
6. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, i;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);

printf("Number\tSquare\tCube\n");
for (i = 1; i <= n; i++) {
printf("%d\t%d\t%d\n", i, i*i, i*i*i);
}

getch(); }
Output:
Enter a positive integer: 3
Number Square Cube
1 1 1
2 4 8
3 9 27

Question: Write a program that displays all odd numbers from 1 to n.

Algorithm:
1. Start
2. Input n
3. Loop from 1 to n
4. If number is odd, display it
5. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, i;

clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);

printf("Odd numbers from 1 to %d:\n", n);


for (i = 1; i <= n; i++) {
if (i % 2 != 0)
printf("%d ", i);
}

getch();
}

Output:
Enter a positive integer: 10
Odd numbers from 1 to 10:
13579

Question: Write a program to display the first n multiplication tables, each up to m rows.

Algorithm:
1. Start
2. Input n and m
3. Loop from 1 to n
4. For each table, loop from 1 to m
5. Display table
6. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, m, i, j;

clrscr();
printf("Enter number of tables: ");
scanf("%d", &n);
printf("Enter number of rows per table: ");
scanf("%d", &m);

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


printf("Table of %d:\n", i);
for (j = 1; j <= m; j++) {
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n");
}
getch();
}

Output:
Enter number of tables: 2
Enter number of rows per table: 3
Table of 1:
1x1=1
1x2=2
1x3=3

Table of 2:
2x1=2
2x2=4
2x3=6

Question: Write a program to display the first n terms of an arithmetic progression given the first
term and common difference.
Algorithm:
1. Start
2. Input first term (a), difference (d), and number of terms (n)
3. Loop from 0 to n-1
4. Calculate term = a + i*d
5. Display term
6. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int a, d, n, i;

clrscr();
printf("Enter first term (a): ");
scanf("%d", &a);
printf("Enter common difference (d): ");
scanf("%d", &d);
printf("Enter number of terms (n): ");
scanf("%d", &n);

printf("Arithmetic Progression:\n");
for (i = 0; i < n; i++) {
printf("%d ", a + i*d);
}

getch();
}

Output:
Enter first term (a): 2
Enter common difference (d): 3
Enter number of terms (n): 5
Arithmetic Progression:
2 5 8 11 14

Question: Write a program to display the first n terms of the Fibonacci sequence.

Algorithm:
1. Start
2. Input n
3. Initialize a = 0, b = 1
4. Loop n times
5. Display a
6. Update a = b, b = a + b
7. End

C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, a = 0, b = 1, c, i;

clrscr();
printf("Enter number of terms: ");
scanf("%d", &n);

printf("Fibonacci Sequence:\n");
for (i = 0; i < n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}

getch();
}

Output:
Enter number of terms: 6
Fibonacci Sequence:
011235

UNIT: 2 Structured Programming Concepts


Question: Write a program to convert temperature from Fahrenheit to Celsius.
Algorithm:
1. Start
2. Input Fahrenheit
3. Celsius = (Fahrenheit - 32) × 5/9
4. Display Celsius
5. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
float fahrenheit, celsius;
clrscr();
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);

celsius = (fahrenheit - 32) * 5 / 9;


printf("Temperature in Celsius: %.2f\n", celsius);

getch();
}
Output:
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37.00
Question: Write a program to sort three numbers in non-decreasing order using if-else
statements.
Algorithm:
1. Start
2. Input a, b, c
3. Use conditional swaps to sort
4. Display sorted numbers
5. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int a, b, c, temp;

clrscr();
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a > b) { temp = a; a = b; b = temp; }


if (a > c) { temp = a; a = c; c = temp; }
if (b > c) { temp = b; b = c; c = temp; }

printf("Sorted order: %d %d %d\n", a, b, c);

getch();
}
Output:
Enter three numbers: 9 3 5
Sorted order: 3 5 9
Question: Write a program to display number, square, and cube from 1 to n using a for loop.
Algorithm:
1. Start
2. Input n
3. Loop i = 1 to n
4. Display i, i², i³
5. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, i;

clrscr();
printf("Enter value of n: ");
scanf("%d", &n);

printf("Num\tSquare\tCube\n");
for (i = 1; i <= n; i++) {
printf("%d\t%d\t%d\n", i, i*i, i*i*i);
}

getch();
}
Output:
Enter value of n: 4
Num Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
Question: Write a program to display odd numbers from 1 to n using a while loop.
Algorithm:
1. Start
2. Input n
3. Initialize i = 1
4. While i ≤ n
o If i is odd, display i
o Increment i
5. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, i = 1;

clrscr();
printf("Enter value of n: ");
scanf("%d", &n);

printf("Odd numbers:\n");
while (i <= n) {
if (i % 2 != 0)
printf("%d ", i);
i++;
}
getch();
}
Output:
Enter value of n: 10
Odd numbers:
13579
Question: Write a program to display multiplication table of a number using do-while loop.
Algorithm:
1. Start
2. Input number and limit
3. Initialize i = 1
4. Do
o Display number × i
o Increment i
5. While i ≤ limit
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int num, limit, i = 1;

clrscr();
printf("Enter number: ");
scanf("%d", &num);
printf("Enter limit: ");
scanf("%d", &limit);

do {
printf("%d x %d = %d\n", num, i, num * i);
i++;
} while (i <= limit);

getch();
}
Output:
Enter number: 5
Enter limit: 3
5x1=5
5 x 2 = 10
5 x 3 = 15
Question: Write a program to display a pattern of $ symbols in increasing rows.
Algorithm:
1. Start
2. Input n
3. Loop i = 1 to n
4. Loop j = 1 to i
5. Print $
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, i, j;

clrscr();
printf("Enter number of rows: ");
scanf("%d", &n);

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


for (j = 1; j <= i; j++) {
printf("$");
}
printf("\n");
}

getch();
}
Output:
Enter number of rows: 3
$
$$
$$$
Question: Write a program to display decreasing number pattern from 12345 to 1.
Algorithm:
1. Start
2. Input n
3. Loop i = n to 1
4. Loop j = 1 to i
5. Print j
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, i, j;

clrscr();
printf("Enter value of n: ");
scanf("%d", &n);
for (i = n; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
getch();
}
Output:
Enter value of n: 5
12345
1234
123
12
1
Question: Write a program to display n terms of an arithmetic progression.
Algorithm:
1. Start
2. Input a, d, n
3. Loop i = 0 to n-1
4. term = a + i*d
5. Display term
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int a, d, n, i;

clrscr();
printf("Enter first term (a): ");
scanf("%d", &a);
printf("Enter common difference (d): ");
scanf("%d", &d);
printf("Enter number of terms (n): ");
scanf("%d", &n);
printf("Arithmetic Progression:\n");
for (i = 0; i < n; i++) {
printf("%d ", a + i*d);
}

getch();
}
Output:
Enter first term (a): 2
Enter common difference (d): 3
Enter number of terms (n): 5
Arithmetic Progression:
2 5 8 11 14
Question: Write a program to display n terms of a geometric progression.
Algorithm:
1. Start
2. Input a, r, n
3. Loop i = 0 to n-1
4. term = a × r^i
5. Display term
6. End
C Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main() {
int a, r, n, i;

clrscr();
printf("Enter first term (a): ");
scanf("%d", &a);
printf("Enter common ratio (r): ");
scanf("%d", &r);
printf("Enter number of terms (n): ");
scanf("%d", &n);

printf("Geometric Progression:\n");
for (i = 0; i < n; i++) {
printf("%d ", a * (int)pow(r, i));
}

getch();
}
Output:
Enter first term (a): 3
Enter common ratio (r): 2
Enter number of terms (n): 5
Geometric Progression:
3 6 12 24 48
Question: Write a program to display the first n terms of the Fibonacci sequence using a while
loop.
Algorithm:
1. Start
2. Input n
3. Initialize a = 0, b = 1, count = 0
4. While count < n
o Display a
o c=a+b
o a = b, b = c
o Increment count
5. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, a = 0, b = 1, c, count = 0;

clrscr();
printf("Enter number of terms: ");
scanf("%d", &n);

printf("Fibonacci Sequence:\n");
while (count < n) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
count++;
}

getch();
}
Output:
Enter number of terms: 6
Fibonacci Sequence:
0 1 1 2 3 5

UNIT III: Problems on Numbers and Basic Statistical Operations


Question: Write a program to extract and display each digit of a number from left to right.
Algorithm:
1. Start
2. Input number
3. Count digits using a loop
4. Use divisor = 10^(digits-1)
5. Extract and display digits using division and modulus
6. End
C Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main() {
int num, temp, digits = 0, divisor;

clrscr();
printf("Enter a positive integer: ");
scanf("%d", &num);

temp = num;
while (temp > 0) {
digits++;
temp /= 10;
}

divisor = pow(10, digits - 1);


printf("Digits from left to right: ");
while (divisor > 0) {
printf("%d ", num / divisor);
num %= divisor;
divisor /= 10;
}

getch();
}
Output:
Enter a positive integer: 2739
Digits from left to right: 2 7 3 9
Question: Write a program to check if a number is a palindrome.
Algorithm:
1. Start
2. Input number
3. Reverse the number
4. Compare with original
5. Display result
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int num, reversed = 0, temp, digit;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);

temp = num;
while (temp > 0) {
digit = temp % 10;
reversed = reversed * 10 + digit;
temp /= 10;
}

if (num == reversed)
printf("%d is a palindrome.\n", num);
else
printf("%d is not a palindrome.\n", num);

getch();
}
Output:
Enter a number: 121
121 is a palindrome.
Question: Write a program to check if a number is prime.
Algorithm:
1. Start
2. Input number
3. Check divisibility from 2 to sqrt(n)
4. If divisible, not prime
5. Else, prime
6. End
C Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main() {
int num, i, isPrime = 1;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

getch();
}
Output:
Enter a number: 29
29 is a prime number.
Question: Write a program to compute the factorial of a number.
Algorithm:
1. Start
2. Input n
3. Initialize fact = 1
4. Loop i = 1 to n
5. fact *= i
6. Display fact
7. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int n, i;
long fact = 1;

clrscr();
printf("Enter a number: ");
scanf("%d", &n);

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


fact *= i;
}
printf("Factorial of %d is %ld\n", n, fact);

getch();
}
Output:
Enter a number: 5
Factorial of 5 is 120
Question: Write a program to convert a decimal number to binary.
Algorithm:
1. Start
2. Input decimal number
3. Divide by 2 and store remainders
4. Reverse and display remainders
5. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int num, binary[32], i = 0;

clrscr();
printf("Enter a decimal number: ");
scanf("%d", &num);

while (num > 0) {


binary[i] = num % 2;
num /= 2;
i++;
}

printf("Binary: ");
for (i = i - 1; i >= 0; i--) {
printf("%d", binary[i]);
}

getch();
}
Output:
Enter a decimal number: 10
Binary: 1010
Question: Write a program to check if a number is an Armstrong number (sum of cubes of digits
equals the number).
Algorithm:
1. Start
2. Input number
3. Extract digits and compute sum of cubes
4. Compare with original number
5. Display result
6. End
C Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main() {
int num, temp, digit, sum = 0;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);

temp = num;
while (temp > 0) {
digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}

if (sum == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

getch();
}
Output:
Enter a number: 153
153 is an Armstrong number.
Question: Write a program to compute the sum of numbers entered by the user until -1 is
entered.
Algorithm:
1. Start
2. Initialize sum = 0
3. Loop: input number
4. If number == -1, break
5. Add to sum
6. Display sum
7. End
C Code:
#include <stdio.h>
#include <conio.h>
void main() {
int num, sum = 0;

clrscr();
printf("Enter numbers (-1 to stop):\n");

while (1) {
scanf("%d", &num);
if (num == -1)
break;
sum += num;
}

printf("Sum = %d\n", sum);

getch();
}
Output:
Enter numbers (-1 to stop):
10
20
30
-1
Sum = 60
Question: Write a program to compute the maximum, minimum, and average of a sequence of
numbers entered using sentinel-controlled repetition.
Algorithm:
1. Start
2. Initialize max, min, sum, count
3. Loop: input number
4. If number == -1, break
5. Update max, min, sum, count
6. Compute average = sum / count
7. Display results
8. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int num, max, min, sum = 0, count = 0;

clrscr();
printf("Enter numbers (-1 to stop):\n");
scanf("%d", &num);
if (num == -1) {
printf("No numbers entered.\n");
getch();
return;
}

max = min = num;


sum += num;
count++;

while (1) {
scanf("%d", &num);
if (num == -1)
break;
if (num > max) max = num;
if (num < min) min = num;
sum += num;
count++;
}

printf("Max = %d\n", max);


printf("Min = %d\n", min);
printf("Average = %.2f\n", (float)sum / count);

getch();
}
Output:
Enter numbers (-1 to stop):
10
20
5
15
-1
Max = 20
Min = 5
Average = 12.50
Question: Write a program to compute Body Mass Index (BMI) and categorize it.
Algorithm:
1. Start
2. Input weight (kg) and height (m)
3. BMI = weight / (height × height)
4. Categorize:
o < 18.5 → Underweight
o 18.5–24.9 → Normal
o 25–29.9 → Overweight
o ≥ 30 → Obese
5. Display BMI and category
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
float weight, height, bmi;

clrscr();
printf("Enter weight (kg): ");
scanf("%f", &weight);
printf("Enter height (m): ");
scanf("%f", &height);

bmi = weight / (height * height);


printf("BMI = %.2f\n", bmi);

if (bmi < 18.5)


printf("Category: Underweight\n");
else if (bmi < 25)
printf("Category: Normal\n");
else if (bmi < 30)
printf("Category: Overweight\n");
else
printf("Category: Obese\n");

getch();
}
Output:
Enter weight (kg): 70
Enter height (m): 1.75
BMI = 22.86
Category: Normal
Question: Write a program to display each digit of a number in words (e.g., 543 → FIVE FOUR
THREE).
Algorithm:
1. Start
2. Input number
3. Reverse number
4. Extract digits and map to words
5. Display words
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void main() {
int num, digit, rev = 0;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);

// Reverse the number to print left to right


int temp = num;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}

printf("Number in words: ");


while (rev > 0) {
digit = rev % 10;
switch (digit) {
case 0: printf("ZERO "); break;
case 1: printf("ONE "); break;
case 2: printf("TWO "); break;
case 3: printf("THREE "); break;
case 4: printf("FOUR "); break;
case 5: printf("FIVE "); break;
case 6: printf("SIX "); break;
case 7: printf("SEVEN "); break;
case 8: printf("EIGHT "); break;
case 9: printf("NINE "); break;
}
rev /= 10;
}

getch();
}
Output:
Enter a number: 5432
Number in words: FIVE FOUR THREE TWO

UNIT: 4 Modular Programming and Arrays


Question: Write a modular program to check if a number is a circular prime (all rotations are
prime).
Algorithm:
1. Start
2. Input number
3. Rotate digits and check each rotation for primality
4. If all rotations are prime → circular prime
5. Else → not circular prime
6. End
C Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>

int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0) return 0;
return 1;
}

int rotate(int n, int len) {


int pow10 = pow(10, len - 1);
return (n % pow10) * 10 + n / pow10;
}

void main() {
int num, temp, len = 0, isCircular = 1;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);

temp = num;
while (temp > 0) {
len++;
temp /= 10;
}

temp = num;
for (int i = 0; i < len; i++) {
if (!isPrime(temp)) {
isCircular = 0;
break;
}
temp = rotate(temp, len);
}

if (isCircular)
printf("%d is a circular prime.\n", num);
else
printf("%d is not a circular prime.\n", num);

getch();
}
Output:
Enter a number: 197
197 is a circular prime.
Question: Write a modular program to find the maximum of 8 numbers.
Algorithm:
1. Start
2. Input 8 numbers into array
3. Loop through array to find max
4. Display max
5. End
C Code:
#include <stdio.h>
#include <conio.h>

int findMax(int arr[], int size) {


int max = arr[0];
for (int i = 1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return max;
}

void main() {
int arr[8];

clrscr();
printf("Enter 8 numbers:\n");
for (int i = 0; i < 8; i++)
scanf("%d", &arr[i]);

printf("Maximum = %d\n", findMax(arr, 8));

getch();
}
Output:
Enter 8 numbers:
12 45 67 23 89 34 56 78
Maximum = 89
Question: Write a modular program to compute mean, range, and mode of an array.
Algorithm:
1. Start
2. Input array
3. Compute mean = sum / n
4. Compute range = max - min
5. Count frequency for mode
6. Display results
7. End
C Code:
#include <stdio.h>
#include <conio.h>

int findMax(int arr[], int n) {


int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];
return max;
}

int findMin(int arr[], int n) {


int min = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] < min) min = arr[i];
return min;
}

int findMode(int arr[], int n) {


int maxCount = 0, mode = arr[0];
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++)
if (arr[j] == arr[i]) count++;
if (count > maxCount) {
maxCount = count;
mode = arr[i];
}
}
return mode;
}

void main() {
int arr[100], n, sum = 0;

clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}

printf("Mean = %.2f\n", (float)sum / n);


printf("Range = %d\n", findMax(arr, n) - findMin(arr, n));
printf("Mode = %d\n", findMode(arr, n));

getch();
}
Output:
Enter number of elements: 5
Enter 5 elements:
23242
Mean = 2.60
Range = 2
Mode = 2
Question: Write a modular program to compute the median of an array of integers.
Algorithm:
1. Start
2. Input array elements
3. Sort the array
4. If n is odd → median = middle element
5. If n is even → median = average of two middle elements
6. Display median
7. End
C Code:
#include <stdio.h>
#include <conio.h>

void sort(int arr[], int n) {


for (int i = 0; i < n-1; i++)
for (int j = i+1; j < n; j++)
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

float findMedian(int arr[], int n) {


sort(arr, n);
if (n % 2 == 0)
return (arr[n/2 - 1] + arr[n/2]) / 2.0;
else
return arr[n/2];
}

void main() {
int arr[100], n;

clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Median = %.2f\n", findMedian(arr, n));

getch();
}
Output:
Enter number of elements: 5
Enter 5 elements:
31425
Median = 3.00
Question: Write your own functions to compute string length and reverse a string.
Algorithm:
1. Start
2. Input string
3. Loop to count characters
4. Loop to reverse string
5. Display results
6. End
C Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>

int stringLength(char str[]) {


int i = 0;
while (str[i] != '\0') i++;
return i;
}

void reverseString(char str[]) {


int len = stringLength(str);
for (int i = len - 1; i >= 0; i--)
printf("%c", str[i]);
}

void main() {
char str[100];

clrscr();
printf("Enter a string: ");
gets(str);

printf("Length = %d\n", stringLength(str));


printf("Reversed = ");
reverseString(str);

getch();
}
Output:
Enter a string: hello
Length = 5
Reversed = olleh
Question: Write a modular program to perform matrix addition, subtraction, and transpose.
Algorithm:
1. Start
2. Input matrices A and B
3. Add and subtract element-wise
4. Transpose matrix A
5. Display results
6. End
C Code:
#include <stdio.h>
#include <conio.h>

void addMatrix(int a[10][10], int b[10][10], int r, int c) {


printf("Addition:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
printf("%d ", a[i][j] + b[i][j]);
printf("\n");
}
}

void subtractMatrix(int a[10][10], int b[10][10], int r, int c) {


printf("Subtraction:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
printf("%d ", a[i][j] - b[i][j]);
printf("\n");
}
}

void transposeMatrix(int a[10][10], int r, int c) {


printf("Transpose:\n");
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++)
printf("%d ", a[j][i]);
printf("\n");
}
}

void main() {
int a[10][10], b[10][10], r, c;

clrscr();
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

printf("Enter matrix A:\n");


for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &a[i][j]);

printf("Enter matrix B:\n");


for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &b[i][j]);

addMatrix(a, b, r, c);
subtractMatrix(a, b, r, c);
transposeMatrix(a, r, c);

getch();
}
Output:
Enter rows and columns: 2 2
Enter matrix A:
12
34
Enter matrix B:
56
78
Addition:
68
10 12
Subtraction:
-4 -4
-4 -4
Transpose:
13
24
Question: Write a recursive program to count the number of digits in a number.
Algorithm:
1. Base case: if n == 0 → return 0
2. Recursive case: return 1 + count(n / 10)
C Code:
#include <stdio.h>
#include <conio.h>

int countDigits(int n) {
if (n == 0)
return 0;
return 1 + countDigits(n / 10);
}

void main() {
int num;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);

printf("Number of digits = %d\n", countDigits(num));

getch();
}
Output:
Enter a number: 5432
Number of digits = 4

UNIT : Recursive Programming


Question(A): Write a recursive function to compute the factorial of a number.
Algorithm:
1. If n = = 0 or n = = 1 → return 1
2. Else → return n × factorial(n - 1)
C Code:
#include <stdio.h>
#include <conio.h>

long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

void main() {
int num;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);

printf("Factorial of %d is %ld\n", num, factorial(num));

getch();
}
Output:
Enter a number: 5
Factorial of 5 is 120
Question(B): Write recursive functions to display digits of a number from left to right and right
to left. Display Digits Left to Right and Right to Left (Recursive)
Algorithm:
 Left to Right: Recurse until n < 10, then print digits on return
 Right to Left: Print digit, then recurse with n / 10
C Code:
#include <stdio.h>
#include <conio.h>

void printLeftToRight(int n) {
if (n < 10)
printf("%d ", n);
else {
printLeftToRight(n / 10);
printf("%d ", n % 10);
}
}

void printRightToLeft(int n) {
if (n == 0) return;
printf("%d ", n % 10);
printRightToLeft(n / 10);
}

void main() {
int num;

clrscr();
printf("Enter a number: ");
scanf("%d", &num);
printf("Left to Right: ");
printLeftToRight(num);
printf("\nRight to Left: ");
printRightToLeft(num);
getch();
}
Output:
Enter a number: 1234
Left to Right: 1 2 3 4
Right to Left: 4 3 2 1

Question(C): Write a recursive function to compute x raised to the power y using only
multiplication.
Algorithm:
1. If y = = 0 → return 1
2. Else → return x × power(x, y - 1)
C Code:
#include <stdio.h>
#include <conio.h>

int power(int x, int y) {


if (y == 0)
return 1;
else
return x * power(x, y - 1);
}

void main() {
int x, y;

clrscr();
printf("Enter base (x): ");
scanf("%d", &x);
printf("Enter exponent (y): ");
scanf("%d", &y);

printf("%d^%d = %d\n", x, y, power(x, y));


getch();
}
Output:
Enter base (x): 2
Enter exponent (y): 4
2 ^ 4 = 16

You might also like