1
I need to sum the digits of a number typed by the user.
For example:
- If input equal to 20, make 2 + 0 = 2
- If the input is 45, make 4 + 5 = 9
Visualg and C can be used.
1
I need to sum the digits of a number typed by the user.
For example:
Visualg and C can be used.
0
Assuming that this input number can be from 0 to several decimal places.
The logic to solve the problem would be to individually divide the number by 10 until 0;
So the code can go like this:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int a,soma=0,aux;
printf("Digite um numero:");
scanf("%d",&a);
while(a!=0)
{
aux=a%10;
a=a/10;
soma+=aux;
}
printf("\nA soma dos digitos eh:%d",soma);
}
0
Use the rest of the division for 10:
int resto= 20 % 10; //obtem o resto da divisão por 10
int valorSemDireita= 20 / 10; //Como é um inteiro vai retirar o nº da direita
resultado= resto + valorSemDireita;
Browser other questions tagged c visualg
You are not signed in. Login or sign up in order to post.
Have you done anything so far? We need to evaluate your code to help!
– Maurício Z.B
I tried using the while. In Visualg, but it doesn’t work. of type. While in %10. Type(in), but this is a mistake
– Martin Dala
Edit the question (clicking here if you don’t know how) and add the code you’ve already done and how far you’ve tried
– Ricardo Pontual
Only two numbers can be entered or there is no limit?
– Thiago Magalhães
Edit your question at the top on the edit button and put the code... I will try to look here your code.
– Maurício Z.B
You can keep the input as string and iterate for each character... it will make it much easier.
– Gabriel
@Martindala put the solution, tested ta ok!
– Maurício Z.B