+ 1

How to find the sum of the last digit of "n" numbers.

I want to learn more of c++ but i can't solve this problem: finding the sums of the last digit of "n" numbers. Can you show me the way?

24th Oct 2019, 7:29 PM
George Listru
George Listru - avatar
8 Respuestas
+ 2
Few problems. You create an array of length x. But what is x? You don't say. Probably you want it to be length n; so the user first has to give it. int n, sum=0; cin >> n; int Cifrele[n]{}; Now you only need to input into the slots of that array: for(int i=0; i<n; i++) cin >> Cifrele[i]; And then you loop over the numbers and add the last digit of each slot to sum (watch out: This has to be zero!). for(int i=0; i<n; i++) sum += Cifrele[i]%10; Be sure to understand each of these steps, while you modify your code.
24th Oct 2019, 8:07 PM
HonFu
HonFu - avatar
+ 1
Where is your attempt?
24th Oct 2019, 7:32 PM
A͢J
A͢J - avatar
+ 1
Here #include <iostream> using namespace std; int main() { int n=0,x,Cifrele[x]={},sum; cout << ""; cin >> n; for(int i=0; i<n; i++) { cout << " "; cin >> n; } cout<<""; for(int i=0; i<n; i++) { cin>>x; Cifrele[x]%10; sum+=Cifrele[x]; } cout<<sum<<endl; return(0); }
24th Oct 2019, 7:33 PM
George Listru
George Listru - avatar
24th Oct 2019, 7:54 PM
George Listru
George Listru - avatar
0
Can you please write it on Sololearn Playground.
24th Oct 2019, 7:50 PM
A͢J
A͢J - avatar
0
George Listru I think we can't take input in loop so Here is a simple solution. #include <iostream> using namespace std; int main() { int n=5, Cifrele[n] = {10, 20, 34, 42, 35}, sum; for(int i=0; i<n; i++) { sum += Cifrele[i] % 10; } cout << sum << endl; return(0); }
24th Oct 2019, 8:13 PM
A͢J
A͢J - avatar
0
We can take input in loop - you just have to split it by line.
24th Oct 2019, 8:19 PM
HonFu
HonFu - avatar
0
you don't need an array for this. take the number, find its last digit(divide by 10 until you get a number less than 10), add it to the sum, till n numbers have been taken.
25th Oct 2019, 3:53 PM
AliZ