0

How do I make my program accept elements of an array as one number or one word ?

# include<iostream> using namespace std; int main() { int a[3]={ 1,2,3}; if(a%2==0) // if 123 divided by two gives a {. // remainder of 0 say Even. cout<<"Even."; } else { cout<< "Odd."; } }

15th Apr 2018, 7:25 AM
Mr Doom
6 odpowiedzi
0
Thanks
15th Apr 2018, 10:02 AM
Mr Doom
+ 1
You access the elements of an array with adding an index. These start from 0 up to your number of elements -1. You use them with the [ ] operator. In your code these indexes would be: 1st. 2nd. 3rd a[0] a[1] a[2] edit: I now realized you want to use all of them. There are multiple ways to do this. For example append them all to a string and convert the string back to an integer or by multiplying the values and add them up. 1, 2, 3 -> 1*10^2 + 2*10^1 + 3*10^0 -> 123
15th Apr 2018, 7:40 AM
Alex
Alex - avatar
0
Thanks
15th Apr 2018, 7:56 AM
Mr Doom
0
// program to accept digits of a number and display the number as one. // Why is it not working ... can any one help ??? #include<iostream> using namespace std; int main() { char t[100],a; int m=0; cout<<"Enter(press * after the last digit to stop entering.): "; while(a!='*') { cin>>a; t[m]=a; m++; } int c; c=t[0]*100+t[1]*10+t[2]; // if the user enters 3 digits. cout<<c; }
15th Apr 2018, 9:26 AM
Mr Doom
0
You read the integers as chars. You would need to convert them back to integers or else it uses the value of the char, which is not the actual digit. Here is a code which shows the characters: https://code.sololearn.com/cGGLzCue2fpx/?ref=app You have multiple ways to solve this problem. 1. read the input as an integer. You would need another while loop to do this, because you can't specify that * ends the loop. 2. use atoi(t) //#include <cstdlib> This converts an array of characters (basically a string) to an integer
15th Apr 2018, 9:54 AM
Alex
Alex - avatar
0
Here are the 2 methods I mentioned for reference: https://code.sololearn.com/ct3L6zDhnD9J/?ref=app
15th Apr 2018, 9:59 AM
Alex
Alex - avatar