设计c语言程序(输入三位数,分别输出该数各个数位上的数字)

兄弟姐妹们哪位知道,设计c语言程序(输入三位数,分别输出该数各个数位上的数字)
最新回答
賲呗^ō^钸哭

2024-11-07 01:25:20

#include <

stdio.h
>

int main()

{int x,a,b,c;

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

scanf("%d",&x);

a=x/100;

b=x/10%10;

c=x%10;

printf("百位是%d\n十位是%d\n个位是%d\n",a,b,c);

return 0;

}

骑猪Δ追阳光

2024-11-07 00:12:54

关键是把这个三位数分成三个数位上的单个数字,用除法运算和取余运算%来完成,关键几句如下:
int x,a,b,c
x=423
c=x % 10余数为3,得到个位上的数字
b=(x%100)/10说明:x%100得余数23,再利用两个整数相除结果取整数商,得十位上的数字2
a=x/100得4 ,这还是利用了整数除法的特点
printf("a=%d b=%d c=%d",a,b,c)输出a=4 b=2 c=3

希望对你有用
我们说好的快乐呢

2024-11-07 00:21:13

/**
*其中的事件响应 机制 比较经典,你可以看看
*
*/

package two;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
* 窗体
* @author Administrator
*
*/
public class MyFrame extends JFrame {
private JPanel panelNorth;
private JPanel panleCenter;
private JPanel panelSouth;
private JLabel jlbA, jlbB, jlbC;

private JTextField jtf;
private JTextField jtfR;
private JButton jbtn;

private JTextField jtfB, jtfS, jtfG;

public MyFrame(String title) {
this.setTitle(title);
initMyFrame();
}

private void initMyFrame() {
this.setLayout(new BorderLayout());
panelNorth = new JPanel();
panleCenter = new JPanel();
panelSouth = new JPanel();

jtf = new JTextField(15);
jtfR = new JTextField(15);
jtfR.setText("<----输入数字");
jtfR.setEditable(false);
panelNorth.add(jtf);
panelNorth.add(jtfR);

jbtn = new JButton("显示");

panleCenter.add(jbtn);

jtfB = new JTextField(10);
jtfS = new JTextField(10);
jtfG = new JTextField(10);

jlbA = new JLabel("百位:");
jlbB = new JLabel("十位:");
jlbC = new JLabel("个位:");

panelSouth.add(jlbA);
panelSouth.add(jtfB);
panelSouth.add(jlbB);
panelSouth.add(jtfS);
panelSouth.add(jlbC);
panelSouth.add(jtfG);

this.getContentPane().add(panelNorth, BorderLayout.NORTH);
this.getContentPane().add(panleCenter, BorderLayout.CENTER);
this.getContentPane().add(panelSouth, BorderLayout.SOUTH);

/**
* 给按钮 添加 事件 监听器
*/
ActionListener al=new BTNListener(this);
jbtn.addActionListener(al);

this.setVisible(true);
this.setBounds(200, 200, 500, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}

public boolean checkInputString(){
String s=getJTFText();
if(s==null || "".equals(s)){
return false;
}

if(s.length()==3){
if((s.charAt(0)> '0' && s.charAt(0)<='9') && (s.charAt(1)>='0' && s.charAt(1)<='9') && (s.charAt(2)>='0' && s.charAt(2)<='9')){
return true;
}
}

return false;
}

public void showMyFrameMessage(){
this.jtfB.setText(String.valueOf(getJTFText().charAt(0)));
this.jtfS.setText(String.valueOf(getJTFText().charAt(1)));
this.jtfG.setText(String.valueOf(getJTFText().charAt(2)));
}

public void clear(){
this.jtfB.setText("");
this.jtfS.setText("");
this.jtfG.setText("");
}

public String getJTFText(){
return this.jtf.getText();
}

public void setJTFRText(String msg){
this.jtfR.setText(msg);
}

public static void main(String[] args) {
new MyFrame("TITLE");
}
}

/**
* 按钮的 监听器
* @author Administrator
*
*/
class BTNListener implements ActionListener{
private MyFrame frame;

public BTNListener(MyFrame frame){
this.frame=frame;
}
@Override
public void actionPerformed(ActionEvent e) {
if(frame.checkInputString()){
frame.showMyFrameMessage();
frame.setJTFRText("再次输入数字");
}else{
frame.setJTFRText("错误的输入,再次输入数字");
frame.clear();
}

}

}
把核心的算法给你。自己去实现吧。
int m;//m为输入的三位数。
int a,b,c;
a = m/100;
b = m%100/10;
c = m%100%10;
伤-蔓延

2024-11-07 00:42:27

#include<stdio.h>
main()
{
int a,b,c,d;
printf("Enter a number:");
scanf("%d",&a)"
b=a%100; //各位的
c=a/100; //百位的
d=(a%100)/10; //10位的
printf("%d%d%d\n",b,d,c);
}

应该是这样了
释放

2024-11-07 00:01:09

#include <stdio.h>

int main()
{
int i=0;
printf("please put a number");
scanf("%d",&i);
while(i)
{
printf("%d\n",i%10);
i=i/10;
}
return 0;
}