i'm trying to find out the highest and lowest number.. but compiler shows negative values in output when i enter a large number i.e 01234556
#include<iostream.h>
#include<conio.h>
void main()
{
int num,high,low,r,n,i,val;
clrscr()
cout<<"enter value";
cin>>val;
n=val;
high=n%10;
low=n%10;
n=n/10;
i=n;
do
{
r=i%10;
if(r>high)
high=r;
if(r<low)
low=r;
i=i/10;
}
while(i>=1)
cout<<"highest number is:"<<high<<endl;
cout<<"lowest number is:"<<low;
getch();
}
actually i've recently joined this forum and i've no idea how to write code while asking question.
#include<iostream.h>
#include<conio.h>
void main()
{
int num, high, low, r, n, i, val;
clrscr()
cout << "enter value";
cin >> val;
n = val;
high = n % 10;
low = n % 10;
n = n / 10;
i = n;
do
{
r = i % 10;
if (r>high)
high = r;
if (r<low)
low = r;
i = i / 10;
}
while (i >= 1)
cout << "highest number is:" << high << endl;
cout << "lowest number is:" << low;
getch();
#include <iostream> // should not include the ".h" at end
// this may be compiler dependant I'm not sure as I'm fairly new to
// programming
#include <conio.h>
usingnamespace std; // you need to include the standard library namespace
// or alternatively all the std library functions need to include a std:: prefix.
// example std::cout << "whatever";
int main() // <-- main function returns a value of type int (integer)
{
int num, high, low, r, n, i, val;
// removed line clrscr() as it wasn't included in headers and notice
// a missing semi colon after the function call.
cout << "enter value";
cin >> val;
n = val;
high = n % 10;
low = n % 10;
n = n / 10;
i = n;
do
{
r = i % 10;
if (r>high)
high = r;
if (r<low)
low = r;
i = i / 10;
}
while (i >= 1); // <-- while in do while loops require a semi colon after
// the expression.
cout << "highest number is:" << high << endl;
cout << "lowest number is:" << low;
_getch(); // apparently getch(); is depreciated and needed to be changed
// to _getch();
}
The results after running the corrected program was
highest number is:6
lowest number is:1
When the number you stated above was entered (01234556).
Why 0 wasn't the lowest I will leave up to you to figure out.
Final thoughts are this.
You should get a IDE with syntax highlighting as this will point out obvious code flaws.
You need to grab the one with the mingw included as that is the IDE with compiler included.
Please mark this thread as solved (button above the thread name) if you accept this answer. As it just makes it easier for others to identify if there is a ongoing investigation. We are here to help, help us help you. :P
As a budding programmer you are going to have to do a lot of this and it will be a big part of learning to grow into a experienced programmer. There are many tools available to find such information, one such tool is google.
I have personally not used the getch function in my programming, but I was instructed by my IDE that it has been depreciated and _getch was infact the newer version. That is one of the perks of using a IDE that displays relevant information.