-1

I have an integer value, such as 5794. I want apply a several of calculations on each number without using array

Example

int encrypt;
cout << "Enter 4 digit numbers to encryption :> ";

cin >> encrypt;

for(int i=1;i<=encrypt;i++){
   cout << encrypt  << endl;
}

This code prints the whole number, but I want to print each digit without using array. If the number is 5794, I want to display:

5
7
9
4

I do not want it to be displayed like this:

5794
andand
  • 17,134
  • 11
  • 53
  • 79
Lion King
  • 32,851
  • 25
  • 81
  • 143

5 Answers5

4

Hint: Use modulus (%) and division (/) operator.

If given number has four digit then divide it by 1000,100,10,1:

std::cout << "\n" << encrypt/1000%10;
std::cout << "\n" << encrypt/100%10;
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • thanks, but I do not mean this, I want print number like 5487 each number individually not all the numbers together with some. This is the simplest thing I want – Lion King Nov 28 '11 at 03:35
1

well, if you have the number 5794, you need to mathmatically extract the digits.

HINT: What happens if you divide the number by 1000?

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

Scan number, then you can extract each digit using integer division and reminder (%) operators.

Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96
0

is this you want?:

// get the number from stdin, std::cin >> v; 
visitNumber(int v)
{
    int v1 = v;
    size_t len = 0;

    while(v1){
        v1 /= 10;    
        ++ len;
    }

    for(int i=len; i>0; --i){
        std::cout << (v/pow(10,i)) << std:endl; // or some other op you favorate
    }
}
Haiyuan Li
  • 377
  • 2
  • 10
  • thanks, but I do not mean this, I want print number like 5487 each number individually Not all the numbers together with some. This is the simplest thing I want – Lion King Nov 28 '11 at 03:37
-1

Since you have troubles putting other replier's suggestions into code:

int divisor = 1000;
std::cout << "enCRAPtion digits:\n";
for (unsigned int j = 0; j < 3; ++j)
{
    cout << (encrypt / divisor) << "\n\n";
    encrypt = encrypt % divisor;
    divisor = divisor / 10;
}

By the way, the word is "encryption" not "encraption".

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Thanks for your help, your code is the closest to the correct, But there is a problem , when print the number as this way, example if the number is (4567), Printed in this way 4 45 456 4567 and I want like this 4 5 6 7 . – Lion King Nov 28 '11 at 05:14