Design a program in C using the method of call by value together with the “switc
ID: 3601433 • Letter: D
Question
Design a program in C using the method of call by value together with the “switch” command, which, when executed, will compute either the triangular number tNo(n) defined by the formula tNo(n)=1+2+…+n or the factorial fNo(n) defined the formula fNo(n)=1.2.3…n, with n being a user-specified positive integer number. Two prototype functions called “triangular” and “factorial” are to be employed in your call by value method.
Your program is to operate over an infinite loop unless the user inputs a value of 0 for n, in which event the whole process is terminated. The four programs specified below are to be designed, followed by the drawing of the flowchart for the first program. The difference between the four programs lies in whether a prototype function is placed before or after the main function, and whether the outputting is done from within the prototype function or from within the main function as specified below:
PROGRAM 1: Both prototype functions are placed before the main function, and the outputting to the console is done from within the prototype functions.
PROGRAM 2: Both prototype functions are placed before the main function, and the outputting to the console is done from within the main function.
PROGRAM 3: Both prototype functions are placed after the main function, and the outputting to the console is done from within the main function.
PROGRAM 4: The prototype function triangular is placed before the main function and the prototype function factorial is placed after the main function, with outputting to the console done from within the main function.
FLOWCHART: Draw the flowchart corresponding to PROGRAM 1.
Explanation / Answer
Program 1
#include<stdio.h>
void triangular(int n){
int i;
int sum = 0;
for (i=1; i<=n; i++){
sum = sum + i;
}
printf("%d ",sum);
}
void factorial(int n){
int i;
int prod = 1;
for (i=1; i<=n; i++){
prod = prod * i;
}
printf("%d ",prod);
}
int main(){
int n;
printf("Enter n : ");
scanf("%d",&n);
triangular(n);
factorial(n);
return 0;
}
Program 2
#include<stdio.h>
int triangular(int n){
int i;
int sum = 0;
for (i=1; i<=n; i++){
sum = sum + i;
}
return sum
}
int factorial(int n){
int i;
int prod = 1;
for (i=1; i<=n; i++){
prod = prod * i;
}
return prod
}
int main(){
int n;
printf("Enter n : ");
scanf("%d",&n);
printf("%d %d ",triangular(n),factorial(n));
return 0;
}
Program 3
#include<stdio.h>
int triangular(int n);
int factorial(int n);
int main(){
int n;
printf("Enter n : ");
scanf("%d",&n);
printf("%d %d ",triangular(n),factorial(n));
return 0;
}
int triangular(int n){
int i;
int sum = 0;
for (i=1; i<=n; i++){
sum = sum + i;
}
return sum
}
int factorial(int n){
int i;
int prod = 1;
for (i=1; i<=n; i++){
prod = prod * i;
}
return prod
}
Program 4
#include<stdio.h>
int triangular(int n){
int i;
int sum = 0;
for (i=1; i<=n; i++){
sum = sum + i;
}
return sum
}
int factorial(int n);
int main(){
int n;
printf("Enter n : ");
scanf("%d",&n);
printf("%d %d ",triangular(n),factorial(n));
return 0;
}
int factorial(int n){
int i;
int prod = 1;
for (i=1; i<=n; i++){
prod = prod * i;
}
return prod
}
flowchart for program1
start
|
|
V
input n
|
|
V
call triangular---->output the value
|
|
V
call factorial----->output the value
|
|
V
stop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.