0% found this document useful (0 votes)
4 views

Problem solving and programming Assignments

The document outlines an assignment for a programming task that requires writing a menu-driven program in C to calculate the factorial of a number and check if a number is prime. It includes the code implementation, algorithms, and step-by-step execution examples for both functionalities. Additionally, it features a flowchart illustrating the program's structure and decision-making process.

Uploaded by

ashrafsuhail1414
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)
4 views

Problem solving and programming Assignments

The document outlines an assignment for a programming task that requires writing a menu-driven program in C to calculate the factorial of a number and check if a number is prime. It includes the code implementation, algorithms, and step-by-step execution examples for both functionalities. Additionally, it features a flowchart illustrating the program's structure and decision-making process.

Uploaded by

ashrafsuhail1414
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/ 8

Name : Ashraf Ahmed

Roll No.: 09
PRN : 12412991
Class : CSDS-A (FY)

Assignment 3
3. Write a Menu Driven Program to perform the following tasks:
a. Accept the number from the user and print its factorial.
b. Accept the number from the user and print if it is prime or non-
prime.
Solution:
#include <stdio.h>
int main() {
int choice, num, i, factorial = 1, isPrime = 1;

// Menu to choose between factorial and prime number check


printf("Choose an option:\n");
printf("1. Calculate factorial\n");
printf("2. Check if prime number\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);

switch (choice) {
case 1: // Calculate Factorial
printf("Enter a number to calculate its factorial: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d is %d\n", num, factorial);
}
break;

case 2: // Check if Prime Number


printf("Enter a number to check if it's prime: ");
scanf("%d", &num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0; // Not a prime number
break;
}
}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
}
break;

default:
printf("Invalid choice. Please choose 1 or 2.\n");
}

return 0;
}

Algorithm
1. Start
2. Display menu options:
o 1: Calculate Factorial
o 2: Check Prime Number
3. Take user input for choice
4. If choice is 1 (Factorial Calculation):
o Ask user for a number
o If the number is negative, print "Factorial is not defined
for negative numbers"
o Otherwise, initialize factorial = 1
o Loop from 1 to num, multiplying factorial by the loop
variable
o Print the factorial result
5. If choice is 2 (Prime Number Check):
o Ask user for a number
o If number is ≤ 1, print "Not a prime number"
o Otherwise, check divisibility from 2 to num/2
▪ If divisible, print "Not a prime number" and exit loop
▪ If no divisors are found, print "Prime number"
6. If choice is invalid, print "Invalid choice"
7. End

Example 1: Factorial
Input:
Choose an option:
1. Calculate factorial
2. Check if prime number
Enter your choice (1 or 2): 1
Enter a number to calculate its factorial: 5

Step-by-Step Execution:
Step Action Condition Value/Update Output

1 Start - - -

2 Menu - - Show options

3 Read choice - choice = 1 -

4 Check choice 1 == 1 - -

5 Read num - num = 5 -

6 Check num < 0 5 < 0 - -

7 Init fact - fact = 1 -

8 Loop Start i = 1 → num - -

9 i=1 - fact = 1 * 1 = 1 -

10 i=2 - fact = 1 * 2 = 2 -

11 i=3 - fact = 2 * 3 = 6 -

12 i=4 - fact = 6 * 4 = 24 -

13 i=5 - fact = 24 * 5 = 120 -

14 Loop End i > num - -

15 Print - - Fact(5) = 120

16 End - - -

Example 2: Prime Check


Input:
Choose an option:
1. Calculate factorial
2. Check if prime number
Enter your choice (1 or 2): 2
Enter a number to check if it's prime: 7
Step-by-Step Execution:

Step Action Condition Value/Update Output

1 Start - - -

2 Menu - - Show options

3 Read choice - choice = 2 -

4 Check choice 2 == 2 - -

5 Read num - num = 7 -

6 Check num <= 1 7 <= 1 - -

7 Init isPrime - isPrime = 1 -

8 Loop Start i = 2 → num/2 (3) - -

9 i=2 7 % 2 == 0 - -

10 i=3 7 % 3 == 0 - -

11 Loop End No divisors isPrime = 1 -

12 Check isPrime 1 == 1 - 7 is prime

13 End - - -
OUTPUT
FLOWCHART
Start

Display Menu (Rectangle):


1. Calculate Factorial
2. Check for Prime
3. Exit
Enter your choice:

Input Choice:
scanf("%d", &choice);

Decision (Choice) :
Case 1: Calculate Factorial
Case 2: Check for Prime
Case 3: Exit

Case 1: Calculate Factorial: Case 2: Check for Prime: Case 3: Exit

Read Num Input num


printf("Exiting...\n");

Fact = 1
Check if
i=1
num<= 1 End

isPrime=0,
i=2
Break
i <= num

While
i <= num/2
Fact = Fact * i

i++
If num % i == 0 If isPrime == 1

Print Fact

End isPrime=0, Prime Not Prime


i=i+1
Break

End

You might also like