I need to desigen a program which it must display positive, negative or zero when user enter the number with a function name called numtest. when i enter number 0 it still appears as, number negative. Can anyone help me on this. Thank you.
#include <iostream>
using namespace std;
float numtest(float a, float b, float c)
{
if(a>1)
return a;
if(b<1)
return b;
if(c=0)
return c;
}
int main()
{
float num;
cout<<"Please enter number";
cin>>num;
if(num>1)
cout<<"number is positive";
if(num<1)
cout<<"number is negative";
if(num=0)
cout<<"number is zero";
return 0;
}
First of all, when checking equality you must use == instead of =.The = sign assigns the R.H.S. to the L.H.S. Eg. c=0 changes the value in c to be 0, so instead it should be if(c==0).
Now, if you want to check positive numbers, the condition should be if(num>0), and for negative numbers, it should be if(num<0).
Lastly, try to use else if and else instead of three ifs.
Looks like you've made the age old problem of assigning a 0 to num when you want an equality comparison.
if(num == 0){ ... }
Also, think of what happens when you enter 0.5 or -0.5. And in your question you mention using your function (numtest) to do something, but your not even calling it in your main function
I believe your issue would be solved if you changed your parameters to:
if(a>0)
if(a==)
if a<0)
Because they way you have it set now you are not accounting for the infinite amount of numbers between 0 and 1 (.10 , .0000005 , etc). And we want to use the "==" sign to so equality.
Hope that helps!
Your program is not using the numtest() function and unwanted variable used and for testing number is +ve or -ve you should use
For Positive: if(num>0) or if(num>=1) in case of floatif(num>=0.001)
For Negative: if(num<0) or if(num<=-1) in case of floatif(num>=-0.01)
For Zero: if(num==0)
Try the below program:
#include <iostream>
#include<conio.h>
void numtest(float num)
{
if(num>0)
cout<<"number is positive";
if(num<0)
cout<<"number is negative";
if(num==0)
cout<<"number is zero";
}
void main()
{
float n;
cout<<"Please enter number";
cin>>n;
numtest(n);
getch();
}
Related
The question is to determine if the equation a*x+b=0 has 1 solution, and write it, if it has 0 solutions write no solutions and if it has infinite solutions type infinite solutions
#include <iostream>
using namespace std;
int main()
{
float a,b,x;
cin>>a>>b;
x=-b/a;
if(x!=0)
cout<<x<<endl;
else if(a==0)
cout<<"no solution"<<endl;
else
cout<<"infinite solutions"<<endl;
return 0;
}
Its supposed to write "no solutions" but instead it says -inf
Your checks are in the wrong order. Since you are not specifying your input, my assumption is:
a=0;
b=1; // Or any other number, for that matter
This means, for your input, you are dividing -1 by 0 which is -inf (Mathematically incorrect, but floating point numbers (sometimes) work that way, see The behaviour of floating point division by zero)
If-else chains are evaluated from top to bottom, stopping at the first condition evaluating to true.
As you are checking x!=0 first and -inf is not equal to 0, you just get the output of -inf.
To ensure you catch the division by zero case, you need to check for that first.
Your code would look like this then:
#include <iostream>
using namespace std;
int main()
{
float a,b,x;
cin>>a>>b;
x=-b/a;
if(a==0)
cout<<"no solution"<<endl;
else if(x!=0)
cout<<x<<endl;
else
cout<<"infinite solutions"<<endl;
return 0;
}
I'm trying to write a program that prints the factorial of a number:
#include <iostream>
using namespace std;
int main() {
int ans,fact=1, number;
cin>>number;
for (int i=1; i<=number; i++) {
fact = fact*i;
}
ans = fact%998244353;
cout <<ans<<endl;
return 0;
}
When I try to input the number 250, the program returns 0, but when I try to print a smaller number like 4, it returns the right number. Does anyone know why this occurs and how to fix this? Thanks.
Some hints for doing math in a finite field or ring while avoiding overflow.
Mathematically is true that:
(a*b)%c == ((a%c)*(b%c))%c
and also:
(a+b)%c == ((a%c)+(b%c))%c
But in the case a*b or a+b will overflow, you have to use the righthand expression because it is less likely to overflow.
This particular Code was made on Dcoder on Android...
My question is,How am I still able to execute it if my input for n is less than 6..(Condition i>=6 is not fulfilled for the for loop right..)Also using this code I always get the answer as 1,2,3,5,8... and the number of terms printed is always more than the input of n..
Also I tried putting i<=0 but I get the same results...
#include <iostream>
using namespace std;
int a=0,b=1,x,i,n;
int main()
{
cout<<"This Program Gives You The List Of First 'n' Fibonacci Numbers:"<<endl
<<"Enter The Value Of 'n':"<<endl;
cin>>n;
if(n<1)
{
cout<<"Invalid Input"<<endl<<"Please Restart This Program And Enter A Natural Number."<<endl;
}
else
{
cout<<"The First "<<n<<" Fibonacci Numbers Are:"<<endl;
for(i;i>=6,i<=n;i++)
{
x=a+b;
a=b;
b=x;
cout<<x<<endl;
}
}
return 0;
}
But surprisingly the code below works..Why? And What is the fundamental difference between the two except that I intentionally print 0 and 1 in the second code...?Also I didn't find any difference when I used post increment and pre increment in my For Loop..Why?Also It would be really helpful to get some examples which behave differently with post and pre increment...
#include <iostream>
using namespace std;
int a=0,b=1,x,i,n;
int main()
{
cout<<"This Program Gives You The List Of First 'n' Fibonacci Numbers:"<<endl
<<"Enter The Value Of 'n':"<<endl;
cin>>n;
if(n<1)
{
cout<<"Invalid Input"<<endl<<"Please Restart This Program And Enter A Natural Number."<<endl;
}
else
{
cout<<"The First "<<n<<" Fibonacci Numbers Are:"<<endl;
cout<<"0"<<endl<<"1"<<endl;
for(i;i>=0,i<=n-2;i++)
{
x=a+b;
a=b;
b=x;
cout<<x<<endl;
}
}
return 0;
}
Your use of the comma operator is a problem and is leading you to believe that the condition is actually enforced.
This statement
i>=6,i<=n;
ignores the result (false if n==6) from i>=6, despite evaluating it and then proceeds to check if i<=n because that's how the comma(,) operator works in this context. Thus your loop still prints values when n is <=6. What you are looking for is
i>=6 && i<=n;
The && is the Logical AND operator which means that both the conditions need to be true for the statement to be true (and doesn't discard the Left Hand Side condition obviously).
As for why that loop runs for 7 times (one more than n if n is 6) that's because your loop essentially becomes:
for(i = 0; i <= 6; i++)
which shall run 7 times, starting from 0.
The same thing happens with your second piece of code, only this time that loop is essentially
for(i = 0; i<= n- 2;i++)
So, for a value of n as 6, you would have 5 iterations, which is what you see, i.e. the 5 terms after 0 and 1
This Program Gives You The List Of First 'n' Fibonacci Numbers:
Enter The Value Of 'n':
The First 6 Fibonacci Numbers Are:
0
1
1
2
3
5
8
I'm writing a program for finding whether a given number is an Armstrong Number:
int main()
{
int n,rem,sum=0;
cout<<"Enter the Number for checking"<<endl;
cin>>n;
while(n!=0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==n)
{
cout<<"Armstrong Number"<<endl;
}
else
{
cout<<"It's not a armstrong number";
}
return 0;
}
When I run it, it always reports "It's not a armstrong number", regardless of input.
I changed the code as follows, and got the correct result. But I don't understand why I need to assign input to n1 and do the operation - why can't I directly do the operation with n?
int main()
{
int n,rem,sum=0,n1;
cout<<"Enter the Number for checking"<<endl;
cin>>n;
n1=n;
while(n1!=0)
{
rem=n1%10;
sum=sum+(rem*rem*rem);
n1=n1/10;
}
if(sum==n)
{
cout<<"Armstrong Number"<<endl;
}
else
{
cout<<"It's not a armstrong number";
}
return 0;
}
In the line if (sum==n) your program compares sum and n. In the second program n is initial number entered by user. But in the first program n==0 (see the loop above it).
So, in the first program the check if (sum==n) works as if (sum==0). But value of sum is never 0 (except user entered 0). So, first program always returns "It's not a armstrong number".
And about style: It is much better to use functions instead of putting the whole logic into one main() function. For instance, you can create a function for calculation of the intermediate sum for cheching of Armstrong Number:
int getSumOfCubesOfDigits(int n)
{
int sum = 0;
while (n)
{
const int rem = n % 10;
sum += rem * rem * rem;
n = n / 10;
}
}
In this case your program will be much simpler and it will be hard to make the mistake you have in the first program of your question:
int main()
{
int n;
cout << "Enter the Number for checking" << endl;
cin >> n;
if(getSumOfCubesOfDigits(n) == n)
cout<<"Armstrong Number"<<endl;
else
cout<<"It's not a armstrong number";
return 0;
}
In the first program, the original number is entered into 'n'. The only problem in your logic is, you forgot that by the time you exit from the while loop, 'n' will no longer be your original number since you are repeatedly doing n=n/10, and hence 'sum==n' never satisfies even for an Armstrong number.
So before you enter the while loop, save the original number into another variable, say n1 (as done in the second program you provided), and only use n1 for operations, ie, n1=n1/10. Leave n alone so that, in the end 'n' will still contain the original number, which you can finally compare with 'sum' to find your answer.
Which number do you compare ? , in first program in while loop , n value is changed ( in this variable you get the input) and finally check with sum == n , so it always get condition fail.
So temp (n1) variable is required , to compare the final result
Your code is different in the second code block, you are still testing if sum=n.
In the second code block, if you tested if(sum=n1), I would suspect it would work the same.
I got this solution for finding an Armstrong Numbers
int main() {
for (int i=10; i<=9999; i++) {
int k = i,z = 1, s = 0, n = i;
while ((k/=10) > 0) z++;
for (int t = z; t; t--, n/=10) {
s += pow(n % 10, z);
}
if (i == s) cout << i << endl;
}
return 0;
}
I'm trying to write code to count the number of times a whole number can be divided by 2 before reaching 1.
When I run my code it prompts me to enter a number as you'll see in my code below, but once I do so, nothing happens, just a blank line appears. Does anyone know what I'm doing wrong?
#include<iostream>
Using namespace std;
int main()
{
float x,i=0,num=0;
cout<<"please enter num:";
cin>>x;
while(x>0)
{
if(x/2>1)
num+=i;
i++;
}
cout<<"Number of times "<<x<<"is divisible by 2 is:"<<num<<endl;
return 0;
}
There are twp problems:
1) you never update x, so you'll iterate a long time.
2) x/2>1 even if x is not divisible by two. Consider use of modulo for the test
For example:
...
while(x>1 && fmod(x,2)==0) // sorry % is for integers only
{
num++;
x /= 2;
}