Originally Posted by
itsme86
Could you post the code?
Yea sure
Code:
#include <stdio.h>
#include <math.h>
int display_menu(void);
double compute_factorial(n);
int main()
{
int n, choice;
double v, c;
printf("Enter a whole number, n, between 1-20: ");
scanf_s("%d", &n); //I'm still not sure why I have to do scanf_s.
if(n < 1 || n > 20)
{
do
{
printf("Invalid option: try again. ");
scanf_s("%d", &n);
}while(n < 1 || n > 20);
}
display_menu();
printf("\nWhat would you like to do? ");
scanf_s("%d", &choice);
if(choice < 1 || choice > 6)
{
do
{
printf("Invalid option: try again. ");
scanf_s("%d", &choice);
}while(choice < 1 || choice > 6);
}
switch(choice)
{
case 1:
for(c = 1; c <= n; c++)
{
printf("%.0lf ", c);
}
break;
case 2:
for(c = n, v = 0; c >= 1; c--)
{
v += c;
}
printf("The sum of all whole numbers 0 to n is: %.0lf", v);
break;
case 3:
c = 2;
v = 0;
do
{
v += (c * c);
c += 2;
}while(c <= n);
printf("The sum of the square of all the even numbers 0 to n is: %.0lf", v);
break;
case 4:
c = 0;
v = 0;
do
{
v += (1 / c);
c++;
}while(c <= n);
printf("The sum of 1 + 1/2 + ... 1/n. is: %lf", v);
break;
case 5:
v = compute_factorial(n);
printf("n! is: %.0lf", v);
break;
case 6:
printf("Program will end now.");
return 0;
break;
}
getch();
}
int display_menu(void)
{
printf("\nMenu of Options:\n");
printf("1.Display numbers 1 to n.\n");
printf("2.Add up all the numbers 1 to n.\n");
printf("3.Add up square of all the even numbers 0 to n.\n");
printf("4.Add up 1 + 1 /2 + ... 1/n.\n");
printf("5.Compute n!\n");
printf("6.Quit\n");
return 0;
//Input: None.
//Output: None.
//Task: Display a "Menu of Options".
}
double compute_factorial(n)
{
int c;
double v;
for(c = 1, v = 1; c <= n; c++)
{
v *= c;
}
return v;
//Input Parameter: A whole number
//Output: Computed n!
//Task: Compute N!
}
case 4 is what I am having problems with. It ends up saving 1.INF00 for v. :S