1 like 1 dislike
8.1k views

Write a program that reads an integer (1-4 digits) and displays the number of its digits and their sum. For example, if the number is 1234, the program should display 4 and 10

寫一個輸入四位整數 輸出數字的位數和位數的總和。例如,數字是1234程式會輸出4和10。

More information about Loops here.

 

Example input:

1234

Example output:

4 10

 

Example input 2:

245

Example output:

3 11

 

[Exercise] Coding (C) - asked in Chapter 6: Loops by (5.2k points)
ID: 28914 - Available when: 2017-10-26 18:00 - Due to: Unlimited

edited by | 8.1k views

85 Answers

0 like 0 dislike
Hidden content!
#include ** **** ****
int ** ** **
{
*** * ** * int n; //n=1234
*** *** * int * *** **
* * ***** int i=1;
*** * ** ** * * **** *** * * *
* **   a=n/1000; * ***
** ** * * **** ***** * * ** * **** *
** * * *** * *** ** ** ** ***** *** *
* *** ** ** ** **
** ** * * ***
****** * **
** * ** ***
** ***** **** *

*** * ** *
* ** ** * * {
* ******* n=n/10;
**** *   i++;
* * * *** ** }
** * ** * * * * * * *
*** * return 0;
**** * *
**
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
***** **** * ** * *** n,d1,d2,d3,d4;
* * *** **** * ** * * *** count = 0, sum;
*** * * ** ****
******** * * ** ** **** * * * * &n);
* * * * **** ** * * **
**** * * ** * * **
***** ** ** * * *** *** * ** *
* ******** ** **** * ** ** *
**** ** * ** ** * ** ****
* ** ** * *** **** *** ** * *
**** * ** * **** * **
* * * ** ** * * **
***** ** * ** ** * * ********** * * ** *
   }
* * ** * ** * *
* *** * * ****** ** * * (d2!=0){
****** * * ** * * * * * * ****** ** * * *
***** * * ***** *
* ** * * ** *** * *** ** * (d3!=0){
*** ** ** **** *** ** * * ** * * * ** * ****
** * *** ** *
**** ** ** ** * * ** ** **
*** ** **** **** *** * * *** **** * **** ** *
**** * * ** ****** * ** * *
    
* *** * * ** ** = d1+d2+d3+d4;
**** * * ** ** ** ** ** * ** *** %d", count, sum);
    
***** * *** *** *** * 0;
}
answered by (323 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main()
{
  
* ** *** ** * **** * num;
***** * **** * * ** ** * ** num1;
** * * * ** * **** num2;
** * *** * * * * * num3;
*** * ** * * ** * * * * * num4;
* * *** * *** * sum;
    
*** * * * ** ** * * * ** *** * * ** ** &num);
    
  
** ** * ** * * ** ** ** ** ((num/1000)>0)
    {
*** *** * ** ** * * ** * * *** * * * ** * * * * ");
**** * *** * ** * * * * * **** *** *
** ** * * ** ***** ** * * *** * * ** *** ****
** * * ** ****** * * * * **** * ***** * * * * ** * *
* *** * * * * * * * ** * * *** *** * * * * *
* * * ****** **** **** ** **** * * ** * * * * ****
** ** *** * * ***** * **** *** * **** * *** *
* * ***** * ***** * * * *** * * * * ** ** ****** ** ***
** * * * * * ** ** * * ** * ***
* **** *** * * ** * * * **** ** * ** ** * ** ** **
    }
    
**** ** * ** *** * * * if ((num/100)>0)
    {
* ** * ** ***** *** * ** * ** ** * * ** ** * ***** * ");
** ******* **** * * ********* * * * ** ** **** **
*** **** *** **** * * ** * * * * * **** * * ****** ** * * * **
* ** * * * ** **** * * *** ** ** ** ** **
** ******* * ** ** ** * * * * ** *******
*** * * * * ** ** ** * * * *** ** ** *** *
* **** ** * ** ***** ** *** ** **** * * * ** * * *
* ** * * **** * * *** * **** * *
* ** * ** * ** * * * **** ** * * * ****** *** *** * ***** ***
    }
    
* * **** * *** * * if ((num/10)>0)
    {
** **** *** **** * * * *** *** *** * * ** ");
** * * ** ** **** * ** *** * * ***
* * ** * **** * * ****** *** ** *** * * **
* * * ** ** * * * * ** * *** ** ** ** *** * ** *** * ***
***** *** * * * * * * ** **** * * * * * * * *
** * * * * ** ** ** ** * **** * *** * * * * ***** * ** *
** * * * * ** * * * * * * * * **
* * ** * * * * ** ** *** ** ** ** * * ** * * *** * ** ** *
    }
    
** ** *** *** ** * ***** if ((num/1)>0)
    {
* ** * * *** ******* ** ** * ** *** * ***** * * **** ** ");
** * * * * * ** *** ***** ** * * *
* ** *** **** ** **** * * * * ** * * ****
** * * ** * * **** *** * *** ******* *********
* ** *** ** *** * *** ** * **** * * ** * * *
* * *** * * ** ********* ** * *** ** *** ***
** * * * * *** **** * * *** *** ** ** * ** * ** * ** **
    }
    
* * ** * *** * ** 0;
}
answered by (-193 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
int main()
{
* *** *** * * *** a,i,j,k,l,m,n;
* ** ** * * * ******** * * *** * * * *
*** * * * *** ** *** ** ** * * * * * * * ****
** ** * ** * i/1000 ;
* **** * ***** * ***** *** ;
** * ** ***** ******* *** * * ;
****** *** * * ** *** ** ;
**** * * * **** * ****
* * ** * ** ** * * * * *
* * * * * * ** * ** *
* * ******* **** * ** ****
*** *** * ****** ****
* *** * * ** * * ** * %d",a,n);
}
answered by (-32 points)
edited by
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
* ******* * ** * * * * a,sum,b;
** ****** *** * * * * *** * ** * * *
* *** * * * * * *** * * * * ***** *** * * * * *
* ** * ** ** * * ** ** * *
* * *** ** **** ** *** * * *** ** **
*** * ** ** **** * **** * ** * *** * ***
* ***** ** * **** * ** *** * ** * *** *** ** *
* ** * ** * * * * if(a<=99&&a>9)
** * **** *** * ** * ** ****** ** *
*** * * ***** * *** * * * ***
* * ** * ** * * * ** * * **** * ** ***** *** * * * * *
*** ***** * * * ** ******* * * %d",b,sum);
* * ***** * ** * 0;
}
answered by (-258 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
can you replace 4 if statements with just 1 loop?
0 like 0 dislike
Hidden content!
#include<stdio.h>

int main(){

* * * * * ** ** a,b,c,d,e,f,g;
*** * * * *** *** * **** *
* * * ** *** * * *
* ***** * ** * * * *** * * **
***** * * * * ** ** * * **
** * *** *** * * * ** ** * *
* ** **** * * * * *** * * * * *
* * ** * ** * ** ***


*** * * * * ** **** * ** *
** ** * * * ** * * **
** * ******* * ** **** * ** ** * * * * **
* *** * * ** * **


* * ** *** ** ** * ** ** * ** * ** %d",b,g);

return 0;

}
answered by (-301 points)
0 0
prog.c: In function 'main':
prog.c:6:6: error: statement with no effect [-Werror=unused-value]
     b==0;
     ~^~~
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main()
{
* ** * ** ** *** ** ** a=0,b,sum=0,i=10;
*** * * * ** ****** ** *** * *** **

**** **** * ** ******* *** ***
* * ** * * * ** ** *
* * *** *** * * * * * *
***** **** * ** * * *
* ** ** * * * ** *** * * *****
** *** * * * * ** * *** %d\n",b,sum);
* ***** ** ** * **** *****

** **** * *** * * * * ***** ** * %d",a,sum);
** **** ** ** * * ** *** ** 0;
}
answered by (54 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main()
{
*** **** *** * a=0,b,sum=0,i=10;
* ** *** ** * ***** ** * * * *** * *****
* *** * ** ** ** ** *

* * * * ** * ** * *
* * * *** *** ** *
* *** *** * ** * ** ***
* ** ** ** * **
** * ** **** ** **** ** * * ** *
** ***** ** * * *** * *** %d\n",b,sum);
**** * *** ** ** *** * **

** * ** * ** * * * ***** ** * * %d",a,sum);
* ** * ** **** ** *** * 0;
}
answered by (54 points)
0 0
prog.c: In function 'main':
prog.c:7:5: error: 'g' undeclared (first use in this function)
     g=b;
     ^
prog.c:7:5: note: each undeclared identifier is reported only once for each function it appears in
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main()
{
* ** * * *** * ** * a=0,b,sum=0,i=10;
** * * *** * * ** * *** * * ** *** *
** * * ** * *** ****** *****

* * ** *** ***** * ** * ** *
*** * ** *** * * ****
* ** * * * * ****
* * **** ** * * ** ** *
** ** * ** * * ***** ** ****
* ** * * * ** * *** ** * * ** * %d\n",b,sum);
* * ** * * * *

* **** ** ** * **** ** * %d",a,sum);
*** *** * * * * ********** *** * 0;
}
answered by (54 points)
0 0
prog.c: In function 'main':
prog.c:7:5: error: 'g' undeclared (first use in this function)
     g=b;
     ^
prog.c:7:5: note: each undeclared identifier is reported only once for each function it appears in
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main()
{
** ****** * **** * ** * **** a=0,b,g,sum=0,i=10;
* ** * * ** ** *** ** *** ** * ** **
* * ***** * * * ** ** * * *

* ** *** * *** * ** **
* ** ** * * * ** * * ***
* * ** **** ** * * *
* * **** * * *** * **** ** *
****** *** ** * * ** * ****
** **** ** * *** * * * **** * * * %d\n",b,sum);
*** **** *** *** * * ****

* ** ** * * ** *** * ** ** %d",a,sum);
*** ** ** ** * * * ** * * *** 0;
}
answered by (54 points)
0 0
prog.c: In function 'main':
prog.c:5:15: error: variable 'g' set but not used [-Werror=unused-but-set-variable]
     int a=0,b,g,sum=0,i=10;
               ^
cc1: all warnings being treated as errors
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.100.171
©2016-2025

Related questions

0 like 0 dislike
69 answers
[Exercise] Coding (C) - asked Oct 26, 2017 in Chapter 6: Loops by semicolon (5.2k points)
ID: 28913 - Available when: 2017-10-26 18:00 - Due to: Unlimited
| 6.3k views
2 like 0 dislike
64 answers
[Exercise] Coding (C) - asked Oct 19, 2017 in Chapter 4: Expressions by thopd (12.1k points)
ID: 27669 - Available when: Unlimited - Due to: Unlimited
| 8.9k views
12,783 questions
183,443 answers
172,219 comments
4,824 users