In: Computer Science
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the user types the sentinel value -1 to indicate “end of data entry.”
Pseudocode:
The informal language to develop an algorithm used by the programmer is known as pseudocode. This describes the text-based step by step solution to the given problem that is intended for human reading.
a)
The program source code is given below:
#include <stdio.h>
int main()
{
//variable declaration
int a, b, c, mul;
//get user input
printf("Enter three numbers: ");
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
//calculate Multiplication
mul = a*b*c;
//display Multiplication
printf("Multiplication is = %d", mul);
return 0;
}
OUTPUT:
Enter three numbers: 5 6 8
Multiplication is = 240
The pseudocode is given below:
Step 1: Declare variable mul, a, b, and c
Step 2: Input three numbers
Step 3: mul = a * b * c
Step 4: Display mul on the computer screen
The flowchart is given below:
b)
The program source code is given below:
#include <stdio.h>
int main()
{
//variable declaration
int a, b;
//get user input
printf("Enter two numbers: ");
scanf("%d", &a);
scanf("%d", &b);
if(a<b)
printf("%d is smaller than %d", a, b);
else
printf("%d is smaller than %d", b, a);
return 0;
}
OUTPUT:
Enter two numbers: 10 20
10 is smaller than 20
The pseudocode is given below:
Step 1: Declare variable a, b
Step 2: Input two numbers
Step 3: if a<b
Step 3.1: Display "a is smaller than b"
Step 3.2: Else
Step 3.3: Display "b is smaller than a"
Step 4: Stop
The flowchart is given below:
c)
The program source code is given below:
#include <stdio.h>
int main()
{
//array declaration
int arr[100];
int num, size = 0;
float avg=0;
//while loop
while(1)
{
//get user input
scanf("%d", &num);
if(num==-1)
break;
arr[size++]=num;
avg = avg + num;
}
//calculate average
avg = (float) avg / size;
//display average
printf("Average = %.4f", avg);
return 0;
}
OUTPUT:
10
20
30
-1
Average = 20.0000
The pseudocode is given below:
Step 1: Start
Step 2: Declare arr[100], num, size, avg
Step 3: Initialize variable size = 0, avg = 0
Step 4: while(1)
Step 4.1: Input num from the user
Step 4.2: if num == -1
Step 4.2.1: break
Step 4.3: arr[size++] = num
Step 4.4: avg = avg + num
Step 5: avg = (float) avg / size
Step 6: Display avg on the computer screen
Step 7: Stop
The flowchart is given below: