Computer Programming Worksheet PDF
Document Details
Uploaded by WondrousVerse7524
Mansoura University
Mohammed Adel Khalifa Bin Salama
Tags
Summary
This document contains practice questions and solutions in computer programming, specifically focused on flowcharting and algorithm design, including examples of C++ program code. It includes questions and answers for topics such as calculating the sum of numbers within a range, identifying even and odd numbers, finding the largest of three values, and calculating the area of geometric figures.
Full Transcript
# Student Workbook - Computer Programming **Mohammed Adel Khalifa Bin Salama** ## Question 02 **Step through the flowchart, and write down the formula for SUM variable?** ``` SUM = 0 N = 1 SUM = SUM + N N = N + 2 ``` **Formula**: SUM = (1 + 3 + 5 ... + 49) ## Question 03 **Step through the fl...
# Student Workbook - Computer Programming **Mohammed Adel Khalifa Bin Salama** ## Question 02 **Step through the flowchart, and write down the formula for SUM variable?** ``` SUM = 0 N = 1 SUM = SUM + N N = N + 2 ``` **Formula**: SUM = (1 + 3 + 5 ... + 49) ## Question 03 **Step through the flowchart, and write down the formula for SUM variable?** ``` SUM = 0 N = 1 SUM = SUM + N N = N + 2 ``` **Formula**: SUM = (1 + 3 + 5 ... + 49) ## Question 04 **Assuming the user enters these values while running the code equivalent to the flowchart below, step through the flowchart and find the value of the MAX value?** ``` INPUT N = 63 CURRENT = 63 COUNTER = 1 ``` **Value of MAX**: 63 ## Question 05 **Write an algorithm and draw a flowchart to count and print from 1 to 10.** **Algorithm:** ``` count = 1 print count count = count + 1 while count <= 10 repeat steps 2, 3 end ``` **Flowchart:** ``` Start count = 1 print count count = count + 1 YES (count <= 10) print count count = count + 1 NO End ``` ## Question 06: **Write an algorithm to calculate the summation for a list of numbers and then draw a flowchart of that algorithm.** **Algorithm:** ``` SUM = 0, COUNT = 1, NUM INPUT NUM SUM = SUM + NUM COUNT = COUNT + 1 while COUNT <= NUM repeat steps 2, 3, 4 PRINT SUM ``` **Flowchart:** ``` Start SUM = 0 COUNT = 1 INPUT NUM SUM = SUM + NUM COUNT = COUNT + 1 YES (COUNT <= NUM) INPUT NUM SUM = SUM + NUM COUNT = COUNT + 1 NO PRINT SUM End ``` ## Question 07 **Draw a flowchart to find out if a number is divisible by 3.** ``` Start INPUT N YES (N % 3 == 0) PRINT "Divisible by 3" NO PRINT "Not divisible by 3" End ``` ## Question 08 **Draw a flowchart to display the largest of the three numbers.** ``` Start INPUT N1, N2, N3 YES (N1 > N2) YES (N1 > N3) PRINT N1 NO PRINT N3 NO YES (N2 > N3) PRINT N2 NO PRINT N3 End ``` ## Question 09 **Draw a flowchart to count the number of odd and even numbers.** ``` Start NUM, NODD = 0, NEVEN = 0 INPUT NUM YES (NUM % 2 == 0) NEVEN = NEVEN + 1 NO NODD = NODD + 1 PRINT NUMBER OF ODD NUMBERS: NODD PRINT NUMBER OF EVEN NUMBERS: NEVEN End ``` ## Question 10 **Draw a flowchart to check if first number is divisible by the second.** ``` Start INPUT N1, N2 YES (N1 % N2 == 0) PRINT "Divisible" NO PRINT "Not Divisible" End ``` ## Question 11 **Draw a flowchart to display the average of first ten odd numbers.** ``` Start NODD = 0, SUM = 0 AVG = SUM / NODD YES (NODD < 10) SUM = SUM + I I = I + 2 NODD = NODD + 1 NO PRINT AVG End ``` ## Question 12 **Write an algorithm and draw only one flowchart to sum the following for all the numbers between 1 and 100: a) Even numbers and then display its sum. b) Odd numbers and then display its sum.** **Algorithms:** ``` // Even numbers SUM = 0 I = 2 while I <= 100 SUM = SUM + I I = I + 2 PRINT SUM // Odd numbers SUM = 0 I = 1 while I <= 100 SUM = SUM + I I = I + 2 PRINT SUM ``` **Flowchart:** ``` Start // Even numbers: SUM_EVEN = 0 I = 2 YES (I <= 100) SUM_EVEN = SUM_EVEN + I I = I + 2 NO PRINT SUM_EVEN // Odd numbers: SUM_ODD = 0 I = 1 YES (I <= 100) SUM_ODD = SUM_ODD + I I = I + 2 NO PRINT SUM_ODD End ``` ## Question 13 **Draw a flowchart to display the product of 2 and a specific number.** ``` Start INPUT N RESULT = 2 * N PRINT RESULT End ``` ## Question 14 **Draw a flowchart to display the highest of any ten numbers.** ``` Start INPUT A, B, C, D, E, F, G, H, I, J MAX = A YES (B > MAX) MAX = B NO YES (C > MAX) MAX = C NO YES (D > MAX) MAX = D NO YES (E > MAX) MAX = E NO YES (F > MAX) MAX = F NO YES (G > MAX) MAX = G NO YES (H > MAX) MAX = H NO YES (I > MAX) MAX = I NO YES (J > MAX) MAX = J NO PRINT MAX End ``` ## Question 15 **Draw a flowchart to find the area of the Circle.** ``` Start INPUT R AREA = 3.14159 * R * R PRINT AREA End ``` ## Question 16 **Draw a flowchart to display the lowest of the three numbers.** ``` Start INPUT N1, N2, N3 LOWEST = N1 YES (N2 < LOWEST) LOWEST = N2 NO YES (N3 < LOWEST) LOWEST = N3 NO PRINT LOWEST End ``` ## Question 17 **Write a C program that reads the length and width of a farmer's field from the user in feet and displays the area of the field in acre.** **Hint:** There are 43,560 square feet in an acre. ```c #include <stdio.h> int main() { float leng, wid, area, acre; printf("Please enter the length and width \n"); scanf("%f %f", &leng, &wid); area = leng * wid; acre = area / 43560; printf("%f area of the field in Acres, acre); return 0; } ``` ## Question 18 **Write a C program to input any character and check whether it is alphabet, digit or special character.** ```c #include <stdio.h> int main() { char ch; printf("Enter any character: "); scanf("%c", &ch); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { printf("%c is alphabet.", ch); } else if (ch >= '0' && ch <= '9') { printf("%c is digit.", ch); } else { printf("%c is special character.", ch); } return 0; } ``` ## Question 19 **Write a C program that reads a four-digit integer from the user and displays the sum of the digits in the number.** ```c #include <stdio.h> int main() { int n, t, sum = 0, remainder; printf("Enter an integer \n"); scanf("%d", &n); t = n; while(t != 0) { remainder = t % 10; sum = sum + remainder; t = t / 10; } printf("Sum of digits of %d = %d", n, sum ); return 0; } ``` ## Question 20 **By using if-else statement, write a C program that reads the score of a student in three subjects. If the total score of these subjects is greater than or equal 150, then print "Pass", otherwise print "Fail".** ```c #include <stdio.h> int main() { int s1, s2, s3, sum; printf("Please enter three grades: \n"); scanf("%d %d %d", &s1, &s2, &s3); if (s1 > 100 || s2 > 100 || s3 > 100) { printf("Wrong grades"); return 0; } sum = s1 + s2 + s3; if (sum >= 150) { printf("Pass"); } else { printf("Fail"); } return 0; } ``` ## Question 21 **By using if-else statement, write a C program that reads a number and checks whether it is even or odd number.** ```c #include <stdio.h> int main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (num % 2 == 0) { printf("%d is even.", num); } else { printf("%d is odd.", num); } return 0; } ``` ## Question 22 **Draw the equivalent flowchart of the following program:** ```c #include <stdio.h> void main() { int x; printf("Enter the score of the student: \n"); scanf("%d", &x); if(x>=90) { printf("A\n"); } else if(x>=70) { printf("B\n"); } else if(x>=60) { printf("C\n"); } else if(x>=50) { printf("D\n"); } else { printf("F\n"); } } ``` **Flowchart:** ``` Start READ 'N' YES (N >= 90) PRINT A NO YES (N >= 70) PRINT B NO YES (N >= 60) PRINT C NO YES (N >= 50) PRINT D NO PRINT F End ``` ## Question 23 **By using if-else if statement, write a simple calculator program that can add, subtract, multiply and divide two floating point numbers.** ```c #include <stdio.h> int main() { float a, b, choice; printf("Enter your choice: \n"); printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n"); scanf("%f", &choice); printf("Enter two decimal numbers \n"); scanf("%f %f", &a, &b); if (choice == 1) { printf("Addition of %.2f and %.2f is %.2f\n", a, b, a + b); } else if (choice == 2) { printf("Subtraction of %.2f and %.2f is %.2f\n", a, b, a - b); } else if (choice == 3) { printf("Multiplication of %.2f and %.2f is %.2f\n", a, b, a * b); } else if (choice == 4) { if (b != 0) { printf("Division of %.2f and %.2f is %.2f\n", a, b, a / b); } else { printf("The number cannot be divided by zero!\n"); } } else { printf("You have entered wrong choice!\n"); } return 0; } ``` ## Question 24 **The following equation is used to obtain the solution of second-degree equation:** $$ x_{1,2} = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$ **Write a C program to solve the second-order equation (Ax² + Bx + C = 0) by entering the parameters A, B, and C and printing the final results x1 and x2 as follows:** **a. If (b² > 4ac) then print the values of x₁ and x₂ as follows:** $$ x_1 = \frac{-b + \sqrt{b^2 - 4ac}}{2a} $$ $$ x_2 = \frac{-b - \sqrt{b^2 - 4ac}}{2a} $$ **b. If (b² = 4ac) then** $$ x_1 = x_2 = \frac{-b}{2a} $$ **c. If (b² < 4ac) then "Imaginary roots" and then print the real and imaginary parts of each root as follows:** $$ Re = \frac{-b}{2a} $$ $$ Im = \frac{\sqrt{b^2 - 4ac}}{2a} $$ ```c #include <stdio.h> #include <math.h> int main() { float a, b, c, discriminant, x1, x2, Re, Im; printf("Enter your coefficients: \n"); scanf("%f %f %f", &a, &b, &c); discriminant = b * b - 4 * a * c; if (discriminant > 0) { x1 = (-b - sqrt(discriminant)) / (2 * a); x2 = (-b + sqrt(discriminant)) / (2 * a); printf("root 1= %f and root 2= %f \n", x1, x2); } else if (discriminant == 0) { x1 = x2 = -b / (2 * a); printf("x1=x2=%f", x1); } else { Re = -b / (2 * a); Im = sqrt(-discriminant) / (2 * a); printf("x1=%.2f+%.2fi and x2 = %.2f-%.2fi \n", Re, Im, Re, Im); } return 0; } ``` ## Question 25 **Write a C program to input any alphabet and check whether it is vowel or consonant.** ```c #include <stdio.h> int main() { char alphabet; printf("Enter the alphabet: "); scanf("%c", &alphabet); if (alphabet == 'a' || alphabet == 'e' || alphabet == 'i' || alphabet == 'o' || alphabet == 'u' || alphabet == 'A' || alphabet == 'E' || alphabet == 'I' || alphabet == 'O' || alphabet == 'U') { printf("The alphabet is vowel."); } else { printf("The alphabet is consonant."); } return 0; } ``` ## Question 26 **Write a C program that computes the average of a collection of values entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your program should display an appropriate error message if the first value entered by the user is 0.** **Hint:** Because the 0 marks the end of the input, it should not be included in the average. ```c #include <stdio.h> int main() { float num, sum = 0, avg, count = 0; printf("Enter a collection of numbers: \n"); scanf("%f", &num); if (num == 0) { printf("Error: First value cannot be 0.\n"); } else { sum = sum + num; count = count + 1; for (;;) { scanf("%f", &num); if (num == 0) { break; } sum = sum + num; count++; } avg = sum / count; printf("The average: %.2f\n", avg); } return 0; } ``` ## Question 27 **The wavelength of visible light ranges from 380 to 750 nanometers (nm). While the spectrum is continuous, it is often divided into 6 colors as shown below:** | Color | Wavelength (nm) | |----------|-------------------------| | Violet | 380 to less than 450 | | Blue | 450 to less than 495 | | Green | 495 to less than 570 | | Yellow | 570 to less than 590 | | Orange | 590 to less than 620 | | Red | 620 to 750 | **Write a C program that reads a wavelength from the user and reports its color. The program will display an appropriate error message if the wavelength entered by the user is outside of the visible spectrum.** ```c #include <stdio.h> int main() { int waveL; printf("Please enter the wavelength: \n"); scanf("%d", &waveL); if (waveL < 750 && waveL > 620) { printf("The color is red"); } else if (waveL > 590 && waveL <= 620) { printf("The color is orange"); } else if (waveL > 570 && waveL <= 590) { printf("The color is yellow"); } else if (waveL > 495 && waveL <= 570) { printf("The color is green"); } else if (waveL > 450 && waveL <= 495) { printf("The color is blue"); } else if (waveL > 380 && waveL <= 450) { printf("The color is violet"); } else { printf("The wavelength entered is outside the visible range."); } return 0; } ``` ## Question 28 **A triangle can be classified based on the lengths of its sides as equilateral (متساوي اضلاع), isosceles (متساوي الساقين), or scalene (مختلف الاضلاع). All 3 sides of an equilateral triangle have the same length. An isosceles triangle has two sides that are the same length, and a third side that is a different length. If all of the sides have different lengths, then the triangle is scalene.** **Write a C program that reads the lengths of 3 sides of a triangle from the user and displays a message indicating the type of the triangle.** ```c #include <stdio.h> int main() { float x, y, z; printf("Enter the length of three sides : \n"); scanf("%f %f %f", &x, &y, &z); if (x + y > z && x + z > y && y + z > x && x > 0 && y > 0 && z > 0) { if (x == y && y == z) { printf("This is Equilateral"); } else if (x != y && y != z && x != z) { printf("This is Scalene"); } else { printf("This is Isosceles"); } } else { printf("Invalid triangle"); } return 0; } ``` ## Question 29 **By using for loop, write a C program to draw the following figures:** **a. ** ``` * ** *** **** ***** ****** ******* ``` **b. ** ``` ******* ****** ***** **** *** ** * ``` **c. ** ``` * ** *** **** ***** ****** ******* ``` ```c #include <stdio.h> int main() { // a. int n = 7; for (int j = 0; j < n; j++) { for (int i = 0; i <= j; i++) { printf("*"); } printf("\n"); } // b. for (int j = 0; j < n; j++) { for (int i = 0; i < n - j; i++) { printf("*"); } printf("\n"); } // c. for (int j = 0; j < n; j++) { for (int i = 0; i <= j; i++) { printf("*"); } printf("\n"); } return 0; } ``` ## Question 30 **Write a C program that prints the product table until reaching 9x9 as follows:** ``` 1x1=1 1x2=2 ... 9x8=72 9x9=81 ``` ```c #include <stdio.h> int main() { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { printf("%dx%d=%d\n", i, j, i * j); } } return 0; } ``` ## Question 31 **Write a C program that reads a number and then it calculates and prints the average of numbers between 1 to this number.** ```c #include <stdio.h> int main() { int n, sum = 0, numbers; float average; printf("Enter how many numbers you want: \n"); scanf("%d", &n); printf("\n Please enter the elements one by one: \n"); for (int i = 1; i <= n; i++) { scanf("%d", &numbers); sum = sum + numbers; } average = (float)sum / n; printf("\nSum of the %d numbers = %d \n", n, sum); printf("\nAverage of the %d numbers = %.2f\n", n, average); return 0; } ``` ## Question 32 **Write a C program that calculates and prints the sum of the even integers from 2 to 30.** ```c #include <stdio.h> int main() { int sum = 0; for (int i = 2; i <= 30; i++) { if (i % 2 == 0) { sum = sum + i; } } printf("The sum of even integers from 2 to 30 = %d\n", sum); return 0; } ``` ## Question 33 **By using `while` and `do-while` statements, write a C program that reads many integer numbers, and then print the number of odd and even integers. (The program stops when entering a number higher than or equal to 1000).** ```c #include <stdio.h> int main() { int num, neven = 0, nodd = 0; printf("Enter integer numbers: \n"); // Using while loop while (1) { scanf("%d", &num); if (num < 1000) { if (num % 2 == 0) { neven = neven + 1; } else { nodd = nodd + 1; } } else { break; } } printf("Number of even numbers: %d\n", neven); printf("Number of odd numbers: %d\n", nodd); return 0; } ``` ## Question 34 **Write a C program to print all natural numbers in reverse (from n to 1) (Using `while` Loop).** ```c #include <stdio.h> int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); while (n >= 1) { printf("%d\n", n); n--; } return 0; } ``` ## Question 35 **Write a C program to calculate factorial of a number.** ```c #include <stdio.h> int main() { int i, num, fact = 1; printf("Enter a positive integer: "); scanf("%d", &num); for (i = num; i >= 1; i--) { fact = fact * i; } printf("Factorial of %d: %d\n", num, fact); return 0; } ``` ## Question 36 **Write a C program to check whether a number is prime number or not.** ```c #include <stdio.h> int main() { int i, n, flag = 0; printf("Enter a positive integer: "); scanf("%d", &n); // 1 is not a prime number if (n == 1) { printf("%d is not prime\n", n); } else { // Iterate from 2 to n/2 for (i = 2; i <= n / 2; ++i) { if (n % i == 0) { flag = 1; break; } } // If flag is 0, then the number is prime if (flag == 0) { printf("%d is a prime number\n", n); } else { printf("%d is not a prime number\n", n); } } return 0; } ``` ## Question 37 **Write a C program to find sum of all prime numbers between 1 to n** ```c #include <stdio.h> int main() { int i, m, end, prime, sum = 0; printf("Find sum of all prime numbers between 1 to: "); scanf("%d", &end); for (i = 2; i <= end; i++) { prime = 1; for (m = 2; m <= i / 2; m++) { if (i % m == 0) { prime = 0; break; } } if (prime == 1) { sum += i; } } printf("Sum of all prime numbers between 1 to %d = %d\n", end, sum); return 0; } ``` ## Question 38 **Write a C program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The table should include rows for all temperatures between 0 and 100 degrees Celsius that are multiples of 10 degrees Celsius, Include appropriate headings on your columns. The formula for converting between degrees Celsius and degrees Fahrenheit can be found on the Internet.** ```c #include <stdio.h> int main() { int c, f; printf("C degree: F degree\n"); for (c = 0; c <= 100; c = c + 10) { f = (c * 9 / 5) + 32; printf("%d: %d\n", c, f); } return 0; } ```