Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a program in C using the SWITCH command together with a runmode operator

ID: 3686570 • Letter: D

Question

Design a program in C using the SWITCH command together with a runmode operator and four prototype functions (called triangular, mtriangular, factorial, and mfactorial ) which will compute any one of the following quantities for a user-specified positive integer number n:

tNo = 1+2+3+…+n
mtNo = 12 + 22 +32 +… +n2
fNo = 1.2.3…n
mfNo = 12.22.32…n2.

The “m” in mtNo stands for modified triangular number, and in mfNo for modified factorial number.

You are to design four programs specified below which will operate in an infinite loop unless the user inputs a value of 0 for n, in which event the whole process is terminated. The outputting consist of a display on the console in a table format of the values of n and the corresponding value of one of the four functions, namely, tNo, mtNo, fNo and mfNo. 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: All four prototype functions are placed before the main function, and the outputting to the console is done from within the prototype functions.
PROGRAM 2:   All four prototype functions are placed before the main function, and the outputting to the console is done from within the main function.
PROGRAM 3:   All four prototype functions are placed after the main function, and the outputting to the console is done from within the main function.
PROGRAM 4:   The two prototype functions triangular and mtriangular are placed before the main function and the remaining two prototype functions factorial and mfactorial are placed after the main function, with outputting to the console done from within the main function.

THE PROGRAMS HAVE TO BE DONE IN MICROSOFT VISUAL C++

Explanation / Answer

Program 1:

#include<stdio.h>
#include<math.h>

// all required function defination

void tNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i;
   printf("tNo(%d): %d ",n, sum);
}

void mtNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i*i;
   printf("mtNo(%d): %d ",n, sum);
}

void fNo(int n){
  
   int fact = 1;
   int i;
   for(i=1; i<=n; i++)
       fact = fact*i;
   printf("fNo(%d): %d ",n, fact);
}

void mfNo(int n){
  
   int total = 1;
   int i;
   for(i=1; i<=n; i++)
       total = total*i*i;
   printf("mfNo(%d): %d ",n, total);
}

int main(){
  
   int n;
  
   while(1){
      
       printf("Enter value of n: ");
       scanf("%d", &n);
      
       if(n==0)
           break;
       tNo(n);
       mtNo(n);
       fNo(n);
       mfNo(n);
   }
   return 0;
}
/*
Output:
Enter value of n: 5
tNo(5): 15
mtNo(5): 55
fNo(5): 120
mfNo(5): 14400
Enter value of n: 4
tNo(4): 10
mtNo(4): 30
fNo(4): 24
mfNo(4): 576
Enter value of n: 2
tNo(2): 3
mtNo(2): 5
fNo(2): 2
mfNo(2): 4
Enter value of n: 0

*/


Program 2:

#include<stdio.h>
#include<math.h>

// all required function defination

int tNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i;
   return sum;
}

int mtNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i*i;
   return sum;
}

int fNo(int n){
  
   int fact = 1;
   int i;
   for(i=1; i<=n; i++)
       fact = fact*i;
   return fact;
  
}

int mfNo(int n){
  
   int total = 1;
   int i;
   for(i=1; i<=n; i++)
       total = total*i*i;
   return total;
  
}

int main(){
  
   int n;
  
   while(1){
      
       printf("Enter value of n: ");
       scanf("%d", &n);
      
       if(n==0)
           break;
       printf("tNo(%d): %d ",n, tNo(n));
       printf("mtNo(%d): %d ",n, mtNo(n));
       printf("fNo(%d): %d ",n, fNo(n));
       printf("mfNo(%d): %d ",n, mfNo(n));
   }
   return 0;
}


Program 3:

#include<stdio.h>
#include<math.h>

//function prototype

int tNo(int n);
int mtNo(int n);
int fNo(int n);
int mfNo(int n);

int main(){
  
   int n;
  
   while(1){
      
       printf("Enter value of n: ");
       scanf("%d", &n);
      
       if(n==0)
           break;
       printf("tNo(%d): %d ",n, tNo(n));
       printf("mtNo(%d): %d ",n, mtNo(n));
       printf("fNo(%d): %d ",n, fNo(n));
       printf("mfNo(%d): %d ",n, mfNo(n));
   }
   return 0;
}

//function defination

int tNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i;
   return sum;
}

int mtNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i*i;
   return sum;
}

int fNo(int n){
  
   int fact = 1;
   int i;
   for(i=1; i<=n; i++)
       fact = fact*i;
   return fact;
  
}

int mfNo(int n){
  
   int total = 1;
   int i;
   for(i=1; i<=n; i++)
       total = total*i*i;
   return total;
  
}


Program 4:

#include<stdio.h>
#include<math.h>

//function prototype
int fNo(int n);
int mfNo(int n);

int tNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i;
   return sum;
}

int mtNo(int n){
  
   int sum = 0;
   int i;
   for(i=1; i<=n; i++)
       sum = sum + i*i;
   return sum;
}

int main(){
  
   int n;
  
   while(1){
      
       printf("Enter value of n: ");
       scanf("%d", &n);
      
       if(n==0)
           break;
       printf("tNo(%d): %d ",n, tNo(n));
       printf("mtNo(%d): %d ",n, mtNo(n));
       printf("fNo(%d): %d ",n, fNo(n));
       printf("mfNo(%d): %d ",n, mfNo(n));
   }
   return 0;
}

//function defination

int fNo(int n){
  
   int fact = 1;
   int i;
   for(i=1; i<=n; i++)
       fact = fact*i;
   return fact;
  
}

int mfNo(int n){
  
   int total = 1;
   int i;
   for(i=1; i<=n; i++)
       total = total*i*i;
   return total;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote