Problem Solving Techniques in C
Problem Solving Techniques in C
Submitted By
7 Array Reversal
Aim
To calculate the GST (Goods and Services Tax) amount for a product based on the given
percentage and product base price, and to compute the final MRP.
Algorithm
1. Start
2. Read prod_base_price and tax_percent
3. Calculate Tax = prod_base_price * (tax_percent / 100)
4. Calculate prod_total_price = prod_base_price + Tax
5. Display Tax and prod_total_price
6. Stop
Pseudocode
BEGIN
INPUT prod_base_price
INPUT tax_percent
COMPUTE tax = prod_base_price * (tax_percent / 100)
COMPUTE prod_total_price = prod_base_price + tax
DISPLAY tax
DISPLAY prod_total_price
END
Flowchart
Program
#include <stdio.h>
#include <conio.h>
void main()
{
float base_price, tax_percent, tax, total_price;
Sample Result
Input:
Product base price = 1000
GST percentage = 18
Output:
Tax = 1000 × (18/100) = 180
Product Total Price (MRP) = 1000 + 180 = 1180
Result
The program successfully calculates the GST amount and the final MRP of the product.
Pseudocode
BEGIN
READ a, b, c
IF (a > b) THEN
IF (a > c) THEN
DISPLAY "a is greatest"
ELSE
DISPLAY "c is greatest"
END IF
ELSE
IF (b > c) THEN
DISPLAY "b is greatest"
ELSE
DISPLAY "c is greatest"
END IF
END IF
END
Flowchart
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
if (a > b)
{
if (a > c)
printf("Greatest number is: %d", a);
else
printf("Greatest number is: %d", c);
}
else
{
if (b > c)
printf("Greatest number is: %d", b);
else
printf("Greatest number is: %d", c);
}
Sample output
Case 1:
Enter three numbers: 25 42 18
Greatest number is: 42
Case 2:
Enter three numbers: 10 5 20
Greatest number is: 20
Case 3 (all equal):
Enter three numbers: 15 15 15
Greatest number is: 15
Result :
The algorithm, pseudocode, and flowchart were implemented successfully. The program correctly
identifies and displays the greatest number among the three given inputs.
Algorithm
1. Start
2. Input value of n
3. Initialize i = 2
4. Check if i <= n
o If true, go to Step 5
o If false, go to Step 8
5. Print value of i
6. Increment i by 2
7. Go to Step 4
8. Stop
Pseudocode
BEGIN
GET n
INITIALIZE i = 2
WHILE (i <= n) DO
PRINT i
i=i+2
ENDWHILE
END
FLOWCHART
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
Sample Output
Result
The program was successfully executed. It accepts a limit n from the user and prints all even
numbers up to n. The output verified that the algorithm, pseudocode, and program logic are correct.
Ex. No : 2
Date :
Algorithm
1. Start
2. Read a number n
3. Call the recursive function factorial(n)
4. Inside function:
o If n == 1, return 1
o Else return n * factorial(n-1)
5. Print the factorial result
6. Stop
Pseudocode
FUNCTION factorial(n)
IF n == 1 THEN
RETURN 1
ELSE
RETURN n * factorial(n-1)
END FUNCTION
BEGIN
READ n
f = factorial(n)
PRINT f
END
FLOWCHART
Program
#include <stdio.h>
int factorial(int n) {
if (n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int n, f;
printf("Enter the number: ");
scanf("%d", &n);
f = factorial(n);
printf("Factorial of %d is %d", n, f);
return 0;
}
Sample Output :
Enter the number: 5
Factorial of 5 is 120
Result
The program was executed successfully. It calculates the factorial of a number using a recursive
function, and the output matches the expected result.
Algorithm
1. Start
2. Read a number n
3. Initialize fact = 1
4. Repeat for i = 1 to n
o Multiply fact = fact * i
5. Print fact
6. Stop
Pseudocode
BEGIN
READ n
fact = 1
FOR i = 1 TO n DO
fact = fact * i
END FOR
PRINT fact
END
Flowchart
Program
#include <stdio.h>
int main() {
int n, i, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
Sample Output
Enter a number: 5
Factorial of 5 is: 120
Result
The program was successfully executed. It calculates the factorial of a number using an iterative
loop, and the result is correct.
Algorithm
1. Start
2. Read the limit n
3. Initialize a = 0, b = 1
4. Print a and b
5. While (a + b <= n)
o t=a+b
o Print t
o Update a = b, b = t
6. Stop
Pseudocode
BEGIN
READ n
a=0
b=1
PRINT a, b
WHILE (a + b <= n) DO
t=a+b
PRINT t
a=b
b=t
END WHILE
END
Program
#include <stdio.h>
int main() {
int n, a = 0, b = 1, t;
printf("Enter the limit: ");
scanf("%d", &n);
while (a + b <= n) {
t = a + b;
printf(" %d", t);
a = b;
b = t;
}
return 0;
}
Flowchart
Sample Output
Enter the limit: 10
Fibonacci series up to 10:
0112358
Result
The program was executed successfully. It generates the Fibonacci sequence up to the given limit
using iteration.
Algorithm
1. Start
2. Read the limit n
3. Define recursive function fibonacci(a, b, n)
o If a <= n
▪ Print a
▪ Call fibonacci(b, a+b, n)
o Else return
4. Call fibonacci(0, 1, n) from main
5. Stop
Pseudocode
FUNCTION fibonacci(a, b, n)
IF (a <= n) THEN
PRINT a
fibonacci(b, a+b, n)
END IF
END FUNCTION
BEGIN
READ n
PRINT "Fibonacci sequence up to n:"
CALL fibonacci(0, 1, n)
END
Program
#include <stdio.h>
void fibonacci(int a, int b, int n) {
if (a <= n) {
printf("%d ", a);
fibonacci(b, a + b, n);
}
}
int main() {
int n;
printf("Enter the limit: ");
scanf("%d", &n);
return 0;
}
Sample Output
Enter the limit: 10
Fibonacci series up to 10:
0112358
Result
The program was executed successfully.
It generates the Fibonacci series up to the given limit using recursion.
Ex. No 3
Date :
Application of Iterative Statements: For, While, and Do-While Loops
Program
#include <stdio.h>
#include <conio.h>
void main() {
int n, i;
clrscr(); // clear screen
printf("Enter the value of n: ");
scanf("%d", &n);
printf("First %d natural numbers are:\n", n);
for (i = 1; i <= n; i++) {
printf("%d ", i);
}
getch(); // wait for key press
}
Flowchart
Sample Output
Enter the value of n: 5
First 5 natural numbers are:
12345
Result
The program successfully prints the first n natural numbers using a for loop. The loop iterates from 1
to n and displays the numbers sequentially
Experiment 3(b): Print the First n Natural Numbers using While Loop
AIM
To write a C program that prints the first n natural numbers using a while loop.
Algorithm
1. Start
2. Input the value of n (the number of natural numbers to be printed)
3. Initialize counter i = 1
4. While i <= n do
o Print i
o Increment i by 1
5. Stop
Pseudocode
BEGIN
INPUT n
INITIALIZE i = 1
WHILE i <= n DO
PRINT i
i=i+1
ENDWHILE
END
Program
#include <stdio.h>
#include <conio.h>
void main() {
int n, i = 1;
clrscr(); // clear screen
Flowchart
Sample Output
Enter the value of n: 5
First 5 natural numbers are:
12345
Result
The program was successfully executed. It prints the first n natural numbers using a while loop.
Experiment 3(c): Print the First n Natural Numbers using Do-While Loop
AIM
To write a C program that prints the first n natural numbers using a do-while loop.
Algorithm
1. Start
2. Input the value of n (the number of natural numbers to be printed)
3. Initialize counter i = 1
4. Do the following:
o Print i
o Increment i by 1
5. While i <= n
6. Stop
Pseudocode
BEGIN
INPUT n
INITIALIZE i = 1
DO
PRINT i
i=i+1
WHILE i <= n
END
Program
#include <stdio.h>
#include <conio.h>
void main() {
int n, i = 1;
do {
printf("%d ", i);
i++;
} while (i <= n);
Flowchart
Sample Output
Enter the value of n: 5
First 5 natural numbers are:
12345
Result
The program was successfully executed. It prints the first n natural numbers using a do-while loop,
ensuring the loop body executes at least once.
Ex. No: 4
Date :
Decision-Making Programs in C Using If-Else, Nested If-Else, and If-Else Ladder
Statements
c. Write a program to determine the grade of a student based on their marks using an if-else ladder.
Pseudocode:
START
INPUT age
IF age >= 18 THEN
PRINT "You are eligible for voting"
ELSE
PRINT "You are not eligible for voting"
END IF
STOP
C Program :
#include <stdio.h>
int main() {
int age;
// Read the age from the user
printf("Enter your age: ");
scanf("%d", &age);
// Decision making using if-else
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
Flow chart
Result:
The program successfully determines if a person is eligible to vote based on their age using the if-else
statement.
Problem 2: Number Classification (Nested If-Else)
Write a program to determine if a given number is positive, negative, or zero using nested if-else statements.
C Program :
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.\n", num);
} else {
if (num < 0) {
printf("%d is negative.\n", num);
} else {
printf("%d is zero.\n", num);
}
}
return 0;
}
Flow chart
Result :
The program successfully determines if a given number is positive, negative, or zero using nested if-else
statements.
Pseudocode:
START
INPUT marks
IF marks >= 90 THEN
PRINT "Your grade: A"
ELSE IF marks >= 70 THEN
PRINT "Your grade: B"
ELSE IF marks >= 50 THEN
PRINT "Your grade: C"
ELSE
PRINT "Your grade: Failed"
END IF
STOP
C program
#include<stdio.h>
int main() {
int marks;
// Input the marks from the user
printf("Enter your marks (0-100): ");
scanf("%d", &marks);
// If-Else Ladder to determine the grade
if (marks >= 90) {
printf("Your Grade: A\n");
} else if (marks >= 70) {
printf("Your Grade: B\n");
} else if (marks >= 50) {
printf("Your Grade: C\n");
} else {
printf("Your Grade: Failed\n");
}
return 0;
}
Result:
The program successfully determines the grade of a student based on their marks using an if-else ladder.
Ex. No : 5
Date :
Implementation of Switch Case
Aim:
To develop a simple scientific calculator using a switch-case statement that performs basic arithmetic
operations such as addition, subtraction, multiplication, division, and modulus.
Algorithm:
1. Start.
2. Display a menu with options for different operations (addition, subtraction, multiplication, division, and
modulus).
3. Read the user's choice of operation.
4. Based on the user's choice, use the switch-case statement to perform the respective operation:
o Case 1: Addition.
o Case 2: Subtraction.
o Case 3: Multiplication.
o Case 4: Division.
o Case 5: Modulus.
5. For each operation, ask the user to input two numbers.
6. Perform the chosen operation and display the result.
7. If the user enters an invalid choice, display an error message.
8. Stop.
Pseudocode:
sql
Copy code
START
DISPLAY menu with options for addition, subtraction, multiplication, division, modulus
INPUT choice
SWITCH choice
CASE 1:
INPUT two numbers
ADD the numbers
PRINT result
BREAK
CASE 2:
INPUT two numbers
SUBTRACT the numbers
PRINT result
BREAK
CASE 3:
INPUT two numbers
MULTIPLY the numbers
PRINT result
BREAK
CASE 4:
INPUT two numbers
DIVIDE the numbers
IF second number is zero THEN
PRINT error message
ELSE
PRINT result
BREAK
CASE 5:
INPUT two numbers
FIND modulus
PRINT result
BREAK
DEFAULT:
PRINT invalid choice
STOP
Flowchart:
C Program:
c
Copy code
#include <stdio.h>
int main() {
int choice;
double num1, num2, result;
case 2:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;
case 3:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;
case 4:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
if (num2 == 0) {
printf("Error: Division by zero is not allowed.\n");
} else {
result = num1 / num2;
printf("Result: %.2lf\n", result);
}
break;
case 5:
printf("Enter two integers: ");
int int1, int2;
scanf("%d %d", &int1, &int2);
if (int2 == 0) {
printf("Error: Modulus by zero is not allowed.\n");
} else {
result = int1 % int2;
printf("Result: %d\n", (int)result);
}
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Sample Output:
Example 1: Addition
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 1
Enter two numbers: 10 20
Result: 30.00
Example 2: Division with Error Handling
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 4
Enter two numbers: 10 0
Error: Division by zero is not allowed.
Example 3: Modulus
markdown
Copy code
Simple Scientific Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 5
Enter two integers: 10 3
Result: 1
Result:
The program successfully implements a simple scientific calculator using the switch-case statement. It can
perform addition, subtraction, multiplication, division, and modulus operations based on user input. The
program also handles invalid choices and division/modulus by zero errors.
Ex. No: 6
Date :
Implementation of Arrays to Handle Group of Similar Data
6. Describe a problem statement in your domain/department where you need to work with group
of same type of data. Provide a solution in terms of C program to store and manage the data
effectively.
Aim
To write a C program that demonstrates how to store and manage a group of data of the same type
(integers in this case) using an array.
Algorithm
1. Start.
2. Declare an array of integers values with size 5.
3. Prompt the user to enter 5 integers.
4. Use a for loop to read 5 integers from the user and store them in the array.
5. Display the stored integers using another for loop.
6. End.
Pseudo Code
BEGIN
DECLARE array `values` of size 5
PRINT "Enter 5 integers: "
FOR i = 0 to 4
READ input and STORE it in values[i]
END FOR
Program
#include <stdio.h>
#include <conio.h>
void main() {
int values[5];
int i;
// Input 5 integers
printf("Enter 5 integers: ");
for (i = 0; i < 5; i++) {
scanf("%d", &values[i]);
}
Sample Output
Enter 5 integers: 10 20 30 40 50
Displaying integers:
10
20
30
40
50
Result
The program successfully stores 5 integers entered by the user in an array and displays them as output.
This demonstrates effective data management of similar types using arrays in C.
Ex. No: 7
Date :
Array Reversal
7. You're playing UNO cards, suddenly a person is getting rev card. Write a C program to
reverse the round by storing the number of players in array.
Aim
To write a C program to reverse the order of players in an UNO game when a reverse card is drawn,
using arrays.
Algorithm
1. Start.
2. Prompt the user to enter the number of players and store it in n.
3. Declare an integer array players of size n.
4. Input player numbers into the array.
5. Display the original order of players.
6. Call the function reverse Array to reverse the order of the array:
o Swap the first element with the last, the second with the second last, and so on.
7. Display the reversed order of players.
8. End.
Pseudo Code
BEGIN
INPUT number of players (n)
DECLARE array `players` of size n
PRINT "Enter the player numbers:"
FOR i = 0 to n-1
READ players[i]
END FOR
PRINT "Original order of players:"
FOR i = 0 to n-1
PRINT players[i]
END FOR
CALL reverseArray(players, n):
FOR i = 0 to n/2
SWAP players[i] and players[n-i-1]
END FOR
RETURN
PRINT "Reversed order of players:"
FOR i = 0 to n-1
PRINT players[i]
END FOR
END
Flow chart
Sub program
Main program
Program
#include <stdio.h>
#include <conio.h>
void main() {
int n, i;
Sample Output
Enter the number of players: 5
Enter the player numbers: 1 2 3 4 5
Original order: 1 2 3 4 5
Reversed order: 5 4 3 2 1
Result
The program effectively reverses the order of players in an UNO game using arrays, mimicking the
effect of a reverse card.
Ex. No: 8
Date :
Array Separation into Odd and Even Numbers
8. Write a C program for Vehicle Regulation System where odd number ending vehicles can
use the road on odd days and even number ending vehicles can use the road on even days using
two separate arrays to store and display the odd and even numbers.
Aim
To develop a C program for a vehicle regulation system where vehicles ending with an odd number
can use the road on odd days, and vehicles ending with an even number can use the road on even
days, using two separate arrays to store and display odd and even vehicle numbers.
Algorithm
1. Start.
2. Prompt the user to input the number of vehicles (n).
3. Declare three arrays:
o arr1[] to store all vehicle numbers.
o arr2[] to store even-numbered vehicles.
o arr3[] to store odd-numbered vehicles.
4. Loop through to input n vehicle numbers into arr1[].
5. For each number in arr1[]:
o If the number is even, store it in arr2[].
o If the number is odd, store it in arr3[].
6. Display the contents of arr2[] (vehicles allowed on even days).
7. Display the contents of arr3[] (vehicles allowed on odd days).
8. End.
Pseudocode
BEGIN
INPUT number of vehicles (n)
DECLARE arr1[n], arr2[n], arr3[n], j = 0, k = 0
PRINT "Enter vehicle numbers:"
FOR i = 0 TO n-1
INPUT arr1[i]
END FOR
FOR i = 0 TO n-1
IF arr1[i] % 2 == 0 THEN
arr2[j] = arr1[i]
INCREMENT j
ELSE
arr3[k] = arr1[i]
INCREMENT k
END IF
END FOR
PRINT "Vehicles allowed on Even days:"
FOR i = 0 TO j-1
PRINT arr2[i]
END FOR
PRINT "Vehicles allowed on Odd days:"
FOR i = 0 TO k-1
PRINT arr3[i]
END FOR
END
Program
#include <stdio.h>
#include <conio.h>
void main() {
int arr1[10], arr2[10], arr3[10];
int i, j = 0, k = 0, n;
printf("\nVehicle regulation system allows vehicles to use the road on specific days:\n");
printf("-----------------------------------------------------------------\n");
Sample Output
Enter the number of vehicle numbers to be stored in the array: 6
Enter 6 vehicle numbers in the array:
Vehicle number-1: 1234
Vehicle number-2: 5679
Vehicle number-3: 2468
Vehicle number-4: 1357
Vehicle number-5: 1122
Vehicle number-6: 7789
The vehicles that can use the road on Even days are:
1234 2468 1122
The vehicles that can use the road on Odd days are:
5679 1357 7789
Result
The program successfully separates odd and even vehicle numbers into two arrays and displays
them. Vehicles ending in:
• Even numbers can use the road on even days.
• Odd numbers can use the road on odd days
Ex. No: 9
Date :
Using Structures in C for Real-Time Data Management
9. Describe a problem statement in your domain/department where you need to work with group
of different type of data. Provide a solution in terms of C program to store and manage the data
effectively
Aim
To write a C program using structures to store and manage student details (name, roll number, and
marks) effectively.
Algorithm
1. Start
2. Define a structure student with fields:
o Name (string)
o roll (integer)
o marks (float)
3. Declare an array of student structure to store details of multiple students.
4. Loop through each student and:
o Assign roll number automatically
o Input name
o Input marks
5. Display all stored student details in a formatted manner.
6. Stop
Pseudocode
BEGIN
DEFINE structure student (Name, roll, marks)
DECLARE array of students
Flowchart
Program
#include <stdio.h>
#include <conio.h>
void main() {
int i;
Sample Output
Enter information of students:
Displaying Information:
Roll number: 1
Name: Arjun
Marks: 85.5
Roll number: 2
Name: Divya
Marks: 90.0
Roll number: 3
Name: Kiran
Marks: 78.5
Result
The program was successfully executed. It demonstrates how to use structures in C to store and
manage a group of different data types (string, integer, float) together, effectively handling student
details.
Ex. No: 10
Date :
Student Admission Eligibility Check using Structure
10. Write a C program to get the details of the student (roll no, name, date of birth, state, 10th
percentage and 12th percentage) using Structure. Calculate the age of the student and display
the eligibility status for his admission. Eligibility criteria: more than 60 percent in 10th and 12th,
age>=17, state==TN
Aim
To create a C program to collect and display student details, calculate their age, and determine their
eligibility for admission based on specific criteria.
Algorithm
1. Start.
2. Define a structure Student to store student details:
o Roll No.
o Name.
o Date of Birth (DOB).
o State.
o 10th percentage.
o 12th percentage.
3. Write a function calculateAge to compute age from the given DOB.
4. In the main function:
o Declare a variable of type Student.
o Prompt the user to enter the student's details.
o Call calculateAge to calculate the student's age.
5. Check eligibility criteria:
o Age should be greater than or equal to 17.
o Both 10th and 12th percentages should be greater than or equal to 60.
o State should be "TN".
6. Display the eligibility status based on the conditions.
7. Print all the entered and calculated student details.
8. End.
Pseudo Code
BEGIN
DEFINE structure `Student` with fields: rollNo, name, dob, state, tenthPercentage,
twelfthPercentage.
FUNCTION calculateAge(dob):
PARSE dob into year, month, day.
GET current year.
RETURN current year - year of birth.
MAIN:
DECLARE student of type Student.
INPUT student details (rollNo, name, dob, state, percentages).
IF (age >= 17) AND ([Link] >= 60.0) AND ([Link] >=
60.0) AND (state == "TN"):
PRINT "Eligible for Admission".
ELSE:
PRINT "Not Eligible for Admission".
Program
#include <stdio.h>
#include <conio.h>
#include <string.h>
// Define structure for student
struct Student {
int rollNo;
char name[50];
int age;
char state[50];
float tenthPercentage;
float twelfthPercentage;
};
void main() {
struct Student student;
Sample Output
Enter Roll No: 101
Enter Name: Arjun
Enter Age: 18
Enter State: TN
Enter 10th Percentage: 85
Enter 12th Percentage: 82
Student Details:
Roll No: 101
Name: Arjun
Age: 18
State: TN
10th Percentage: 85.00
12th Percentage: 82.00
Result
The program accurately calculates the age and determines the eligibility of the student for admission
based on the given criteria.