How to get leftmost number from an integer

Hello

I got one type of questions, I did it right but one thing I couldn't know how to do it, actually I am not that good in math so I couldn't find a right equation for it.

Question says:
Write a program that reads a positive integer number of any digit size and do the following:
a. Print the sum of its digits.
b. Print the average of its digits.
c. Print the leftmost digit.
d. Print the maximum digit.

If the input number is negative, convert it to positive.


And this is a sample of Input/Output:
Enter a positive integer: 1246
The Sum is 13
The average is 3.25
The leftmost digit is 1
The maximum is 6


Well, I did it all except (c), I couldn't find a proper equation to find leftmost digit.

Here is what I have done so far
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
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int Num, Sum=0, LM=0, MAX=0, SumTool=10, X=0, NumDup;
	double AV=0;

	cout<<"Enter a positive integer: ";
	cin>>Num;

	if(Num < 0)
	{
		Num*=-1;
		cout<<"\nNotice: Number converted to positive \""<<Num<<"\"."
			<<endl;
	}

	NumDup=Num;
	while(NumDup != 0)
	{
		Sum+=(NumDup%10);
		NumDup/=10;
	}

	NumDup=Num;
	while(NumDup != 0)
	{
		NumDup/=10;
		X++;
	}

	NumDup=Num;
	while(NumDup > MAX)
	{
		if(MAX < NumDup)
			MAX= Num%SumTool;
		NumDup/=10.0;
	}


	cout<<"The Sum is "<<Sum
		<<"\nThe average is "<<setprecision(2)<<fixed
		<<showpoint<<(double)Sum/X<<endl;
	cout<<"The leftmost digit is "<<LM
		<<"\nThe maximum is "<<MAX<<endl;

	return 0;
}
Divide it by (the integer) ten until the answer is zero; you want the value it had right before that last division.
Last edited on
You don't need an "equation" for the leftmost:
1
2
3
4
5
6
7
8
	int LeftMost = 0;
	NumDup=Num;
	while(NumDup != 0)
	{
		LeftMost = NumDup;
		NumDup/=10;
		X++;
	}
Topic archived. No new replies allowed.