Question #1
Study the following C program and answer
the below questions
Hint: you might
run it in Dev C++ before starting answering the questions!
a)
Show a sample output of the
program.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b)
Extract a function declaration
(prototype) for:
1)
void functions with
void argument
2)
void function with a single input argument
………………………………………………………………………………………………………………… |
3)
function with a single input
argument and return
………………………………………………………………………………………………………………… |
4)
a recursive function
………………………………………………………………………………………………………………… |
c)
What is the difference between
normal functions and recursive functions?
|
|
|
d)
How many times are the function
print_stars() called in the program? (mention the line number of each call).
|
|
|
#include<stdio.h>
void print_stars();
void option_list();
void divisors(int n);
int summation(int s);
int factorial (int n) ;
int main(void) {
int number , result , op ;
char
flag;
printf("Enter
an integer number > ");
scanf(
"%d" , &number );
do{
option_list();
scanf("%d"
, &op);
switch(op) {
case
1:
divisors(number);
break;
case
2:
result
= summation(number) ;
printf("Summation
= %d\n" , result);
break;
case
3:
result
= factorial(number) ;
printf("Factorial
= %d\n" , result);
break;
default:
printf("\nERROR:
%d is unrecognized option!!! \n" , op);
}
print_stars();
printf("Do
you want to check another function? (Y/N)> ");
scanf("
%c" , &flag);
}
while (flag == 'Y' || flag == 'y');
return
0;
}
void option_list(){
print_stars();
printf("1:
Find the divisors \n");
printf("2:
Compute the summuation\n");
printf("3:
Compute the factorial \n");
print_stars();
printf("Please
enter your choice: ");
}
void print_stars(){
int
i;
for(i
= 1 ; i <= 25 ; i++)
printf("*");
printf("\n");
}
void divisors(int n) {
int
i;
printf("Divisors
of %d are\n",n);
for(i=1;i<=n;i++)
if(n%i==0)
printf("
%d",i);
printf("\n");
}
int factorial (int n){
int i , fact = 1 ;
for
(i = 2 ; i <= n ; i++)
fact
*= i;
return
fact;
}
int summation(int s){
if (s == 0)
return
0;
else
return s + summation(s-1);
}
e)
What does each function do?
Hint:
Write a simple description for each of the user-defined functions.
void
print_stars(): |
|
Related Questions in others category
Get Higher Grades Now
Tutors Online
Get Free Quote! 309 Experts Online |