1 like 1 dislike
11k 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 | 11k views

85 Answers

0 like 0 dislike
Hidden content!
** ** *** * *
int main *
{
int *
***** * *** * * * ** *
** (a != 0){
**** **** * ** ** *** += a%10;
* * * ** *** *** *** * /= 10;
* * **** * * **

}

** * **** * *** **
*** * 0;
}
answered by (-255 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, a2, b1, b2, b3, b4, i, sum;
***** * **** ** * ** * **
**** *** * * ** * * **** * ***** * *** &a);
* * ***** ***** *** * *** *
** * * * ***** * * ** ** * ** {
** ** * * ** ** ** * *** **** * ** * ** ** **
** **** ** * ** * ** * ** * * * * *** * break;
* * *** ** ** * * * * * * * * ** **** * ***
    }
** * * * ** * * *** * **** * * *** ** ", i);

* ** * ** ***** **** * * * {
** * **** *** * ** * ***** ** * ** * ***
*** * ** *** * * ** * ** ***** * * *
** ****** **** * * * ****** * ***** ** * * * * ** * * **
* * * * * * * **** * ** * * ** ** * * **** *** * **** ** *** ** * * ***
    }
** ** * * * *** * * if(i==3) {
* * * * * * **** ** * * ** **** *** ** ** *
** ****** * ** * ** * ** ** ** ***** *** * * ****** * *
* * * ** * * * * *** * * * **** * * *** * * * *
    }
*** ** **** * * **** if(i==2) {
* * * * ** * **** * ** * * ** ******* ** *
* ** *** ** ** * ** * * **** * ** ** ** * ** ** * ** ** * *
    }
*** ****** * * ** *** ** if(i==1) {
* * * * * * ** ** * ** * ** * ** **** * ***
    }
* ********** * **** ** **** * *
*** ** ** ** ** * ***** *** %d %d %d", b4, b3, b2, b1);
**** * *** ** *** * * * * * ** * sum);
}
answered by (-108 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 only 1 loop?
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
** * * ** * * **** * a,b,c,d,e,f;
* * ***** * * ** * *** * * %1d %1d ** ** * ****** ** *** * ** ****** *
* ***** ** * *** ***** (a==0 && b==0 && c==0)
**** * * ** **** * * * ****
***** ** ** ** *** ** * ***
* *** ** * ** * * **
* * * ** * ****** if(a==0 && b==0)
* *** ** * * * ** * ** * *
**** **** * * ** *** ** * * ** * ** ***
* * * ****** ** *
* ** ** *** * ** * ** if(a==0 && b==0)
** * * * ** *** ***** ****
** ***** * ***** * * ***** *** * * * ** ** *
* * * * * ** * *** ** *
** * * * **** ** **** ** ****
* ** * ** * **** * ** **
** * ** ** **** * * *** ** * * ** ** **** **
** *** ** * * *
* * * * **** * * * * * *****
* *** * *** * * ** ** *** **** %d",f,e);
*** ** * ***** * * *** 0;
}
answered by (-229 points)
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 0
what if a == 0 && c == 0 && d == 0
or b == 0 && c == 0 && d == 0
it will all print f = 4
0 like 0 dislike
Hidden content!
int main() {
    int a, a2, b1, b2, b3, b4, i, sum;
* ***** ***** *** * * *
*** * * * * * **** * * ** * ** * * &a);
* * * * *** * *** * * *
* * * ****** * * * * {
* * * *** * * * ** ***** ** * * * *
* **** * *** * * * * * ** * ** **** * * * ***** * break;
* *** * *** * *** *** *** ** * ** * ******* ** *
    }
* * * ** ** *** * ** * * ***** ** * * ", i);

* ***** * * ** * * *** {
* *** ** ** * * * ***** * * * ***** * **** * *
***** ** ***** * **** * *** *** * * * * * * ** **
*** ***** * * * * **** ***** *** * ** *** ***** * *
* ** *** ** * * ** ** * * * ** ** **** ** ***** * ** *
    }
** ** * * *** **** * * ** if(i==3) {
** ** * * ** *** * ** * * * ** * ** * * * ** ****
* ***** ** * ** *** ******** *** * *** * * ** **
* * * ** * * *** ** ***** * * *** * ** *** * * **
    }
* * ** * ** * * *** *** if(i==2) {
* * *** * * * * *** **** * *** *** * ** *
** ** * * ***** ****** * * ** * * ** * ** * ****
    }
** * * * *** ***** if(i==1) {
**** * * * * *** ** *** * *** **** *** * *
    }
* * * * **** **** * ** ** *
** ** ** **** * ** * * *** * %d %d %d", b4, b3, b2, b1);
** ******* ** ** **** * ** *** ** ***** sum);
}
answered by (-108 points)
0 0
prog.c: In function 'main':
prog.c:4:5: error: implicit declaration of function 'scanf' [-Werror=implicit-function-declaration]
     scanf("%d", &a);
     ^~~~~
prog.c:4:5: error: incompatible implicit declaration of built-in function 'scanf' [-Werror]
prog.c:4:5: note: include '<stdio.h>' or provide a declaration of 'scanf'
prog.c:11:5: error: implicit declaration of function 'printf' [-Werror=implicit-function-declaration]
     printf("%d ", i);
     ^~~~~~
prog.c:11:5: error: incompatible implicit declaration of built-in function 'printf' [-Werror]
prog.c:11:5: note: include '<stdio.h>' or provide a declaration of 'printf'
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
*** ** * * **** * *** *** a,b,i,max;
* * ** ** ** * * * * * * ** * * * %d",&a,&b);
* * * * * * * * * *** * * ******* **** ** *
* ***** * * ** * *
***** * * * * * ** ** ** *** ** * ** ** *** * *** * ***
** ******** ** ** * * **** **** * ** * ** * **
* * * * * * *** ** **** *
** *** * * ** ** *** ** *** ****
**** * * * ** ** * 0;
}
answered by (-284 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main(){
* ** * * * ** ** * i;
* * ** *** * **** sum=0,counter=0;
**** ** * *** * ** ** ** ** * *** * ** ){
* * * ***** * * ** **** * ***** ** * ** * ***** ** ** ***
** ** * * * * * **** *** * * * * ** *** ****
* * ** ** *** **** * * * * *
** ** ***** * ** * * ** %d",counter,sum);
* * * *** * * ** * ** * ** * 0;
}
answered by (-116 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main (void)
{int sum,d,c,i,m;
****** **** ** * * ** * * *** * * *** *** **
*** * * * * * * ** * * ** * * *
** * ** * *** * * * ** *
    {
* * **** * ****** ** *** * ** * * ** ** * * *
* ** ** * * *** * * ** * * * ****
* * ** * * **** ** *** * **** * ** *** ***
***** * *** * **** * ** ** * *
* ** * **** * * *** ******* ** * ** *** ** ** *
    }
* *** * * * *** * * * * * *
* ** * ** * * *** * * ** *** %d",i+1,sum);
** * *** ** * ** *** *** ** 0;
}
answered by (-168 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
1 like 0 dislike
Hidden content!
#include<stdio.h>

int main() {
* ** * * ** *** ** a, a2, b1, b2, b3, b4, i, sum;
* * * * * *** * * *** **
* ** * ** * * * * *** * * * **** * &a);
***** ** ** *** ** *** *
**** *** * *************** * {
***** * * ** * * * ** ** * * ** * * *** ***
** ***** ** * * ****** * *** *** ** *** * * ** ** break;
** * * * *** * *** *** ** ** ******* *** **** * *
    }
** * ** *** **** ** ** * * ", i);

* * * * * ** * * ** * ** ** * ** {
*** ** * **** *** ** * * * * ** * * **** ** * * *
* ******** ********* *** ** * *** ******* * * ** * ** **** *
******* ** ** *** ** ** * ***** * ******* * * ** **
* ** * * * * *** ** *** ***** ** * * **** *** * ** * * ****
    }
** * *** *** * * *** * if(i==3) {
* ** ** *** ** ** * * * * ** * * * ***** ** * *
** * * * * * * ** * * * ***** ** * *** *
* * **** ** **** * * * ** * * ** ** * ** ** *** ** **
    }
* *** ***** * ***** * ** if(i==2) {
** * *** *** ** * ** * ** *** *** **** * *** *****
** * * ** ** * * * ** * **** * ** * * *** * ** ** ***
    }
* * ***** ** * ** ** * *** if(i==1) {
* * * **** * * **** * ** * ** ** ** **
    }
* **** * *** ****** ** * ***
* *** ** ** * **** *** ** * ** * * %d %d %d", b4, b3, b2, b1);
* * * * ** *** ***** *** sum);
}
answered by (-284 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(){
** ** **** *** ****** * ** i;
* * * ** *** * * sum=0,counter=0;
* * * * * * *** **** * *** * ** * ***
*** ** ** * *** * * *** {
* *** *** * ** ** * * ** * * ** * * * * * **** *
***** * * * ** ** * * * * * * ** * * i%10;
* * * * ********* ** * ** * **** * * ** *
** ** * * * * * ****** ** ** ** *

*** * * * * * * **** ** * * %d",counter,sum);
* * ** *** *** ** ** ** 0;
}
answered by (-116 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!
*** ***** * *

int * * *

int **

int

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

* * * ** *
a= **
* *
}


** * * ** * * * * *


}
answered by (-336 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.71.255.100
©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
| 8.7k 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
| 12k views
12,783 questions
183,442 answers
172,219 comments
4,824 users