Question

Write some C++ program segments that solves the indicated tasks (you do not have to write...

Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough).

a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen.


a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive, and zero otherwise.


b) A program to print all even integers between 1 and 30 on the screen


c) A program to reads a real number as input and adds it to a running total until the user enters the number -1. At that time the program should print out the final sum (not including, of course, the number -1).

Homework Answers

Answer #1

Please look at my code and in case of indentation issues check the screenshots.

---------------main1.cpp---------------

#include <iostream>
using namespace std;

/*
a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen.
*/


int main()
{
   double number;
   cout << "Enter a double number: ";
   cin >> number;                           //read double number

   if(number > 0)                           //make a decision using if else
       cout << "The number " << number << " is a positive number" << endl;
   else if(number < 0)
       cout << "The number " << number << " is a negative number" << endl;
   else
       cout << "The number " << number << " is a zero" << endl;
   return 0;
}

---------------main2.cpp---------------

#include <iostream>
using namespace std;

/*
A function isPositive that takes as input a double number and returns the integer 1 if the number is positive, and zero otherwise.
*/

int isPositive(double number)
{
   if(number > 0)
       return 1;
   else
       return 0;
}

int main()
{
   double number;
   cout << "Enter a double number: ";
   cin >> number;                           //read double number

   int value = isPositive(number);           //call the function to make a decision
   if(value == 1)                          
       cout << "The number " << number << " is a positive number" << endl;  
   else
       cout << "The number " << number << " is not a positive number" << endl;
   return 0;
}

---------------main3.cpp---------------

#include <iostream>
using namespace std;

/*
A program to print all even integers between 1 and 30 on the screen
*/

int main()
{
   cout << "The even numbers between 1 to 30 are: ";
   for(int i = 2; i <= 30; i = i + 2){                       //start i from 2, loop until i <= 30, increment i by 2
       cout << i << " ";
   }
   cout << endl;
   return 0;
}

---------------main4.cpp---------------

#include <iostream>
using namespace std;

/*
A program to reads a real number as input and adds it to a running total until the user enters the number -1.
At that time the program should print out the final sum (not including, of course, the number -1)
*/

int main()
{
   double number;
   double sum = 0;
   cout << "Enter a real number: ";
   cin >> number;                           //read first number  
   while(number != -1){                   //loop breaks if number is equal to -1
       sum = sum + number;                   //compute sum
       cout << "Enter a real number: ";
       cin >> number;                       //read next number          
   }

   cout << "The sum of the numbers is " << sum << endl;    //print the sum
   return 0;
}

--------------Screenshots-------------------

------------------Output-----------------

----------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou

Know the answer?
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
Similar Questions
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously...
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously prompt the user for numbers. When the user enters a negative number, print the highest positive number entered by the user and exit the program. Print a zero if they did not enter any positive numbers. 2.DivisionWrite an ANNA assembly program (div.ac) that divides two positive numbers that come from user input and returns both the quotient and the remainder. For example, if the...
in C++ Write a program to use the IF ELSE statement in the following way. Have...
in C++ Write a program to use the IF ELSE statement in the following way. Have the user input a value, x If the value is less than 100, present, "you got 5% interest" and display on the screen the total of the X*1.05 Else print on the screen, "you got 10% interest", and display the value X*1.10.
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Vowels and consonants Write a program that reads a word and prints the number of vowels...
Vowels and consonants Write a program that reads a word and prints the number of vowels and consonants in the word. For this exercise assume that ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, and ‘y’ are vowels. For example, if the user enters the input “Harry”, the program should print “The word contains 2 vowels and 3 consonants”.
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Problems . Using the if...else if...else if......else, write a C++ program that - Asks the user...
Problems . Using the if...else if...else if......else, write a C++ program that - Asks the user to input two integers. - If both of them are greater than zero, then output their sum. - If only one integer is greater than zero, then output their difference. - Otherwise, output a message "your two number <0".
Write a program which (1) reads a double number from a user, and (2) stores the...
Write a program which (1) reads a double number from a user, and (2) stores the Math.sqrt of the number if it is > 0 and -1 otherwise into a variable named answer, and (3) return answer.
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
IN C++ PLEASE. Write a program where the user enters a number and you output an...
IN C++ PLEASE. Write a program where the user enters a number and you output an unfilled square of stars. (DO NOT PROMPT THE USER, just take in a number, The only console output should be the squares). For example the program would start with console input, The user would enters 3, you would output (note 3 is the lowest number your program will be tested with) *** * * *** or, The user enters 5, you would output *****...