Question

1. Write a program that takes a number as input and check whether the number is...

1. Write a program that takes a number as input and check whether the number is positive, negative or zero.

2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd.

3.  Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3.

4. Write a program that takes a number as input and prints its multiplication table up to 10.

5. Write a function to read an integer n and print the factorial of n.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

Code:

#include<iostream>
using namespace std;
int main(){
   int num;
   cout << "Enter a number: ";
   cin >> num;
   if(num > 0){
       cout << "The entered number is positive." << endl;
   }
   else if(num < 0){
       cout << "The entered number is negative." << endl;
   }
   else{
       cout << "The entered number is zero." <<endl;
   }
}
Output:

Explanation:

2.

Code:

#include<iostream>
using namespace std;
int main(){
   int num;
   cout << "Enter a number: " ;
   cin >> num;
   if(num % 2 == 0){
       cout << num << " is an even number." << endl;
   }
   else{
       cout << num << " is an odd number." << endl;
   }
}

Output:

Explanation:

3.

Code:

#include<iostream>
using namespace std;
int main(){
   int choice;
   cout << "Enter a number 1, 2 or 3" << endl;
   cin >> choice;
   switch(choice){
       case 1:
           cout << "Red" << endl;
           break;
       case 2:
           cout << "Yellow" << endl;
           break;
       case 3:
           cout << "Green" << endl;
           break;
       default:
           cout << "Invalid choice" << endl;
           break;
   }
}

Output:

Explanation:

4.

Code:

#include<iostream>
using namespace std;
int main(){
   int num, i;
   cout << "Enter a number: ";
   cin >> num;
   for(i=1; i<=10; i++){
       cout << num << " * " << i << " = " << num * i << endl;
   }
}

Output:

Explanation:

5.

Code:

#include<iostream>
using namespace std;
int factorial(int n);
int main(){
   int n, result;
   cout << "Enter a number: ";
   cin >> n;
   result = factorial(n);
   cout << result << endl;
}
int factorial(int n){
   if(n < 0){
       return 0;
   }
   else if(n == 0){
       return 1;
   }
   else{
       return n*factorial(n-1);
   }
}
Output:

Explanation:

Add a comment
Know the answer?
Add Answer to:
1. Write a program that takes a number as input and check whether the number is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...

    Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch statement and functions. 2. Write a program to find the summation of N numbers. Use two functions. One function will take the input from user and the other will perform the summation from 1 to N. 3. Write a program to find the factorial of a number. Use two functions. One function will take the input from user and the other will perform the...

  • Write a program which prompts the user to enter a number of rows, and prints a...

    Write a program which prompts the user to enter a number of rows, and prints a hollow square star pattern with 2 diagonals. Note: you can assume that the integer will always be > 1 and will be an odd number. For example: Input Result 5 Enter number of rows: 5 ***** ** ** * * * ** ** ***** 9 Enter number of rows: 9 ** ** ** * * * * * * * * * * **...

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • Write a C program that takes a positive integer n as input and prints all the...

    Write a C program that takes a positive integer n as input and prints all the prime numbers from 1 to n. Sample Input/Output 1: Enter your n: 20 Prime number(s) from 1 to 20 : 2 3 5 7 11 13 17 19 Sample Input/Output 2: Enter your n:2Prime number(s) from 1 to 2 : 2

  • C++ only Implement a program that prompts a user to enter a number N where N...

    C++ only Implement a program that prompts a user to enter a number N where N is a positive number. The program must validate a user input and ask a user to re-enter until an input is valid. Implement a function that pass N as an argument and return a result of calculates N! (N-factorial): The function must use loop for the implementation. Hint: Factorial: N! = N * (N-1) * (N-2) * …. * 1 ( This is only...

  • Write a Java program for a fortune teller. The program should begin by asking the user...

    Write a Java program for a fortune teller. The program should begin by asking the user to enter their name. Following that the program will display a greeting. Next, the program will ask the user to enter the month (a number 1-12) and day of the month (a number 1-13) they were born. Following that the program will display their zodiac sign. Next, the program will ask the user their favorites color (the only choices should be: red, green and...

  • please solve using python thank you! Write a program which prompts the user to enter a...

    please solve using python thank you! Write a program which prompts the user to enter a number of rows, and prints a hollow square star pattern with 2 diagonals. Note: you can assume that the integer will always be > 1 and will be an odd number. For example: Input Result 5 Enter number of rows: 5 ***** ** ** * * * ** ** ***** 9 Enter number of rows: 9 ** ** ** * * * * *...

  • Write code that prompts the user to input a number. The program should then output the...

    Write code that prompts the user to input a number. The program should then output the number and a message saying whether the number i "Positive, "Negative" or "Zero". It should further output whether the number is odd or even (zero is counted as even) For example:

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT