Switch case missing in C program
#include<stdio.h>
#include<conio.h>
void inversion(void);
void ways(void);
void prime(void);
void power(void);
void fibonacci(void);
void main(void)
{
char choice;
printf("Enter a choice:\n 1.Perform inversion of digits \n 2.calculate all the ways that a positive number can be get by adding \n 3.Calculate prime numbers in a range\n 4.Calculate power of a number. \n 5.generate a particular numebr of fibonacci");
choice==getche();
switch(choice)
{
case 1: inversion();
break;
case 2: ways();
break;
case 3: prime();
break;
case 4:power();
break;
case 5:fibonacci();
break;
}//switch ends
//printf("Do you want to perform this once more? (Y?N):");
//}
//while (choice=='y'||choice=='Y');
getch();
}
void inversion(void)
{
clrscr();
int num,i,x,y;
printf("Enter a 4 digit number:");
scanf("%d",&num);
for(i=1;i<=4;i++)
{
x=num%10;
y=num/10;
printf("%d",x);
num=y;
}// for ends
getch();
}
void ways(void)
{ clrscr();
int num,i,j;
printf("Enter the number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
if(i+j==num)
printf("%d+%d=%d",i,j,num);
getch();
}
void prime(void)
{
int num1,num2,i;
printf("Enter a range separated by space:");
scanf("%d %d",num1,num2);
for(i=2;i<num2;i++)
{ if(num1%i!=0)//is prime
printf("%d",num1);
}//for ends
getch();
}//prime ends
void power(void)
{
int num,index,i,result=1;
printf("Enter the number and its index (eg.2^3)");
scanf("%d %d",&num,index);
for(i=1;i<=index;i++)
result=num*result;
printf("%d",result);
}//power function ends
void fibonacci(void)
{
int num,x=1,y=1,i,z;
printf("Enter the term you want to find in fibonacci series:");
scanf("%d",&num);
if (num==1)
printf("it is the 1st & 2nd term");
for(i=1;i<=num+2;i++)开发者_JS百科
{
z=x+y;
if (i==num)
printf("%d",z);
x=y;
y=z;
}
printf("%d");
}//fibonacci func ends
There are a lot of problems in your code:
# I would like to suggest you to use getchar() instead of getche() for a character input.
# Now you are using a charcter for numeric options which you could have avoided by defining your choice variable as integer:
int choice;
scanf (%d, &choice);
# also if you were using char choice;
you are having cases as:
case 1: inversion();
break;
while your choice variable was char so you should have case like:
case '1': inversion();
anyways if you will define choice as int then your code should work fine.
or you can change your cases also with case values in single quotes.
精彩评论