[ beg @ 28.03.2007. 16:55 ] @
Tek sam poceo da programiram, treba mi program koji izracunava sumu cifara ne brojeva. Npr broja 222 suma je 6. Ako je moguce sa wahil i for petljom. Hvala svima na pomoci! |
[ beg @ 28.03.2007. 16:55 ] @
[ SuPeR_MaSteR @ 28.03.2007. 18:55 ] @
Code: int suma = 0, broj = 222; for (int tmpBroj = broj; tmpBroj > 0; tmpBroj /= 10) suma += tmpBroj % 10; [ RMAN @ 28.03.2007. 22:23 ] @
Evo ja sam uradio tako da mi ucitavani broj bude tipa char
Code: #include<iostream.h> #include<string.h> #include<stdlib.h> int main() { char broj[10],*p; static int i,suma; cout<<"Ucitati broj: "; cin>>broj; for(i=0;i<=strlen(broj);i++) { *p=broj[i]; suma+=atoi(p); } cout<<"Suma je: "<<suma<<endl; } Copyright (C) 2001-2025 by www.elitesecurity.org. All rights reserved.
|