i need to produce a program which display Odd and Even number. User must be able to enter how many time the program loop.
example:
Enter loop: 2
Enter Selection: o
Enter Input 1: 1
Enter Input 2: 10
output:
number: 1 3 5 7 9
total: 25
Enter Selection: e
Enter Input 1: 1
Enter Input 2: 11
output:
number: 2 4 6 8 10
total: 30
my program always terminte after it runs once. where do i make the mistake ?Code:int main() { int i, total=0, input1, input2, counter; char selection; printf("Enter loop: "); scanf("%d", &counter); for(i=0; i<counter; i++) { printf("\nEnter selection: "); scanf("%s", &selection); printf("\nEnter input 1: "); scanf("%d", &input1); printf("\nEnter input 2: "); scanf("%d", &input2); switch (selection) { case 'e': if(input1 % 2 == 0) { printf("\nEven Number: "); while(input1<=input2) { printf("%d ",input1 ); total = total + input1; input1+=2; } printf("\nTotal: %d", total); printf("\n"); } else { input1++; printf("\nEven Number: "); while(input1<=input2) { printf("%d ",input1 ); total = total + input1; input1+=2; } printf("\nTotal: %d", total); printf("\n"); } break; case 'o': if(input1 % 2 != 0) { printf("\nOdd Number: "); while(input1<=input2) { printf("%d ",input1 ); total = total + input1; input1+=2; } printf("\nTotal: %d", total); printf("\n"); } else { input1++; printf("\nEven Number: "); while(input1<=input2) { printf("%d ",input1 ); total = total + input1; input1+=2; } printf("\nTotal: %d", total); printf("\n"); } break; } } system("pause"); return 0; }
Thank You



LinkBack URL
About LinkBacks


