如何使用c中的循环反向打印数字? [英] How do I Print numbers in reverse using loop in c?

查看:130
本文介绍了如何使用c中的循环反向打印数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我被要求创建一个程序,可以从用户那里获得数字。

数字应该总是正数而忽略负号。

然后数字将转换为数字。

我需要按照正确的顺序打印它,因为它看起来像用户输入的数字。

我也应该得到数字的总和并告诉它是否可以被3整除。

如果用户的输入为0,程序应该结束。



所以这是我的代码:

  #include   <   stdio.h  >  
#include < stdlib.h >
int < /跨度> main(){
int 输入,个别,反向= 0 ; // 声明

do { // 循环infinitley除非输入为0
printf( 请输入一个数字:); // 输入数字的语句
scanf( %d,& amp; input); // 扫描输入

if (输入!= 0 ){ // 检查输入是否为0如果是结束程序
int sum = 0 ,i = 1 ; // sum的初始值和i作为计数器

printf( 数字是); // 打印介绍性文字

while (输入!= 0 ){ // 检查输入是否为0
input = abs(输入); // 数字应为正数,因此我使用绝对
individual = input%10; // 得到输入的模数
sum + = individual; // 添加数字
input / = 10 ; ++ I; // 除数位和更新计数器
printf( %d,个人); // 打印个别数字但应该正确订购
}

printf( \ n数字之和为%d \ n ,sum); // 打印总和

if (sum% 3 == 0 ){ // 检查sum是否可被3整除
printf( 此数字可被3整除!\ n); // print
}
printf( \ n); // break line
}

else if (input == 0 ){ // 如果输入为0则结束程序
return 0 ;
system( pause);
}

} while 1 ); // 无限循环

system( pause);
}





样品运行:

请输入一个数字:123

数字是3 2 1

这个数字的总和是6

这个数字可以被3整除!



请输入一个数字



我希望它输出的是:

请输入一个数字:123

数字是123

这个数字的总和是6

这个数字可以被3整除!



请输入一个数字

解决方案

首先,您不是将它们转换为数字 - 您正在从中提取个别数字,但您将获得它们首先从最低位数位置开始 - 所以它会以相反的顺序自然地打印它们。



我建议你做的是分阶段尝试这样做。 br />
1)读取用户的输入。

2)将数字提取到各个变量中(数组是一个好主意)

3 )Pri nt数组中的数字。

3)对数组中的数字求和,然后打印

4)如果总和可以除以3则打印。



目前,当你需要先行走时,你正试图跑步:正确地获得基本版本,然后看看它是否更有效率。



但这是你的作业 - 所以我不会给你任何代码! :笑:

Hi!
I was asked to create a program that will get numbers from the user.
The numbers should always be positive and disregards the negative sign.
The numbers will then be converted to digits.
I need to print it in the right order as it looked like the number entered by the user.
I should also get the sum of the digits and tell if it is divisible by 3.
If the input of the user is 0 the program should end.

So here's my code:

#include <stdio.h>
#include <stdlib.h>
int main () {
	int input, individual, reverse=0; // declarations
	
do {	// loop infinitley except when input is 0
    printf ("Please Enter a number: "); // Statement to enter a number
	scanf ("%d", &amp;input); // scan input
    
	if (input!=0) { // to check if input is 0 or not if yes end program
	 int sum=0, i=1; // initial value of sum and i as counter
	 
     printf ("The Digits are "); // print the introductory text
    
     	while (input!=0) { // check if input is 0 or not
     	input = abs (input); // numbers should be positive so i use absolute
		individual= input%10; //gets modulo of input  
		sum += individual; // adds the digits
		input/=10; ++i; // divides digits and update counter
 		printf ("%d ", individual); // prints individual digit BUT SHOULD BE IN PROPER ORDER
        }
       
		printf ("\nThe Sum of the Digit(s) is %d\n", sum);// prints the sum
		
		if (sum % 3 == 0) { // checks if sum is divisible by 3
         printf ("This Number is divisible by 3!\n"); // prints
         }
         printf ("\n"); // break line
    }
    	
	else if (input == 0) { // ends the program if input is 0
      return 0;
      system ("pause");
    }
         
} while (1);        // infinite loop
        	
 	system ("pause");
}



Sample run:
Please Enter a number : 123
The Digits are 3 2 1
The sum of the digits is 6
this number is divisible by 3!

Please Enter a Number

What I wanted it to output is :
Please Enter a number : 123
The Digits are 123
The sum of the digits is 6
this number is divisible by 3!

Please Enter a Number

解决方案

First off, you aren't converting them to digits - you are extracting individual digits from it, but you are getting them from the least significant digit position first - so it naturally prints them in the reverse order.

What I would suggest you do is try to do this in stages.
1) Read the input from the user.
2) Extract the digits into individual variables (an array would be a good idea)
3) Print the digits from the array.
3) Sum the digits from the array, and print that
4) Print if the sum divisible by three.

At the moment, you are trying to run when you need to walk first: get the "basic" version right, and then look at making it more efficient.

But this is your homework - so I'll give you no code! :laugh:


这篇关于如何使用c中的循环反向打印数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆