Don't like ads? PRO users don't see any ads ;-)

Untitled

By: Mohamed_Samy on May 7th, 2012  |  syntax: None  |  size: 0.65 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int x,base,n,m,sum=0,origbase,origx;
  7.     printf("Welcome to Converting to Decimal Program\n");
  8.     printf("Enter your Base [2-10]: ");
  9.     scanf("%d",&base);
  10.     printf("Enter a number in Base %d: ",base);
  11.     scanf("%d",&x);
  12.     origbase=base;
  13.     origx=x;
  14.     while(x>0){
  15.     x=x/10;
  16.     m=x%10*1;
  17.     if(m>=base){
  18.         printf("\nInvalid number");
  19.         break;
  20.         }
  21.     else{
  22.         n=m*base;
  23.         base=base*base;
  24.         sum=sum+n;
  25.         }
  26.         }
  27.         sum=sum+origx%10;
  28.         printf("%d (base %d) = %d (decimal)",origx,origbase,sum);
  29.     return 0;
  30. }