Write a program which will calculate, for every integer that is being input, a code in the following way

first digit multiplied by 3
plus
second digit multiplied with the third digit
minus
fourth digit

thus the number 3124(of type int) should generatethe code 7, because 3*3 +1*2 - 4=7.
you may assume that the given integers will all consist of four digits. submit printouts of the program and output with he six integers below as input. use a for loop iterating from 1 to 6. NB: You may not input the digits separately, but should input the number as a whole. (hint:Use / and % in the calculation.)

3255
1067
1393
2892
1111
2020

i tried this program below but it gives me (-2answer)
please help me

#include <iostream>
using namespace std;

int main( )
//Initialize integers
{
int number,first,second,third,fourth,answer;

//For loop
for(int x=1;x <= 6;x++)

//Input of number from user   
    cout << "Enter in number: ";
    cin >> number,first,second,third,fourth;

//Get the first digit in the thousands place   
first = number / 1000;
number = number % 1000;

//Get the second digit in the hundreds place   
 second = number / 100;
 number = number % 100;
 
//Get the third digit in the tens place   
  third = number / 10;
  number = number % 10;
  
//Remaining is the ones place
  fourth = number;

//Calculate and give answer
  answer = first *3+second *third -fourth ;
  cout << "Your aswer is" << answer;
  cin >> answer;
  return 0;
  
}

Its not

cin >> number,first,second,third,fourth;

but

cin >> number>>first>>second>>third>>fourth;

and kindly use code tags in future

it looks like you are prompting for 6digits and you specified that the requirement is to get a whole number...then change your input to get the number as a whole

cin>>number;

your for loop doesnt look controlled, why are you prompting the user for "answer"??

Thanx for that correction
But still the problem is not solved the program gives me the wrong output. i need help on the calculation formular.

GUYS I NEED YOUR HELP

Thanx for that correction
But still the problem is not solved the program gives me the wrong output. i need help on the calculation formular.

GUYS I NEED YOUR HELP

You might want to repost your program (using code tags) to see if it really was fixed.

Also, you may want to change something like

first = number/1000;

to

first = floor(number/1000);

floor() is declared in math.h (so add #include<cmath> or #include<math.h> to the top of your source). i think it would be better than simply relying on the integer round down, but it won't really change your program's results.

Secondly . LOETO
I think you should remove

cin >> number,first,second,third,fourth;

Or

cin >> number>>first>>second>>third>>fourth;

And just use the

cin>>number;

As YOU only require to take in one number each time and get the first,second,third and fourth out from the main number.

As for the floor() thing, I think you are getting floor() confused with a rounding function. floor() always rounds down, so floor(3.812) would be 3, not 4. conversely, ceil() always rounds up. so ceil(3.224) would be 4, not 3.

if you used abs() to truncate the non-integral part of the number, you would just be doing the same thing that he's currently doing, only doing indirectly through the abs() function, and not on your own.

Well i am sorry .. MAde a bad comment on that one. I respond with DEEP apologies.

And edit my post as well

No need to apologize - just pointing it out. Everybody makes mistakes.

There is no need to use floor() as int truncates any decimal place as it is.

for any 4 digit number this should do the trick to extract all 4 digits

i=0;
while(number>0)
{
a[i]=number%10;
number=number/10;
i++;
}

i=0 will correspond to the last digit(4th in this case), i=1 to the 3rd and so on.

so your answer will be

ans=a[3]*3+a[2]*a[1]-a[0];

There is no need to use floor() as int truncates any decimal place as it is.

Is that method of truncation considered "safe"? I thought it wasn't a good idea to let C++ take care of the truncation for you when you pass a non-integral number to an int.

I am not sure, it has worked for me and I have never bothered to search. I will leave it for more experienced programmers to answer your question.

Thanks again guys
but the way i understand this question i have to put all six integers at once this way :325510671393289211112020; & then i have to separate them and calculate as indicated 3255*3 +1067*1393 -2892 and i think the answer should be 1493204

Er... at first you said

the given integers will all consist of four digits

. But you just mentioned six digits. And then you gave 325510671393289211112020, which is a 24 digit number, and then broke it up into six sections of four digits, treating each one like a "digit". Then you performed the digit operations that you mentioned (first*3 + second*third - fourth) on the first four of those "digits", and just ignored the last two. What exactly is the program supposed to do? Give an example of input, output, and an explanation, because what you just said contradicts what you said originally.

Sorry , like i said still new in programming


but the way i understand this question i have to put all six integers at once this way :325510671393289211112020; & then i have to separate them and calculate as indicated 3*3 +2*5 -5

little bit confused

Do you have the exact prompt with you? Could you post it, because I doubt that all six of the inputs would be put in at once without any breaks. I think they might mean that the digits within each of the six numbers need to be taken it at once, but not all the numbers together.

:icon_mrgreen: I made a much more efficient version using arrays (I don't know if you have used arrays but try to understand, it's quiet simple)

#include <iostream>
using namespace std;
int main()
{
int number[4];


cout << "Enter number: ";
cin >> number[0];
cin >> number[1];
cin >> number[2];
cin >> number[3];
cout << "\n\nThe answer is: " << number[0]*3 + number[1]*number[2] - number[3] << "\n\n";
system ("pause");
return 0;
}

I believe every four digit number had to be taken in at once, not each digit separately (otherwise your approach works). But I'm not sure exactly, so I'm waiting for him to try and clarify exactly what the program needs to do.

I think you are right about that the numbers need to be taken in all at once, but there is no way to do that using the cin function provided by #include <iostream>. This requires the use of some other reference source or library like allegro.

No it doesn't. He had the basic code for it somewhere above. You take in the number as one value using cin, and you yourself take it apart and figure out the different digits. If the number is four digits long, we can obtain the thousands place with floor(number/1000), the hundreds place with floor((number%1000)/100), the tens with floor(((number%1000)%100)/10), and the ones with floor(((number%1000)%100)%10).

You could get the whole thing into a string or an array and then convert it into Int.

Yes, that approach would be effective. However the OP said something about using modulus to find the answer, which you wouldn't need if you took the data in as a string.

So it would be good if the OP could post the exact promt.

#include <iostream>
using namespace std;

int main ( )
{
int number,dig1,dig2,dig3,dig4,second,third,fourth;
//For loop
for ( int x = 1; x <= 6; x++ )
{
cout << "Enter in number: ";
cin >> number;
// Obtain FIRST Digit
dig1 = number / 1000;
// Obtain SECOND digit
second = (number-dig1) / 100;
dig2 = second % 10;
// Obtain THIRD Digit
third = number / 10;
dig3 = third % 10;
// Obtain FOURTH Digit
fourth = number / 1;
dig4 = fourth % 10;
// The answer for the equation: (first digit multiplied by 3), plus (second digit multiplied by third didit) minus fourth digit
cout << (dig1 * 3) + (dig2 * dig3) - dig4 << endl;
return 0;
}
}

Please help guys this works only with the first four digit number (3255).
my problem is how to make or input the other five numbers
1067
1393
2892
1111
2020

i spend sleep less night to solve this program, i hope you understand my problem..

First, some stuff on cleaning up your code. Skip to the bottom for the solution I think you're looking for.

second = (number-dig1) / 100;
dig2 = second % 10;

looks like it should be just dig2 = (number-dig1*1000) / 100; So if your number is 3255, digit one would be 3, then 3255-3*1000 = 255, and then that divided by 100 equals 2 (with int truncation).

Then, for the third number, you have

third = number / 10;
dig3 = third % 10;

The variables second, third, and fourth seem unneccesary to me. Just do dig3 = ((number-dig1*1000)-dig2*100)/10 . That way, with the 3255 example, dig1 = 3, number-dig1*1000 = 255, and that minus dig2*100 = 55. 55/10 is 5, your third digit.

And a simmilar thing with dig4: dig4 = (((number-dig1*1000)-dig2*100)-dig3*10) .

The code you have may have worked too, but this seems less messy. There may be better ways still to do this, but w/e.

The reason that your program is only doing it once, is that you have return 0; at the end of your loop (in the loop, not after it). This means that after one iteration of the loop the program will terminate. Move return 0; to outside the loop, but still inside the main() function.

Thanks man
i will do as you say & come back to you

Thanx again CoolGamer48

You have made my day you have solved the problem.
keep up the good work

This question has already been answered. Start a new discussion instead.