题目内容:
从键盘输入6位仅由数字0~9组成的密码。用户每输入一个密码并按回车键后,程序给出判断:如果是数字,则原样输出该数字,并提示用户目前已经输入了几位密码,同时继续输入下一位密码;否则,程序提示"error",并让用户继续输入下一位密码。直到用户输入的密码全部是数字为止。
程序的运行结果示例:
Input your password:
1↙
1, you have enter 1-bits number
6↙
6, you have enter 2-bits number
a↙
error
d↙
error
4↙
4, you have enter 3-bits number
6↙
6, you have enter 4-bits number
8↙
8, you have enter 5-bits number
2↙
2, you have enter 6-bits number
#include <stdio.h>
int main()
{
char array[6];
printf("Input your password:\n");
for(int i=0;i<6;i++)
{
scanf("%c",&array[i]);
getchar();
if('0'<=array[i]&&array[i]<='9')
{
printf("%c, you have enter %d-bits number\n",array[i],i+1);
}
else
{
printf("error\n");
}
}
return 0;
}
题目内容:
从键盘输入一系列正整数,输入-1表示输入结束(-1本身不是输入的数据)。编写程序判断输入数据中奇数和偶数的个数。如果用户输入的第一个数据就是-1,则程序输出"over!"。否则。用户每输入一个数据,输出该数据是奇数还是偶数,直到用户输入-1为止,分别统计用户输入数据中奇数和偶数的个数。
程序运行结果示例1:
Please enter the number:
1↙
1:odd
5↙
5:odd
8↙
8:even
9↙
9:odd
12↙
12:even
17↙
17:odd
-1↙
The total number of odd is 4
The total number of even is 2
程序运行结果示例2:
Please enter the number:
-1↙
over!
The total number of odd is 0
The total number of even is 0
#include <stdio.h>
int main()
{
int number=0;
int count=0;
int x=0;
int y=0;
printf("Please enter the number:\n");
while(number!=-1)
{
scanf("%d",&number);
if(count==0&&number==-1)
{
printf("over!\n");
break;
}
if(count>0&&number==-1)
break;
if(number%2==0)
{
printf("%d:even\n",number);
x++;
}
else if(number%2==1)
{
printf("%d:odd\n",number);
y++;
}
count++;
}
printf("The total number of odd is %d\n",y);
printf("The total number of even is %d\n",x);
return 0;
}
#include <stdio.h>
int main()
{
int odd=0,even=0,input;
printf("Please enter the number:\n");
for(scanf("%d",&input);input!=-1;scanf("%d",&input)){
getchar();
if(input%2){
printf("%d:%s\n",input,"odd");
++odd;
} else{
printf("%d:%s\n",input,"even");
++even;
}
}
printf("The total number of odd is %d\nThe total number of even is %d\n",odd,even);
}