PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] How to only consider the first N cipher of an int number?

 July 19, 2022     c, integer, numbers, types     No comments   

Issue

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{
...
}

Solution

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);
}


Answered By - Nikhil Gowda Shivaswmay
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing