+ 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?
8 Respostas
+ 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.
+ 1
Where is your attempt?
+ 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);
}
0
Can you please write it on Sololearn Playground.
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);
}
0
We can take input in loop - you just have to split it by line.
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.