modulo and divide looping?

if a number is divided by 10 the remainder will
be the last digit.e.g 487 % 10 is 7. dividing
the number by 10 and then finding remainder then
487/10 =48%10 gives 8.use this logic and proceed
using loops to store input number and base in array
format and then perform the calculation.

i want to maximize my number of digits not just three, i will be tired of making and naming a lot of variables without a loop, help me to debug my code here

this code i have made here is fixed on accepting only 3-digit numbers, i want to maximize its digit accepting capacity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <string.h>
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <math.h>

using namespace std;
int numr[100],base,i,ii,numpls,numans,nf,nmin,nff,nf2,nf3;
int main()
{
cout<<"Input number: ";
cin>>numans;
cout<<"\n";    
cout<<"Input base: ";
cin>>base;
ii=i;
  
   nf=numans%10;
    cout<<nf;//3
    cout<<"\n"; 
   nf=numans/10;
   nff=nf%10;
    cout<<nff;//2
    cout<<"\n"; 
   nf=numans/10;  
   nf3=nf/10;
    cout<<nf3;
    cout<<"\n";   
   
                  
  //numpls=numr[i]^base;
  
  
  
  //cout<<numr[numans];
{getch();}
}
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()
{
int base,i,numans;
char numr[20];
cout<<"Input number: ";
cin>>numans;
cout<<"\n";    
cout<<"Input base: ";
cin>>base;
i=0;
  while(numans)
  {
	  numr[i]=numans%base;
	  numr[i]+=numr[i]>9?55:48;
	  i++;
	  numans/=base;
  }
  while(i)
	  cout<<numr[--i];
	system("pause");
	return 0;
}                                                           
Last edited on
if you're using the int datatype, you're capped somewhere around 8 or 9 digits i believe.
what you need is a for loop, your current array can hold 100 digits it appears, what you would want to do is start at the end [99], and go down to [0] so something like this might work...
1
2
3
for (int i = 99; i >= 0 ; --i){
     numr[i] = numans%base;
     numans /= base;}


but like i said, int is limited in size for your numbers.
you should also be sure to throw in 0's for the unused portion of the array, and you'll want it to not print the beginning zeros.

also, because of the nature of arrays, you can simply write in
 
cout << numr;

to output your array... however this will display your beginning zeros.
alternatively you can use another for() loop with a nested if() statement to display your number or you could use a while loop to find the first actual number, then output from there using a for loop.
Last edited on
Topic archived. No new replies allowed.