I have a little project to do, I've created a program that when the user types a number, it will say if it's even or odd. I even used a function. Here's my question:
How do I use integer arguments in a function for the program? (My program does work, it just doesn't use integer arguments.)
Instructions
Write a C++ function that accepts an integer argument, determines whether the passed integer is even or odd, and displays the result of this determination. (Hint: Use the % operator.)
Make sure your function is called from main(). Test the function by passing various data to it.
My code
#include <iostream>
using namespace std;
void oddEven() //My Function
{
int num;
cout << "Please enter a number " << endl;
cin >> num;
if (num % 2)
{
cout << "It's odd" << endl;
}
else
{
cout << "It's even" << endl;
}
}
int main() //Main program
{
oddEven(); //Calling my Function
return 0;
}
Sample program:
#include <iostream>
using namespace std;
void oddEven(int num/*num is integer argument*/){
if (num % 2)
{
cout<<"It's odd"<<endl;
}
else
{
cout<<"It's even"<<endl;
}
}
int main() //Main program
{ int x;
cout<<"Please enter a number "<<endl;
cin>>x;
oddEven(x); //Calling my Function
return 0;
}
Here's the code
#include <iostream>
using namespace std;
void oddEven( int num ) //My Function
{
if (num % 2)
{
cout << "It's odd" << endl;
}
else
{
cout << "It's even" << endl;
}
}
int main() //Main program
{
int num;
cout << "Please enter a number " << endl;
cin >> num;
oddEven(num); //Calling my Function
return 0;
}
Related
My prototype doesn't work, probably cause I'm unable to do the referencing of values properly. I'm trying to add a function called num_pos to return number of even values entered but I have no idea where to start
#include <iostream>
using namespace std;
void entry(double arry[],int size);
//Additional function prototype when needed
void displayEntry(double dtAry[],int sz) ;
int main()
{
int size = 6;
double values[6];
cout << "Data Analysis" << endl;
cout << "=============" << endl<<endl;
entry(values, 6);// Call function to allow user to enter the readings
// Additional function calls when needed
return 0;
}
// Your function implementaiton here
void entry(double arry[],int size)
{
int i;
for(i=0;i<size;i++)
{
cout << "Enter Reading " << i+1 << ": ";
cin>> arry[i];
}
return;
}
void displayEntry(double dtAry[],int sz)
{
cout <<"You have entered the following readings: ";
for(int i=0;i<sz-1;i++)
{
cout << dtAry[i] <<", ";
}
}
Consider the following code:
#include <iostream>
using namespace std;
int main(){
int a,b;
cout << "Enter two positive numbers:" <<endl;
cin >> a >> b;
if (a<b) cout <<a<<" is less than "<< b<<endl;
else if (a>b) cout <<a<<" is greater than " <<b<<endl;
}
How can I make the program endlessly repeat asking for a new set of numbers as input?
Here's the simplest way of doing what you want (there are other ways). Basically, you just need to 'wrap' the code that you want to repeat in a loop, where the 'test' condition for the loop will always evaluate to true.
Note the comments with "///" I've given:
#include <iostream>
//using namespace std; /// Search this site for "Why using namespace std is bad"
using std::cout;/// Just declare usage of those feature you ACTUALLY use...
using std::cin;
using std::endl;
int main() {
int a, b;
while (true) { /// The test condition will always be "TRUE" so the loop will never end!
cout << "Enter two positive numbers:" << endl;
cin >> a >> b;
if (a < b) cout << a << " is less than " << b << endl;
else if (a > b) cout << a << " is greater than " << b << endl;
// cout /// This line is wrong!
}
}
Feel free to ask for further clarification and/or explanation.
Depends on what exactly do you want your program to do. If you want it to "deny access". For example lets say you have want a number K > 3 always for the program to continue. The all you have to do is use a do- while loop:
do
{
cout << "Enter the value for the sequence: ";
cin >> K;
if ( K <= 3)
{
cout << "Write a bigger number!" << endl;
}
} while(K <= 3);
Otherwise just use a normal loop with the condition suitable for the task.
Suppose your program is to find the Factorial of number and you want it to loop such that it ask for new value from the user
int main()
{
int n;
while (true) {
int factorial = 1;
cin >> n;
if (n==0) {
cout << 0;
}
else {
for (int i=n;i>0;i--) {
factorial = factorial*i;
}
cout << factorial;
}
}
return 0;
}
I'm trying to make this recursive program count how many times it calls itself, and I was going to use a global variable to keep count but eclipse isn't recognizing it for some reason. Here's my code:
#include <iostream>
#include <cstdlib>
using namespace std;
int count = 0;
int fib(long int);
int main()
{
long int number;
cout << "Enter a number => ";
cin >> number;
cout << "\nAnswer is: " << fib(number) << endl;
return 0;
}
int fib (long int n)
{
//cout << "Fibonacci called with: " << num << endl;
if ( n <0 )
{
cout <<" error Invalid number\n";
exit(1);
}
else if (n == 0 || n == 1)
return 1;
else{
count++;
return fib(n-1) + fib(n-2);}
cout << count;
}
Whenever I initially declare count, it doesn't even recognize it as a variable, does anybody know the reason for this?
Your problem is here:
using namespace std;
It brings in std::count from the algorithm header, so now count is ambiguous. This is why people are told not to do using namespace std;. Instead, remove that line and put std::cout instead of cout (and the same for cin and endl).
I was writing a code that calculates the two factors of any given number using two nested 'While' loops but after just one iteration the loop just stops
Program
#include <iostream>
#include <conio.h>
using namespace std;
long int Password;
void main()
{
long int n=2,n1=2;
cout<<"Type the number whose factor you need"<<endl;
cin>>Password;
while( n < 3600 )
{
while( n1 < 3600 )
{
if( n*n1 == Password )
{
cout<<"your Factors are "<<n<<" and "<<n1<<endl;
getch();
}
else
{
n1++;
break;
}
}
n++;
}
}
Output is only working for small numbers but when a little big numbers are inserted the program terminates. I am not understanding the problem as the code is perfectly fine. Am I having a less powerful processor?
Your program is not calculating the factors of any given number. Also, naming the number "Password" is confusing.
Maybe you want something like this in C++:
#include <iostream>
using namespace std;
int main() {
unsigned int number;
cout << "Enter a positive integer whose factors you need: " << endl;
cin >> number;
cout << "Factors of " << number << " are ";
for (int i = 1; i <= number; ++i) {
if (number % i == 0)
cout << i << " ";
}
cout << endl;
return 0;
}
As was noted in the comments, in C++ you have int main() not void main(), even though some compilers do support void main().
I try to write a factorial using recursive function. What is wrong with this code?
I get error return-statement with a value, in function returning 'void' [-fpermissive]
#include <iostream.h>
int factorial(int);
int factorial(int number)
{
return number==0?1: number * factorial*(number - 1);
}
void main(void)
{
int number;
cout << "Please enter a natural number: ";
cin >> number;
if (number < 1)
cout << "That is not a natural number.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;
return 0;
}
The function main should return int, not void. Also void within parenthesis is useless in C++.
Use:
int main() {
or:
auto main() -> int {
fix the code.
main must be int and function argument should be entered immediately without * in between:
#include <iostream.h>
int factorial(int number)
{
return number==0?1: number * factorial(number - 1);
}
int main(void)
{
int number;
cout << "Please enter a natural number: ";
cin >> number;
if (number < 1)
cout << "That is not a natural number.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;
return 0;
}