gpt4 book ai didi

c - 使用 do while 的基于菜单的程序

转载 作者:行者123 更新时间:2023-11-30 21:21:41 28 4
gpt4 key购买 nike

#include <stdio.h>
#include <conio.h>

void main()
{
int choice,cala,calb,add,product,subtract,mod,bininput, c, k,perfectinput, perfecttotal, perfecti,primei, primeinput, flagprime,factinput,factorial,facti;
float divide;
do
{
printf("********** Main Menu **********\n(1).Calculator\n(2).Decimal to Binary convertor\n(3).Perfect number finder\n(4).Prime number or composite number\n(5).Factorial Calculator\n(6).To exit\n");
scanf("%d",choice);

switch (choice)
{
case 1:
clrscr();
printf("********** Calculator **********\n\nEnter numbers:-\n");
scanf("%d%d" , &cala,&calb);

add = cala + calb;
subtract = cala - calb;
product = cala * calb;
divide = cala / (float)calb;
mod = cala % calb;

printf("Sum = %d\n",add);
printf("Sub = %d\n",subtract);
printf("product = %d\n",product);
printf("divide = %2f\n",divide);
printf("mod = %d\n",mod);
printf("(0). Return to main menu");
printf("\n");
break;

case 2:
clrscr();

printf("********** Decimal to Binary convertor **********\n\nEnter an integer in decimal number system:-");
scanf("%d", &bininput);

printf("%d in binary number system is:\n", bininput);
printf("\n");

for (c = 10; c >= 0; c--)
{
k = bininput >> c;

if (k & 1)
printf("1");
else
printf("0");
}
printf("\n(0). Return to main menu\n");
printf("\n");
printf("\n");
break;

case 3:
clrscr();

printf("********** Perfect number finder **********\n");
printf("Enter a number:- ");
scanf("%d" ,&perfectinput);
perfecttotal=0;
perfecti=1;
for(perfecti=1;perfecti < perfectinput;perfecti++)
{
if(perfectinput%perfecti == 0)
perfecttotal=perfecttotal+perfecti;
}
if(perfecttotal==perfectinput)
printf("%d is a perfect number\n\n\a\a",perfectinput);
else
printf("%d is not a perfect number\n\n\a",perfectinput);
printf("(0). Return to main menu\n");
break;

case 4:
clrscr();


printf("********** Prime number or composite number **********\n");
printf("Enter a number:- ");
scanf("%d" ,&primeinput);
flagprime=0;
primei=2;
while(primei <= primeinput/2)
{
if (primeinput%primei == 0)
{
flagprime=1;
break;
}
primei++;
}
if(flagprime==0)
printf("%d is a prime number\n\n",primeinput);
else
printf("%d is a composite number\n\n",primeinput);
printf("(0). Return to main menu\n");
break;

case 5:
clrscr();

printf("********** Factorial Calculator **********\n");
printf("Enter a number:- ");
scanf("%d" ,&factinput);
factorial=1;
for(facti=factinput;facti>0;facti--)
{
factorial=factorial*facti;
}
printf("Factorial of %d = %d\n\n",factinput,factorial);
printf("(0). Return to main menu\n");
break;

case '6':
clrscr();
printf("Program terminated.\nPress any key to exit");
break;
default: printf("wrong input");
}
}while(choice != 6);
printf("bye");
getch();
}

最佳答案

你忘了&...

scanf("%d",&choice);

关于c - 使用 do while 的基于菜单的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20794511/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com