When I execute this code ,it accepts 2 nos,prints the msg enter your choice and terminates..
#include<stdio.h>
int main()
{
char ch;
int a,b;
printf("enter 2 nos :");
scanf("%d %d",&a,&b);
printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);
switch(ch)
{
case '1':
printf("sum : %f",a+b);
break;
case '2':
printf("diff : %f",a-b);
break;
case '3':
printf("mul : %f",a*b);
break;
}
return 0;
}
But If I accept choice before accepting nos. then it works.. As follows.. Can any any body tell me d reason 4 it.. and solution as well.. Thanx in advance..
#include<stdio.h>
int main()
{
char ch;
int a,b;
printf("\nenter your choice:");
ch=getchar();
printf("%c",ch);
printf("enter 2 nos :");
scanf("%d %d",&a,&b);
switch(ch)
{
case '1':
printf("sum : %f",a+b);
break;
case '2':
printf("diff : %f",a-b);
break;
case '3':
printf("mul : %f",a*b);
break;
}
return 0;
}