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).
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
Get Answers For Free
Most questions answered within 1 hours.