Solved

Algorithm

Posted on 1998-06-08
4
365 Views
Last Modified: 2010-04-01
What is the best algorithm to check a number is in which particular range of:
0 to 9
10 to 99
100 to 999
1000 to 9999
10000 to 99999
...rate at exponential of 10

E.g X = 555 is in the range of 100 - 999

I use a if-else-then to do the checking but its looks terribly ugly.

Any advice?
0
Comment
Question by:engllf
4 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1165462
int n = 100000; // or another max number
while (n > 1)
    if ( x != x % n)
        break;
    else
        n /= 10;

x is in the reange n to n*10.
0
 
LVL 4

Expert Comment

by:sganta
ID: 1165463
Hi !

I have simple and beautiful algorithm and solution for your problem.

ALGORITHM
1. Read the number into "n";
2. Store this number n as a string into test
    i.e., test = string(n);
3. Get the length of the string into length
    length = string_length(n);
4. Now
    Range_From = power(10,length-1)
    Range_To     = power(10,length)-1
5. End of the algorithm

/* Here the solution in C++ */

#include <iostream.h>
#include <string.h>
#include <math.h>

void main(void)
{
   int n,len,ifrom,ito;
   char *test;

   cout << "Enter a number ";
   cin >>n;

   test = itoa(n); /* Converts integer to Ascii */
   len = strlen(test);
   ifrom = pow(10,len-1);
   ito = pow(10,len)-1;
   cout << "The number "<<n<<" is in the range from "<<ifrom<<" to "<<ito<<endl;
}

This  algorithm is tested and it works fine. If you are interested with my answer
Please reject the above answer and accept my answer.
Thank you.
JESUS LOVES YOU - sganta
0
 
LVL 11

Expert Comment

by:alexo
ID: 1165464
#include <math.h>
unsigned NumberOfDigits(unsigned n)
{
    return n ? log10(n) + 1 : 1;
}

0
 
LVL 1

Expert Comment

by:Booth882
ID: 1165465
here's one thats straight forward and has no limit to the size of the number:

#include <iostream.h>

int Power = 1;

cin >> int TheNumberToBeChecked;

while(1)
{
    if(TheNumberToBeChecked >= 10 ^ Power)
        Power++;
    else
        break;
}

try it and see what you think
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
gdb doesn't stop on breakpoint 2 92
Error creating a new C++ project in ,net 20 41
Gaming Software 1 18
I could not set window to top 4 20
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question