PDA

View Full Version : [C]: come faccio a sommare le cifre di un numero intero dato da input


lucas87
20-10-2006, 16:54
Come faccio a sommare le cifre di un numero intero, con il ciclo while, senza vettori: esempio

Input: 3276523176523
Output: 52

ringrazio in anticipo.

-fidel-
20-10-2006, 17:08
#include <stdio.h>

int main(void)
{
unsigned long int x;
int totale;

printf("Inserisci un numero intero positivo: ");
scanf("%lu", &x);
totale = 0;
while(x != 0)
{
totale += x % 10;
x = x / 10;
}
printf("Somma delle cifre: %d\n", totale);
return 0;
}


EDIT: Vabbé, a questo giro mi sono fatto sfuggire subito il codice...

lucas87
20-10-2006, 17:46
Ti ringrazio infinitamente, cmq non preoccuparti, questo non è un programma, ma una parte di un programma che devo fare.

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.