So my program basically allows the user to enter a positive integer and then a menu will pop up like this:
Enter a positive integer: 11
1. Is the given number an even or an odd number
2. Find out if this number is a prime number or not
3. Calculate the factorial of this number
4. Find the sum of the numbers from 0 to this number
5. Return and integer showing the number in base of 8
6. Exit
Your choice: .....
Then the user can pick which ever function they want to do and the program will print out the answer.
This program works perfectly fine on Dev C++, but when I copy the code to the Unix Server for my school using Putty, I get this weird issue for the sum function. Basically, when I run the program and choose 4 at the beginning I get the right answer for sum, 66(using input = 11). But if i go through each choice, starting from one and then go to four, i get 78. This was not an issue that all on Dev C++. Anyone know the reason?
Heres the code
Code:#include <stdio.h> void menu(); int odd_even(int); int prime(int); int factorial(int); int sum(int); int base(int); int main() { int input; int choice = 0; printf("Enter a positive integer: "); scanf("%d", &input); while (choice != 6 && input > 0) { menu(); printf("Your choice:"); scanf ("%d", &choice); switch (choice) { case 1: odd_even(input); break; case 2: prime(input); break; case 3: printf("The factorial of %d is %d", input, factorial(input)); break; case 4: printf("Sum of numbers from 0 to %d is %d", input, sum(input)); break; case 5: printf("%d in base 8 is %d", input, base(input)); break; case 6: printf("Goodbye!\n"); break; default: /* If the value entered is invalid, prompt the user to enter again */ printf("\nInvalid Input. Try Again !\n"); } } return 0; } void menu() { printf("\n1. Is the given number even or odd \n"); printf("2. Find out if this number is prime or not \n"); printf("3. Calculate the factorial of this number \n"); printf("4. Find the sum of the numbers from 0 to this number \n"); printf("5. Return an integer showing the number in base of 8\n"); printf("6. Exit\n"); } int odd_even(int input) { if (input%2 == 0) printf(" %d is an even number", input); else printf("%d is an odd number", input); return 0; } int prime(int input) { int divisor; for (divisor = 2; divisor <= input; divisor++) { if (input%divisor != 0 && input != divisor) printf("%d is a prime number", input); else printf("%d is not a prime number", input); break; } return 0; } int factorial(int input) { int count; int result = 1; for (count = 1; count <= input; count++) result = result * count; return result; } int sum(int input) { int count = 0; int sum; for (count = 0; count <= input; count++) sum = count + sum; return sum; } int base(int input) { int digits, power, result; power = 1; /*Value of digit position 0*/ result = 0; /*Where the decimal result will go*/ while(input > 0) { digits = input % 8; /*Get right most digit*/ result += digits * power; /*If 1, add its position value to the result*/ power *= 10; /*Increase the value of the next digit position*/ input /= 8; /*Knock off the right most digit*/ } return result; }



LinkBack URL
About LinkBacks



