0% found this document useful (0 votes)
31 views5 pages

Lab 6

The document contains code for 3 different C++ programs. Program 1 sums even and odd numbers input by the user up to an upperbound. Program 2 performs basic math operations on two input numbers. Program 3 finds the maximum and minimum numbers from a vector of positive numbers input by the user, with -999 indicating the end of input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Lab 6

The document contains code for 3 different C++ programs. Program 1 sums even and odd numbers input by the user up to an upperbound. Program 2 performs basic math operations on two input numbers. Program 3 finds the maximum and minimum numbers from a vector of positive numbers input by the user, with -999 indicating the end of input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TASK 1

int main() {
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int upperbound; // Sum from 1 to this upperbound

// Prompt user for an upperbound


cout << "Enter the upperbound: ";
cin >> upperbound;

int number=0 ;
while (number <= upperbound) {
if (number % 2 == 0) {
cout << "even number";// Even number
sumEven += number; // Add number into sumEven
}
else {
cout << "odd number"; // Odd number
sumOdd += number; // Add number into sumOdd
}
++number; // increment number by 1
}

cout << "The sum of odd numbers is " << sumOdd << endl;
cout << "The sum of even numbers is " << sumEven << endl;

return 0;
}
TASK 2:

#include <iostream>

#include <cmath>

#include <string>

using namespace std;

int main() {

// for the program to keep executing until control + c is pressed, or 'q' is


entered

bool done = false;

int input;

char op;

float num1, num2;

int sum;

cout << "Enter operator either + or - or * or /: ";

cin >> op;

cout << "Enter two operands: ";

cin >> num1 >> num2;

switch (op)

case '+':

cout << num1 + num2;

break;

while (!done) {

std::cout << "Please enter an integer or 'q' to quit" << std::endl;

// user input

cin >> input;

if (input == 'q')

done = true;

break;

cout << "closing the program Enter Q";


}

int main() {
int number;
int sum = 0;
int input;
bool done = false;

// take input from the user


cout << "Enter a number: ";
cin >> number;

while (number >= 0) {


// add all positive numbers
sum += number;

// take input again if the number is positive


cout << "Enter a number: ";
cin >> number;
}

// display the sum


cout << "\nThe sum is " << sum << endl;
return 0;
{
while (!done) {
std::cout << "Please enter an integer or 'q' to quit" << std::endl;
// user input
cin >> input;
if (input == 'q')
done = true;
break;
}
cout << "closing the program Enter Q";
}
TASK 3:

#include<iostream>
#include<vector>
#include<climits>
using namespace std;
int main()
{

int large=INT_MIN,small=INT_MAX;
cout<<"Please Enter positive number\n Input as many as you want\nwhen you finish
enter ""-999"""<<endl;
int num;
vector<int> v;
while(num!=-999)
{
cin>>num;
if(num<0 && num!=-999)
{
cout<<"Warning:That is not a positive Integer\nplease continue to enter positive
number\nwhen you finish enter ""-999"""<<endl;
continue;
}

if(num!=-999) v.push_back(num);
}
int i;
for(i=0;i<v.size();i++)
{
if(v[i]>large)
large=v[i];
if(v[i]<small)
small=v[i];
}
cout<<"Maximum= "<<large<<endl;
cout<<"Minimm= "<<small<<endl;

You might also like