Name M Arslan Afzal Lab 6 ID F18602037: Iostream
Name M Arslan Afzal Lab 6 ID F18602037: Iostream
Name M Arslan Afzal Lab 6 ID F18602037: Iostream
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
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>
int main() {
int input;
char op;
int sum;
switch (op)
{
case '+':
break;
while (!done) {
// user input
if (input == 'q')
done = true;
break;
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");