Hello all, I need help with a scientific notation problem. I am to create a program where the user enters a 5 digit number, and it is displayed in scientific notation. This part is easy, however I am trying to take it to the next step and make it so '0's do not show up.

So 40587 = 4x10000 + 5x100 + 8+10 + 7
*notice the 0 is not in the equation.

well I got quite far in my code, and got it to finally work. HOWEVER although it works, I still get an error message when the code runs..

Help?

#include<iostream>
using namespace std;
int main()
{
	int number, notation=10000;
	cout << "Enter a five digit number: ";
	cin >> number;
	cout << endl << number << " = ";
	for(int x=0;x<5;x++){
		if((number/notation)%10 != 0){
		cout << (number/notation)%10;
		if(notation > 1)
			cout << "x" << notation;
		if(((number/(notation/10))%10 != 0)||(notation != 10))
			cout << " + ";
		}
		notation /= 10;
	}
	cout << endl << endl;
}

Whats the error message? And all this math can be avoided by using string.

void printN(char ch, int N){
 while(N--) cout << ch;
}

int main(){

string num = "40587";

for(unsigned i = 0; i < num.size() - 1; ++i){
  if(num[i] != '0' ){
      cout << num[i] << "x1";
      printN('0',num.size() - i - 1);
      cout << " + ";
  }
 }
 cout << num[num.size()-1] << endl;

}

Edited 6 Years Ago by firstPerson: n/a

Whats the error message? And all this math can be avoided by using string.

void printN(char ch, int N){
 while(N--) cout << ch;
}

int main(){

string num = "40587";

for(unsigned i = 0; i < num.size() - 1; ++i){
  if(num[i] != '0' ){
      cout << num[i] << "x1";
      printN('0',num.size() - i - 1);
      cout << " + ";
  }
 }
 cout << num[num.size()-1] << endl;

}

God, I know like none of this stuff. Lol, I only took one class in C++. But yeah, I got my program to work actually. Thanks for helping, even though I understood next to none of the code, haha.

using namespace std;
void printN(char ch, int N){
 while(N--) cout << ch;
}

this part is so cute. N=numbersize()-i-1
he puts in while N-- for making exact number of zero with char.
if i am right, it is so amazing

This article has been dead for over six months. Start a new discussion instead.