You are on page 1of 5

Lab no.

5
Name : Kashan Zulfiqar
Enrollment no. : 01-131192-043
Date : 17 October, 2019

Title: c++ loop statements.


Introduction:
 While loop: is used when the number of iterations are unknown.
 do-while loop: is used when the number of iterations are unknown.
 for loop: is used when the number of iterations are known.

 Programs:

1. Write a program that takes decimal number as input and convert it in binary ,octal or
hexa decimal number.
2. Write a program to make a calculator that perform simple arithmetic calculations on
input variables and asks that if the user wants to do another operation on the variables
or not.

Tools and Technology used:


Visual studio 2013.

MS office.

Windows 8.1.
Task 1: -
Write a program to make a calculator that perform simple arithmetic calculations on input
variables and asks that if the user wants to do another operation on the variables or not.

Program:-
#include<iostream>
using namespace std;
void main()
{
cout << "my name is kashan\n my enrollment number is 01 - 131192 - 043";
int a, b, c;
char t;
char d = 'y';
while (d == 'y')
{
cout << "enter two intergers as operand for applying aperations";
cin >> a >> b;
cout << "enter an operator as + for addition \n-for substraction\n/for division\n*for
multiplication";
cin >> t;
switch (t)
{
case'+':
c = a + b;
break;
case'-':
c = a - b;
break;
case'/':
if (b>0)
c = a / b;
else
cout << "denominator is greator then numinator";
break;
case'*':
c = a*b;
break;
default:
cout << "invalid operator";
}
cout << c;
cout << "if you want to perform anyother operation then press y or pres any other key
to exit";
cin >> d;
}
system("pause");
}

Output:-

Task 2: -
Write a program that takes decimal number as input and convert it in binary ,octal or hexa
decimal number.

Program:-
#include <iostream>
#include <string>
#include<string.h>
using namespace std;
void main()
{
cout << "my name is kashan\n my enrollment number is 01 - 131192 - 043";
char a;
string s;
string g;
string h;
int b, c;
cout << "press H or h for converting decimal number to hexa decimal number";
cout << "press o or O for converting decimal number to octal number";
cout << "press b or B for converting decimal number to binary number";
cin >> a;
cout << "now enter a decimal number to be converted into hexa decimal binary number or octal
number from decimal number";
cin >> b;
switch (a)
{
case 'h':case'H':
while (b >= 16)
{
c = b % 16;
b = b / 16;
s = s + to_string(c);
}
break;
case 'o':case 'O':
while (b >= 8)
{
c = b % 8;
b = b / 8;
}
break;
case 'b':case 'B':
while (b >= 2)
{
c = b % 2;
b = b / 2;
s = s + to_string(c);
}
break;
default:
cout << "invalid input";
}
s = s + to_string(b);
for (int i = 1; i <= s.length(); i++)
{
cout << s[s.length() - i];
}
system("pause");
}
Output:-

Conclusion:-
 Today we performed two tasks and I was successfully able to perform these
tasks
 We learned string and length() statement.

You might also like