integers and loops

Hi

would you kindly help me to wright the following Program.
The program must 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 generate the code 7, because 3*3+1*2-4=7.Use a for loop iterating NB: You may not input the digits seperately, but should input the number as a whole.(Hint: use / and % in the calculations.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream.h>
using namespace std;

int main()
{
  int num, n1, n2, n3, n4;
  cin >> num;
  n1=(num-(num%1000))/1000;
  n2=((num-(n1*1000))-((num-(n1*1000))%100))/100;
  n3=((num-(n1*1000)-(n2*100))-(num-(n1*1000)-(n2*100))%10)/10;
  n4=(num-(n1*1000)-(n2*100)-(n3*10));
  cout << 3*n1+n2*n3-n4 << endl;
  return 0;
}
excellent it works, thanks
Topic archived. No new replies allowed.