如何用c语言分别输出个位、十位和百位的数字

如何用c语言分别输出个位、十位和百位的数字,第1张

用c语言分别输出个位十位和百位的数字,可以根据下面步骤进行,c语言作为一种程序代码,因此在输入时绝对不能弄错任何符号。

#include<stdioh>

int main()

{int x;

scanf("%d",&x);

printf("%d的百位=%d,十位=%d,个位=%d\n",x,x/100,x/10%10,x%10);

return 0;

}

扩展资料

#include

main()

{

int a;

printf("请输入一个三位数:");

scanf("%d",&a);

printf("百位数:%d\n",a/100);

printf("十位数:%d\n",(a%100)/10);

printf("百位数:%d\n",a%10);

}

参考资料:

百度百科 printf

百度百科 c语言

data segment

;定义please等提示信息

str db 20

db 50h,7,0,0

db 6ch,7,0,1

db 65h,7,0,1

db 61h,7,0,1

db 73h,7,0,1

db 65h, 7,0,1

db 70h,7,1,-10

db 72h,7,0,1

db 65h,7,0,1

db 73h,7,0,1

db 73h,7,0,1

db 53h,7,0,2

db 6bh,7,0,2

db 65h,7,0,1

db 79h,7,0,1

db 65h,7,0,2

db 78h,7,0,1

db 69h,7,0,1

db 74h,7,0,1

db 21h,7,0,1

;定义0-9十个图形数字

s0 db 6

db 5fh,7,0,0

db 7ch,7,1,-1

db 7ch,7,1,0

db 5fh,7,0,1

db 7ch,7,0,1

db 7ch,7,-1,0

sy db 2

db 7ch,7,1,0

db 7ch,7,1,0

s2 db 5

db 5fh,7,0,0

db 7ch,7,1,1

db 5fh,7,0,-1

db 7ch,7,1,-1

db 5fh,7,0,1

s3 db 5

db 5fh,7,0,0

db 7ch,7,1,1

db 5fh,7,0,-1

db 7ch,7,1,1

db 5fh,7,0,-1

s4 db 4

db 7ch,7,1,0

db 5fh,7,0,1

db 7ch,7,1,1

db 7ch,7,-1,0

s5 db 5

db 5fh,7,0,0

db 7ch,7,1,-1

db 5fh,7,0,1

db 7ch,7,1,1

db 5fh,7,0,-1

s6 db 6

db 5fh,7,0,0

db 7ch,7,1,-1

db 5fh,7,0,1

db 7ch,7,1,1

db 5fh,7,0,-1

db 7ch,7,0,-1

s7 db 3

db 5fh,7,0,0

db 7ch,7,1,1

db 7ch,7,1,0

s8 db 7

db 7ch,7,1,0

db 5fh,7,-1,1

db 7ch,7,1,1

db 5fh,7,0,-1

db 7ch,7,1,1

db 5fh,7,0,-1

db 7ch,7,0,-1

s9 db 6

db 7ch,7,1,0

db 5fh,7,-1,1

db 7ch,7,1,1

db 5fh,7,0,-1

db 7ch,7,1,1

db 5fh,7,0,-1

s11 db 2

db 03h,7,1,0

db 03h,7,1,0

data ends

stack segment

db 200 dup (0)

stack ends

code segment

assume cs:code,ss:stack,ds:data

start:

mov ax,data

mov ds,ax

mov ah,0 ;设置显示方式

mov al,4

int 10h

mov ah,0bh ;置彩色调板

mov bh,0

mov bl,1

int 10h

mov ah,0bh ;置彩色调板

mov bh,1

mov bl,4

int 10h

mov ah,2 ;取系统时间

int 1ah

mov ax,0

mov bx,0

mov ah,ch ;取系统时间

mov al,cl ;保存分钟

mov bh,dh ;保存秒

mov bl,dl ;保存百分之一秒

tt:

push ax

push bx

mov ah,0

mov al,4

int 10h

mov ah,0bh

mov bh,0

mov bl,1

int 10h

mov ah,0bh

mov bh,1

mov bl,4

int 10h

MOV AH,1 ;读键盘缓冲区字符

INT 16h

JZ ww

MOV AH,8 ;从键盘输入字符

INT 21h

CMP AL,'s' ;输入s,结束程序

JE tu

ww: pop bx

pop ax

call xian ;调用显示子程序

push ax

qu: mov ah,2 ;取系统时间

int 1ah

cmp bh,dh

je qu

pop ax

inc bh ;对时间进行加1

cmp bh,100

jl tt

mov bh,0

inc al

call beep

cmp al,100

jl tt

mov al,0

inc ah

cmp ah,24

jl tt

mov ah,0

jmp tt

tu: mov ah,4ch

int 21h

ret

xian proc ;对时间值进行处理并显示的子程序

push si

push di

push dx

push cx

push bx

push ax

pop ax

push ax

mov bx,ax

mov ax,0

mov al,bh

mov cl,4

shr al,cl

mov bl,al

mov dh,10

mov dl,2

call ff

pop ax

push ax

mov bx,ax

mov ax,0

mov al, bh

and al,0fh

mov bl,al

mov dh,10

mov dl,6

call ff

mov dh,10

mov dl,10

mov bl,10

call ff

pop ax

push ax

mov cl,4

shr al,cl

mov bl,al

mov dh,10

mov dl,14

call ff

pop ax

push ax

and al ,0fh

mov bl,al

mov dh,10

mov dl,18

call ff

mov dh,10

mov dl,22

mov bl,10

call ff

pop ax

pop bx

push bx

push ax

mov ax,0

mov al,bh

mov cl,4

shr al,cl

mov bl,al

mov dh,10

mov dl,26

call ff

pop ax

pop bx

push bx

push ax

mov al,bh

and al,0fh

mov bl,al

mov dh,10

mov dl,30

call ff

mov dh,0

mov dl,15

mov bl,22

call ff

pop ax

pop bx

pop cx

pop dx

pop di

pop si

ret

xian endp

ff proc ;显示具体数值

push ax

push bx

push cx

push dx

cmp bl,0

je l0

cmp bl,1

je l1

cmp bl,2

je l2

cmp bl,3

je l3

cmp bl,4

je l4

cmp bl,5

je l5

cmp bl,6

je l6

cmp bl,7

je l7

cmp bl,8

je l8

cmp bl,9

je l9

cmp bl,10

je l10

cmp bl,22

je ld

l0: lea di,s0

jmp ty

l1 : lea di,sy

jmp ty

l2: lea di,s2

jmp ty

l3: lea di,s3

jmp ty

l4: lea di,s4

jmp ty

l5: lea di,s5

jmp ty

l6: lea di,s6

jmp ty

l7: lea di,s7

jmp ty

l8: lea di,s8

jmp ty

l9: lea di,s9

jmp ty

l10: lea di,s11

jmp ty

ld : lea di,str

ty: mov ah,0fh ;示方式

int 10h

sub cx,cx

mov cl,[di]

inc di

plot_next: add dh,[di+2]

add dl,[di+3]

mov ah,2

int 10h

mov al,[di]

mov bl,[di+1]

push cx

mov cx,1

mov ah,09

int 10h

pop cx

add di,4

loop plot_next

pop dx

pop cx

pop bx

pop ax

mov cx,100

gg: dec cx

loop gg

ret

ff endp

clear proc

push ax

push bx

push cx

push dx

mov ah,6

mov al,0

mov bh,1

int 10h

pop dx

pop cx

pop bx

pop ax

ret

clear endp

beep proc

push ax

push bx

push cx

push dx

mov al ,10110110b

out 43h,al

mov ax,533h

out 42h,al

mov al,ah

out 42h,al

in al,61h

mov ah,al

or al,03

out 61h,al

sub cx,cx

mov bl,28h

g7: loop g7

dec bl

jnz g7

mov al,ah

out 61h,al

pop dx

pop cx

pop bx

pop ax

ret

beep endp

code ends

end start

#include<stdioh>

#include<stdlibh>

#include<timeh>

main()

{

int i,j,k;

int l,n;

int a=0,b=0,m=0;

int x[4],y[4];

int count=0;

//将数字输入数组x

printf("请输入4个数字:\n");

for(i=0;i<=3;i++) /Imput a number/

{

scanf("%d",x+i);

}

/ 输出数组 x

for(i=0;i<=2;i++)

printf("%d ",x[i]);

/

//机器猜的数

for(n=1000;n<10000;n++)

{

y[0]=n/1000;

y[1]=n%1000/100;

y[2]=n%100/10;

y[3]=n%10;

count++;

printf("机器猜的数为:");

for(i=0;i<=3;i++)

{

printf("%d ",y[i]);

}

//计数A

for(i=0;i<=3;i++)

{

if(x[i]==y[i])

a++;

}

//计数B

for(i=0;i<=3;i++)

{

for(j=0;j<=3;j++)

{

if(x[i]==y[j])

b++;

}

}

if(m<a)

{m=a;}

printf("%d",a);

printf("A");

printf("%d",b);

printf("B");

printf(" \n");

a=0;b=0;

if(m==4)break;

}

printf("共花了%d次猜出结果",count);

}

1、首先打开C-Free50,然后一个空白的C语言页面保存为switchc文件,继续输入一般的C语言的基础格式。

2、C语言的输入语句,首先定义一个整型数值,然后从外面写入这个值。#include <stdioh>main(){int a;printf("请输入一个1-5之间的数值: ");scanf("%d",&a);}这里的scanf() 就是从外面读入一个值给a。

3、要看下输入的值是不是赋值给了a,可以把a的值输出,如下图调试。从下图可以得出,a的值就是输入的值。

4、接着按照获得的值来执行相应的步骤,代码如下图。

5、从系统中输入一个数值,然后switch根据读到的数值判断应该做什么,注意,输入的值必须是定义的值,就比如本程序里面switch里面是一开始定义的aint a;printf("请输入一个1-5之间的数值: ");scanf("%d",&a);switch (a){}调试下程序,看看对不对。

#include"stdioh"

#include"timeh"

#include"windowsh"

#include"mathh"

#include"stdlibh"

int num=5;

int numIntermission=1;

int flashNum=5;

int allNumIntermission=600;

void mainPage()//主界面:欢迎界面、进度条

{

int i;

system("color 9f");

printf("\n\n\n\n\t\t系统加载中,请稍候……\n");

printf(" \n");

printf("\t -------- __@ __@ __@ __@ __~@ \n");

printf("\t ----- _`\<,_ _`\<,_ _`\<,_ _`\<,_ _`\<,_ \n");

printf("\t ---- ()/ () ()/ () ()/ () ()/ () ()/ () \n");

for(i=0;i<69;i++)

{

printf("<");

if(i%5!=0)

{

Sleep(50);

}

else

{

Sleep(i3);

}

}

printf("\n");

printf(" \n");

system("CLS");

system("color d");

printf("\n\n");

printf("\t≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≮≮≮≮≮≮≮≮≮≮≮≮≮≮≮\n\n");

system("color a");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≮≮≮≮≮≮≮≮≮≮≮≮≮≮≮\n\n");

system("color b");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯ 欢迎使用记数游戏 ≮≮≮≮≮≮≮≮≮≮\n\n");

system("color c");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯ ≮≮≮≮≮≮≮≮≮≮\n\n");

system("color d");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯ ≮≮≮≮≮≮≮≮≮≮\n\n");

system("color a");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯ 信息工程系 ≮≮≮≮≮≮≮≮≮≮\n\n");

system("color b");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯ 应用电子(1)班 ≮≮≮≮≮≮≮≮≮≮\n\n");

system("color c");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯ ≮≮≮≮≮≮≮≮≮≮\n\n");

system("color d");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯ 201211≮≮≮≮≮≮≮≮≮≮\n\n");

system("color a");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≮≮≮≮≮≮≮≮≮≮≮≮≮≮≮\n\n");

system("color c");

Sleep(500);

printf("\t≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≮≮≮≮≮≮≮≮≮≮≮≮≮≮≮\n\n");

Sleep(3000);

system("CLS");

system("color b");

printf("\n\t 1 表示退出 \n");

printf("\t\t 表示退出这个游戏 \n");

system("color 9a");

Sleep(1000);

printf("\n\n\t 2 表示游戏开始 \n");

printf("\t\t 表示开始游戏这个游戏 \n");

system("color 9b");

Sleep(1000);

printf("\n\n\t 3 表示游戏设置 \n");

printf("\t\t 目前游戏系统相关设置\n");

printf("\t\t 设置显示数字的个数是多少\n");

printf("\t\t 设置显示数字间间隔的秒数是多少\n");

printf("\t\t 设置显示数字闪动次数是多少\n");

printf("\t\t 设置每次显示全部数字间隔时间秒数是多少\n");

system("color 9c");

Sleep(1000);

printf("\n\n\t 5 表示帮助 \n");

printf("\t\t游戏规则:输入1,表示退出%\n");

printf("\t\t 输入2,表示游戏开始\n");

printf("\t\t 输入3,表示游戏系统设置\n");

printf("\t\t 输入5,表示游戏帮助\n");

printf("\t\t 说明:系统随机生成指定个数的数字,闪现一段时间\n");

printf("\t\t 输入数字必须按照数字出现先后顺序\n");

printf("\t\t 当记忆全对,你就胜出了,否则给鼓励信息\n");

Sleep(5000);

system("CLS");

system("color 9c");

}

void help()

{

printf("\t\t 帮助信息\n");

printf("\t\t游戏规则:输入1,表示退出%\n");

printf("\t\t输入2,表示游戏开始\n");

printf("\t\t输入3,表示游戏系统设置\n");

printf("\t\t输入5,表示游戏帮助\n");

printf("\t\t说明:系统随机生成指定个数的数字,闪现一段时间\n");

printf("\t\t输入数字必须按照数字出现先后顺序\n");

printf("\t\t当记忆全对,你就胜出了,否则给鼓励信息\n");

printf("\t\t帮助信息\n");

Sleep(2000);

}

void exitGame()

{

char select;

printf("你确定要退出码?退出请按Y或y,继续点任意键\n");

scanf("%c",&select);

getchar();

fflush(stdin);

if(select=='Y'||select=='y')

{

printf("欢迎再来游戏\n");

exit(1);

}

else

{

return;

}

}

void SystemSet()

{

char choice;

printf("############进行游戏系统相关设置###########\n");

printf("目前显示数字的个数是%d\n",num);

printf("是否设置显示个数,不设置请输入Y或y,设置点任意键\n");

scanf("%c",&choice);

getchar();

fflush(stdin);

if(choice=='Y'||choice=='y')

{printf("保持默认值:%d\n",num);}

else

{

printf("请输入你设置的数值");

scanf("%d",&num);

getchar();

fflush(stdin);

printf("显示数字个数设置成功\n");

}

printf("目前显示数字时间间隔的秒数是%d\n",numIntermission);

printf("是否设置显示数字时间间隔的秒数,不设置请输入Y或y,设置点任意键\n");

scanf(" %c",&choice);

getchar ();

fflush(stdin);

if(choice=='Y'||choice=='y')

{

printf("保持默认值:%d\n",numIntermission);

}

else

{

printf("请输入你设置的数值:");

scanf(" %d",&numIntermission);

getchar();

fflush(stdin);

printf("显示数字时间间隔的秒数设置成功\n");

}

printf("目前显示数字闪动次数是%d\n",flashNum);

printf("是否设置显示数字闪动次数,不设置请输入Y或y,设置点任意键\n");

scanf("%c",&choice);

getchar();

fflush(stdin);

if(choice=='Y'||choice=='y')

{

printf("保持默认值:%d\n",flashNum);

}

else

{

printf("请输入你设置的数值");

scanf("%d",&flashNum);

getchar();

fflush(stdin);

printf("显示数字闪动次数设置成功\n");

}

printf("目前每次显示全部数字间隔时间秒数是%d\n",allNumIntermission);

printf("是否设置每次显示全部数字间隔时间秒数,不设置请输入Y或y,设置点任意键\n");

scanf("%c",&choice);

getchar();

fflush(stdin);

if(choice=='Y'||choice=='y')

{printf("保持默认值:%d\n",allNumIntermission);}

else

{

printf("请输入你设置的数值");

scanf("%d",&allNumIntermission);

getchar();

fflush(stdin);

printf("每次显示全部数字间隔时间秒数设置成功\n");

}

printf("游戏系统相关设置\n");

Sleep(3000);

system("cls");

printf("目前游戏系统相关设置\n");

printf("目前显示数字的个数是%d\n",num);

printf("目前显示数字间间隔的秒数是%d\n",numIntermission);

printf("目前显示数字闪动次数是%d\n",flashNum);

printf("目前每次显示全部数字间隔时间秒数是%d\n",allNumIntermission);

printf("目前游戏系统相关设置\n");

}

void displayRandNumber(int array[],int n) //生成指定个数的随机数,按规定时间间隔显示数字,再整体显示指定次数。

{

int i,j;

//生成被记忆的随机数

printf("共有%d个数字需要记忆\n",num);

srand(time(0));

for(i=0;i<num;i++)

{

array[i]=rand()%100;

}

printf("\n");

//按序输出被记数字

for(i=0;i<num;i++)

{

printf("\r");

for(j=0;j<i;j++)

{

printf(" ");

}

printf("%d",array[i]);

Sleep(numIntermission1000);

}

//闪显被记数字

for(j=0;j<flashNum;j++)

{

printf("\r");

if(j%2!=0)

printf(" ");

for(i=0;i<num;i++)

{

printf("%d ",array[i]);

}

Sleep(allNumIntermission);

}

}

void InputMemoryNumber(int confirmArray[],int n) //按显示的顺序手动输入记忆的数字

{

int i;

printf("\r");

printf("请按序输入你记住的相应数字,每个数字用空格隔开\n");

for(i=0;i<num;i++)

{

scanf("%d",&confirmArray[i]);

}

getchar();

fflush(stdin);

}

int countRightnumber(int array[],int confirmArray[],int n) //统计记忆正确数字的个数

{

int i,trueCount=0;

for(i=0;i<num;i++)

{

if(array[i]==confirmArray[i])

{

trueCount++;

}

}

return trueCount;

}

void displyMemoryCondition(int rightNum) //显示记忆状况

{

if(rightNum==num)

{

printf("\n你真棒,全对了\n");

}

else if(rightNum==0)

{

printf("\n你怎么了,一个都没记住\n");

}

else if(rightNum>0&&rightNum<num)

{

printf("\n你记住了%d个,还有%d个没记住\n",rightNum,num-rightNum);

}

else

{

printf("异常\n");

}

}

void gameDie(int array[],int confirmArray[],int n)//游戏核心模块集{

int rightNum=0;

displayRandNumber(array,100);

InputMemoryNumber(confirmArray,100);

rightNum=countRightnumber(array,confirmArray,100);

displyMemoryCondition(rightNum);

}

void gameMenu() //主菜单,用户进行选择

{

int select;

int array[101];

int confirmArray[100];

int n=100;

while(1){

printf("\t\t##################################\n\n");

system("color 1a");

Sleep(500);

printf("\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");

system("color 1b");

Sleep(500);

printf("\t 1 退出 2 游戏开始 \n");

system("color 1c");

Sleep(500);

printf("\t 3 游戏配置 5 帮助 \n");

system("color 1d");

Sleep(500);

printf("\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");

printf("请输入相应数字,进入不同功能模块\n");

system("color 1a");

Sleep(500);

scanf("%d",&select);

getchar();

fflush(stdin);

if(select==2)

{

gameDie(array,confirmArray,n);

}

else if(select==5)

{

help();

}

else if(select==1)

{

exitGame();

}

else if(select==3)

{

SystemSet();

}

else

{

printf("选择非法,请重新输入进行再次选择\n");

}

}

}

void main()

{

mainPage();

gameMenu();

}

按照你的要求编写的输出数字图案的Java程序如下

public class M{

public static void main(String[] args) {

int i,j,k,index,count=1,N=5;

int arr[][]=new int[N][N];

for(index=1;index<=N;index++){

i=0;

j=index-1;

for(k=1;k<=index;k++){

  arr[j][i]=count++;

  i++;

  j--;

}

}

for(i=0;i<N;i++){

for (j=0;j<N-i;j++){

  Systemoutprint(arr[i][j]+" ");

}

Systemoutprintln();

}

}

}

判断素数的原理是:

素数是只能被1和本身整除的数。例如 3只能被1和3整除,17只能被1和17整除等等,想9就不是素数(能被1,3,9整除)。

编程的算法是:

穷举法,就是将需要判断的数除2、除3一直除到这个数减1

例如:判断7是不是素数的算法是进行如下运算 7/2,7/3,7/4,7/5,7/6,在上述除的过程中如果都不能被除尽则可以断定7是素数,否则就说明7不是素数。

注意:编程时使用求余运算,判断是否能被除尽。

参考程序如下:

dim i as integer

dim j as integer

dim x as integer

x=val(inputbox("请输入一个自然数:","输入提示"))

for i=2 to x-1

if x mod i=0 then exit for

next i

if i=x then '此处若i的值变为x则说明所以上面的循环全被执行完

print x & "是素数"

else

print x & "不是素数" '若i<x则说明循环的中途x就能被除尽了

endif

当然程序还可以被优化,例如不一定非除到n-1,完全可以除到根号下n,

举例一:判断25是不是素数的话,只需要除到5即可

举例二:对于开根后是小数的可以取整,如判断15是不是素数只需要除到3即可。

简化后的程序运行速度会更快的。

以上就是关于如何用c语言分别输出个位、十位和百位的数字全部的内容,包括:如何用c语言分别输出个位、十位和百位的数字、用汇编语言编写一个数字电子钟程序代码、由电脑来猜4位数字游戏的C/C++程序代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10136500.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存