C/C++ Programming Learning-Week 2 ③ Output a three-digit number in reverse

Topic link

Title description

Xiao Suan Suan has a three-digit number, and she wants the smarter you to output the three-digit number in reverse.

Input format
A three-digit n (100 ≤ n ≤ 999).

Output format
Reverse output n, keep leading 0.

Sample Input

100

Sample Output

001

Ideas

Idea 1:
We can store the input numbers in a character array and output them in reverse.

Code:

#include<stdio.h>
int main()
{
    
    
    char a[100];	//定义一个字符数组
    scanf("%s",a);	//读取数字,从第0位开始,存储在字符数组中。
    for(int i = 2; i >= 0; --i)	//逆向输出
        printf("%c", a[i]);
    return 0;
}

Idea 2:
We can divide this number by 10 or take the remainder of 10 to separate the numbers on each person.

#include<stdio.h>
int main()
{
    
    
    int n;		//定义一个变量n,存储输入的三位数
    scanf("%d", &n);		//读入
    while(n)	//相当于while(n>0)
    {
    
    
        printf("%d", n % 10);//数字的个位数
        n /= 10;		//把个位数字去掉,并更新n
    }
    return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n;
	while(cin >> n)
		cout << n % 10 << n / 10 % 10 << n / 100 << endl;
	return 0;
}

Other exercises this week:

C language programming column

C/C++ Programming Learning-Week 2 ① Output Mario

C/C++ Programming Learning-Week 2② Print ASCII code

C/C++ Programming Learning-Week 2 ③ Output a three-digit number in reverse

C/C++ Programming Learning-Week 2 ④ Calculate the value of a polynomial

C/C++ Programming Learning-Week 2 ⑤ Calculation of the last term of the arithmetic sequence

C/C++ Programming Learning-Week 2 ⑥ Collect bottle caps to win prizes

C/C++ Programming Learning-Week 2 ⑦ Find the sum and mean of integers

C/C++ programming learning-week 2 ⑧ output character triangle

C/C++ Programming Learning-Week 2⑨ Judging Leap Year

C/C++ Programming Learning-Week 2⑩ Mr. Suantou goes to work

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112853046