I have an integer number, which can be codified on 3
or 4
ciphers (for example, 123
, 1234
and so on...) or can be codified on 7 or 8 cipher (for example, 1234567
, 12345678
and so on...).
Now, the problem is:
if the number is codified on 7 ciphers, I only need to save the first 3. For example:
1234567 -> 123
if the number is codified on 8 ciphers, I only need to save the first 4. For example:
12345678 -> 1234
How can I only save the first N cipher of the number into an other int value? Thank you everybody! I thought about something like this:
int number = 12345678;
if(number>9999999){ //if the number is codified on 8 ciphers
...
}else if(number>9999 && number<10000000){ \\if the number is codified on 7 ciphers
...
}else{
...
}
CodePudding user response:
you can make use do while loop. I believe cipher codified only on 7 and 8. If you still continue you can keep adding the code. As far as I understood, i tried to put into code here,
#include <stdio.h>
int main() {
long long n;
int num=3;
int count=0;
printf("Enter an integer: ");
scanf("%lld", &n);
if(n>9999999)
{
num=4;
}
printf("Number of cipher: %d", num);
// iterate at least once, then until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
do {
n /= 10;
count ;
} while (count!=num);
if(count==3)
{
n/=10;
}
printf("Number of cipher: %lld", n);
}