Hi! I am a new member on DaniWeb site. And I need your help!!!
I just started C++.
I have project to do. The user must insert 4 integers. My main problem is that I may not input th digits seperately, but should input the number as a whole.
And then I must do calculation on those 4 number.
example:
for numbers 3124

Is it possible???????
:o

Is that random or is there some logic behind that?

Is that random or is there some logic behind that?

No that are no random in it

user can enter any 4 numbers from 0 to 9 with no space

Is that random or is there some logic behind that?

the whole question is.
write a program which will calculate, for every integer that is being entered, a code inthe following way:
first digit multiplied by itself plus
second digit multiplied with the third digit minus
fourth digit
You may assume that the given inyegers will all consist of fout digits. Use a for loop iterating from 1 to 6.
You may not input the digits separately, but should input the number as whole. (Use / and % in the calculations)

The basis for a solution to this project is given in the parenthesis at the end of the problem statement.

If you use the modulo operator on two ints like this:

a % b

then the return value is the remainder of a divided by b. So if a were 11 and b were 10 then the return value will be 1 and if a were 1462 and b were 10 then the return value would be 2.

In C/C++ if you have 2 ints and use the / operator like this:

a/b

then the return value is only the integer number of the result; any fractional/decimal value is ignored. So 10/ 4 is 2, not 2.5. Likewise, 1462/10 will be 146, not 146.2.

If you combine these two processes in the correct order within a loop, then you can separate the user input into four separate integers that can then be rearranged in the fixed equation presented.

Two options:
#1) Lerner's idea will work well
#2) Input the number as a string of characters. If you input 3157, you wil have a string with '3','1','5','7'. Each character is then converted to an integer by subtracting '0', or 48.

Hi there!

You can separate the digits of an integer simply by using this method:

Declare a variable with name say 'number';
lets say its a 4-digit number, now lets say the user gave 3452 as the input number so to separate the digits simply find the number modulo 10 and the 4th digit will come out store it in a variable say digit4, now the quotient of the number when divided by 10 should be the next target for the number modulo 10 so store number / 10 in number and again number modulo 10 will give you the 3rd digit and so on, example :
---------------------------------------
int number,d1,d2,d3,d4;
cin >> number;
d4=number%10;
number=number/10;
d3=number%10;
number=number/10;
d2=number%10;
number=number/10;
d1=number;
---------------------------------
After the number is divided four times by 10, the fourth quotient will be the first digit so the final step shall simply be assigning the number which is previous number divided by 10 to the d1 variable

Hope it helps,

QaiS

Guys! You are great ! :)
Thanks a lot. After all it was so simple. I just finish it! Works great!

cout << "Enter 4 digits number: ";
cin >> num;
answer = ((num / 1000) * (num / 1000)) + (((num / 100) % 10) * ((num / 10) % 10)) - (num %10);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.