0% found this document useful (0 votes)
35 views

Request The User To Type Numbers, Each Time Printing Its Triple, Until The User Enters - 1

The document contains 5 code snippets that demonstrate different programming concepts: 1. A program that requests numbers from the user and prints the triple of each number until -1 is entered. 2. A program that counts the even and odd numbers in a data series and terminates if -1 is entered. 3. The output of a program using a continue statement in nested for loops. 4. A program that calculates the product of odd integers from 1 to 100. 5. A program that calculates the average of integers entered by the user until -1 is entered.

Uploaded by

tarek mahmoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Request The User To Type Numbers, Each Time Printing Its Triple, Until The User Enters - 1

The document contains 5 code snippets that demonstrate different programming concepts: 1. A program that requests numbers from the user and prints the triple of each number until -1 is entered. 2. A program that counts the even and odd numbers in a data series and terminates if -1 is entered. 3. The output of a program using a continue statement in nested for loops. 4. A program that calculates the product of odd integers from 1 to 100. 5. A program that calculates the average of integers entered by the user until -1 is entered.

Uploaded by

tarek mahmoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Request the user to type numbers, each time printing its triple, until
the user enters -1.

#include <iostream.h>

int main() {
for (int input; input != -1;) {
cout << "Enter a number (-1 to quit): ";
cin >> input;
if (input != -1)
cout << "Tripled: " << input * 3 <<
endl;
}

return 0;
}
2. Write a Program to find out no. of Even & Odd no. in a given Data
Series and terminate if entered value is -1.
#include <iostream.h>

int main()

int even_n = 0; int odd_n = 0;

int num;

cout << "Enter a positive number: ";

cin >> num;

while(num != -1){

if (num % 2 == 0) even_n++;

else odd_n++;

cout << " input enter a positive number: ";

cin >> num;

cout << "\nEven No.: " << even_n << "\n";

cout << "Odd No.: " << odd_n << "\n";

return 0;

}
3. What is the output of the following

#include <iostream.h>

int main()

for (int i = 0; i < 10; i++) {

if (i != 5) continue;

cout << i << " ";

cout << '\n';

for (int j = 0; j < 2; j++) {

for (int k = 0; k < 5; k++) {

if (k == 3) continue;

cout << j << k << " ";

5
00 01 02 04 10 11 12 14
4. Write a program that calculates and prints the product of the odd
integers from 1 to 100.

#include <iostream.h>

int main()

long product = 1;

for ( long i = 3; i <= 15; i += 2 )

{ product *= i; }
cout << "Product of the odd integers from 1 to
100 is: "

<< product << “\n”;

return 0;

}
5. Write a program that calculates and prints the average of several
integers. Assume the last value read is the sentinel -1

#include <iostream.h>

int main()

int value, count = 0, total = 0;

cout << "Enter an integer (-1 to end): ";

cin >> value;

while ( value != -1 ) {

total += value;

++count;

cout << "Enter next integer (-1 to end): ";

cin >> value;

if ( count != 0 )

cout << "\nThe average is: "

<< ( total ) / count << "\n";

else

cout << "\nNo values were entered." << "\n";

return 0;

You might also like