Name M Arslan Afzal Lab 6 ID F18602037: Iostream

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

NAME M ARSLAN AFZAL

LAB 6
ID F18602037

TASK 1
#include<iostream>
using namespace std;

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";

TASK 3:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num, max=0, min=0;
while(num != -999)
{ cout<<"Enter a number:\n";
cin>> num;
if (num>max)
{ max = num;}
if (num<min)
{ min = num;}
}
cout<<"Largest number is: "<<max << endl;
cout<<"Smallest number is: "<<min << endl;
cout <<"END!";
}

TAK 4

#include<iostream>
using namespace std;
int power(int x, int y) {
int i,power=1;
if(y == 0)
return 1;
for(i=1;i<=y;i++)
power=power*x;
return power;
}
int main() {
int x = 4;
int y = 5;
cout<<"x = "<<x<<endl;;
cout<<"y = "<<y<<endl;
cout<<"x^y = "<<power(x,y);
return 0;
}

TASK 5

#include <iostream>
#include <ctime>
#include<string>
using namespace std;
const int MAX_TRIES = 5;
int letterFill(char, string, string&);
int main()
{
system("color 70");
string name;
char letter;
int num_of_wrong_guesses = 0;
string word;
string words[] = { "Pakistan",
"Germany","USA","canada","uk","Malta","Iran","Qatar","Oman","Italy"

};
srand(time(NULL));
int n = rand() % 10;
word = word[n];
string unknown(word.length(), '*');
cout << "\n\n welcome to Hang-Man---Guess country Name\n";
cout << "\n\n Each letter represented by star\n";
cout << "\n\n you have to type only one letter in one try\n";
cout << "\n\n you have" << MAX_TRIES << "tries to try and guess word";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n\n Guess a letter";
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "That is not have" << endl;
num_of_wrong_guesses++;
}
else {
cout << endl << "You have found a letter isn't that exciting!" <<
endl;
cout << "you have" << MAX_TRIES - num_of_wrong_guesses;
cout << "guesses left" << endl;
if (word == unknown)
{
cout << word << endl;
cout << "\n yeah you got it";
break;
}
if (num_of_wrong_guesses = MAX_TRIES)
{
cout << "\n Sorry.you loose loose ---you have been hanged" <<
endl;
cout << "That word was:" << word << endl;
}
cin.ignore();
cin.get();
system("pause");

int(letterFill(char.guess,string secretword,string&guess word) {


int i;
int matches = 0;
int len = secret word.Length();
for (i = 0; i <= len; i++)
{

if (guess == guess word[i])


return 0;
if (guess == secret word[i])
{
guess word[i] = guess
matches++;
}
}

You might also like