0
Everything is fine in this code but when I am choosing choice number 1 it is showing invalid choice I feel so irritate now
#include<stdio.h> int main(){ int choice,a,b,s; printf("\n1.addition"); printf("\n2.odd-even"); printf("\n3.printing n numbers"); printf("\n\n enter your choice:"); scanf("%d",&choice); switch(choice){ case1: printf("enter two numbers:"); scanf("%d,%d",&a,&b); s=a+b; printf("\n sum is %d",s); break; case 2: printf("enter a number:"); scanf("%d",&a); if(a/2==0){ printf("even number:"); } else{ printf("odd number:"); } break; case 3: printf("enter a number: "); scanf("%d",&a); for(b=1; b<=a;b++) printf("%d",b); break; default: printf("invalid choice:"); break; } return 0; }
3 Answers
+ 2
Label for case 1 was missing a space. It should be
case 1: // not case1:
To verify <N> is an even number, modulo <N> by 2, and ensure that the result was zero (no remainder from the division operation).
if( a % 2 == 0 ) // not a / 2
+ 1
This code is hard to read cause it's just text with no indentation. Can you link the code from the Code Playground instead?
+ 1
Thanx alot