Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,287 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 957 people online right now. Registration is fast and FREE... Join Now!




Check digit in number

 
Reply to this topicStart new topic

Check digit in number

Meldra
26 Nov, 2008 - 04:43 PM
Post #1

New D.I.C Head
*

Joined: 23 Nov, 2008
Posts: 7

Hi all,
I’m new to C++ and programming in general.
I have to write a program that convert a number (taken from user) from base number b=2, b=4, b=5, b=8 to decimal number .
My question is how can I check if given number is valid for convertation. For example,
Input:
21
Output:
From b=2 - not binary number;
From b=4 9
From b=5 11
From b=8 17.

Any suggestions are welcomed.


User is offlineProfile CardPM
+Quote Post


KYA
RE: Check Digit In Number
26 Nov, 2008 - 06:06 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
Half ass way: Check if the number only has ones and zeros, fails on numbers such as 10, 101, etc... but that can be accommodated

Better way: Define a isBinary() function to test if the number is an assembly of ones and zeros, i.e. base 2. You would probably have to do this for each base, so it won't be a blanket solution for the program. The real key to this problem isn't checking for conversion but the understanding of all the various bases and their relationships with one another.


User is offlineProfile CardPM
+Quote Post

Meldra
RE: Check Digit In Number
30 Nov, 2008 - 01:11 PM
Post #3

New D.I.C Head
*

Joined: 23 Nov, 2008
Posts: 7

Thank you for reply.
I got my program work. It wasn't so hard as it seemed.
I used this function for checking.
CODE

int check (int a, int d)//a given number, d digits from 9 to 1
{              
    int y;
    do
    {
            y=a%10;
            if (y==d) return y;
            else a /=10;
            }while (a > 0);
            return y;
}


This post has been edited by Meldra: 30 Nov, 2008 - 01:12 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 03:16PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month