Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Problem - for, while and do-while problem

Name: Anonymous 2012-03-13 13:20

Hi guys, so basically I have a little problem. I wrote a systematic program designed to convert base 5 numbers to base 10, (but it'll function with mostly any bases)

here's the code:
#include "stdafx.h"

#include <conio.h>


#include <stdlib.h>




int convert(int a,int base);


int main()
{
 int a,base,ans;
 
 printf("\nEnter your preferred number: ");
 scanf("%d", &a);

 printf("\nEnter the base of that number: ");
 scanf("%d", &base);

 ans=convert(a,base);
    
 printf("The converted base 10 is: %d", ans);
 getch();
 return 0;
}  


int convert(int a,int base)
{
int sum=a%10;
for(int i=base;(a/=10)!=0;i*=base)
sum+=a*i;
return sum;
}

//it compiles and works.

the issue with this is that im also supposed to switch the for conditional, and make it work with while, and do-while, and acquire the same results... and ive literally tried everything.

int convert(int a,int base)
{
int sum=a%10;
int i = base;
        
while((a/=10)!=0){
i*=base;
sum+=a*i;
}
return sum;
}


int convert(int a,int base)
{
int sum=a%10;
int i = base;
do{
i*=base;
sum+=a*i;   
}while((a/=10)!=0);

return sum;
}

yes so this is my dilemma. so if anyone is willing to help, it will be greatly appreciated.

Name: Anonymous 2012-03-13 13:27

well im actually using the borland compiler to run this so the stdafx.h is unnecessary and this point, and no this is not my homework. im majoring in math, and im just interested in programming. if i cant ask for help, then whats the point of this board?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List