Sunteți pe pagina 1din 17

Assignment 1

Task 1:
# include <iostream>
using namespace std;
int main ()
{
char a;
int ascii;
cout << "Enter a chracter: ";
cin >> a;
ascii = a;
cout << "The ASCII value of " << a << " is: " << ascii;
return 0;
}
Output:

Task 2:
# include <iostream>
using namespace std;
int main()
{
char oper;
int a;
int b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << "Enter operation to be performed: ";
cin >> oper;
switch(oper)
{
case '+':
cout << a+b << endl;
break;
case '-':
cout << a-b << endl;
break;
case '*':
cout << a*b << endl;
break;
case '/':
cout << a/b << endl;
break;
case '%':
cout << a%b << endl;
break;
default:
cout << "Sorry ! you entered the wrong operator." << endl;
break;
}
return 0;
}
OUTPUT:

TASK 3:
#include <iostream>
using namespace std;
int main()
{
float a;
float b;
float c;
float discriminant;
cout << "Enter value of a: ";
cin >> a;
cout << "Enter value of b: ";
cin >> b;
cout << "Enter value of c: ";
cin >> c;
discriminant = b*b - 4*a*c;

if (discriminant > 0)
{
cout << "Roots are equal." << endl;
}

else if (discriminant == 0)
{
cout << "Roots are unequal." << endl;
}
else
{
cout << "Roots are imaginary.";
}
return 0;
}

OUTPUT:

TASK 4:
#include <iostream>
using namespace std;
int main()
{
char a;
cout << "Enter first letter of your name: ";
cin >> a;
switch (a)
{
case 'A': case 'E' : case 'I' : case 'O' : case 'U':
cout << "Yes ! It is vowel.";
break;
default:
cout << "NO ! It is not vowel.";
}
return 0;
}
OUTPUT

TASK 5:
1. START
2. Declare variables A,B,C, biggest
3. Input A,B,C
4. If,
4.1. A > B and A > C
4.2. Then, biggest = A
5. Else If,
5.1. B>A and B>C
5.2. Then, biggest = B
6. Else,
6.1. Biggest = C
7. Print biggest
8. END

TASK 6:
1. START
2. Declare two variables A, B
3. Input A,B
4. Calculate A=A+B
5. Calculate B=A-B
6. Calculate A=A-B
7. Print A, B
8. END

TASK 7:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int amount = 10000;
int choice;
int age;
int money;
char acctype;
string name;
int count = 1;
while (count == 1)
{
cout << "What do you want to do ?" << endl
<< "To make account Press 1" << endl
<< "To deposit money press 2" << endl
<< "To make withdraw pres 3" << endl
<< "To exit Press 0" << endl
<< "Choice: ";
cin >> choice;
if (choice == 1)
{
cout << "Enter Your name: ";
cin >> name;
cout << "Enter your Age: ";
cin >> age;
cout << "Enter your Account type (Press S for Saving and C for Checking): ";
cin >> acctype;
cout << "Your Account has been created successfully." << endl;
}
else if (choice == 2)
{
cout << "Enter amount you want to deposit: ";
cin >> money;
amount = amount+money;
cout << "Action successfull." << endl;
}
else if (choice == 3)
{
cout << "Enter amount you want to withdraw: ";
cin >> money;
amount = amount - money;
cout << "Action successfull." << endl;
}
else if (choice == 0)
{
cout << "Thank You ! You are exiting";
}
cout << "If you want to go back to main menu press 1 or to exit press 0: "<< endl << "Choice: ";
cin >> count;
}
cout << "Customer's name: " << name << endl;
cout << "Customer's age: " << age << endl;
cout << "Customers Account type: " << acctype << endl;
cout << "Current balance: " << amount << endl;
return 0;
}

OUTPUT:
TASK 8:
#include <iostream>
using namespace std;
int main()
{
char a;
int b;
float c;
double d;
cout << "Enter a chrachter: ";
cin >> a;
cout << "Enter Integer: ";
cin >> b;
cout << "Enter a floating Integer: ";
cin >> c;
cout <<"Enter a integer (double): ";
cin >> d;
cout << "Size of char: " << sizeof(a) << " byte" << endl;
cout << "Size of int: " << sizeof(b) << " bytes" << endl;
cout << "Size of float: " << sizeof(c) << " bytes" << endl;
cout << "Size of double: " << sizeof(d) << " bytes" << endl;
return 0;
}

OUTPUT

TASK 10:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a;
cout << "Enter a floating number: ";
cin >> a;
cout << "The number upto 2 decimal places is " << setprecision(3) << a << endl;
cout << "The number upto 3 decimal places is " << setprecision(4) << a<< endl;
cout << "The number upto 4 decimal places is " << setprecision(5) << a<< endl;
cout << "The number upto 5 decimal places is " << setprecision(6) << a<< endl;
cout << "The number upto 6 decimal places is " << setprecision(7) << a<< endl;
cout << "The number upto 7 decimal places is " << setprecision(8) << a<< endl;
return 0;
}

OUTPUT:

TASK 11:
#include <iostream>
using namespace std;
int main ()
{
int month;
int year;
cout << "Enter year number: ";
cin >> year;
cout << "Enter month number: ";
cin >> month;
if (year%4 == 0 && year%100 == 0 && year%400 == 0) //leap year
switch (month)
{
case 2:
cout << "29 days"<< endl;
break;
case 1 :case 3: case 5:case 7:case 8:case 10:
cout << "31 days" << endl;
break;
case 4:case 6:case 9:case 11:case 12:
cout << "30 days"<< endl;
break;
}
// The distribution is according to Georgian calender
else
{
switch (month)
{
case 2:
cout << "28 days";
break;
case 1 :case 3: case 5:case 7:case 8:case 10:
cout << "31 days" << endl;
break;
case 4:case 6:case 9:case 11:case 12:
cout << "30 days"<< endl;
break;
}
}

return 0;
}

OUTPUT:
Task 12:
#include <iostream>
using namespace std;
int main()
{
int amount;
int drink;
char drinktype;
cout << "Enter Amount";
cin >> amount;
if (amount >= 60)
{
cout << "Which type of drink do you want ?"
<< endl << "Enter 'H' for Hot and 'C' for Cold Drink."
<< endl << "Choice: ";
cin >> drinktype;
if (drinktype == 'H')
{
cout << "What do you want ?" << endl
<< "Enter 1 for Black Tea" << "\t" << "-/60 rupees" << endl
<< "Enter 2 for Cofee" << "\t" << "-/60 rupees" << endl
<< "Enter 3 for Green Tea" << "\t" << "-/60 rupees" << endl
<< "Enter 4 for cappucino" << "\t" << "-/60 rupees" << endl
<< "Choice: ";
cin >> drink;
switch (drink)
{
case 1:
cout << "You Selected Black Tea" << endl;
break;
case 2:
cout << "You selected Cofee" << endl;
break;
case 3:
cout << "You Selected Green Tea" << endl;
break;
case 4:
cout << "You Selected cappucino" << endl;
break;
default:
cout << "You entered wrong digit" << endl;
}
}
else if (drinktype == 'C')
{
cout << "What do you want ?" << endl
<< "Enter 1 for Coke" << "\t" << "-/60 rupees" << endl
<< "Enter 2 for Water" << "\t" << "-/60 rupees" << endl
<< "Enter 3 for Sprite" << "\t" << "-/60 rupees" << endl
<< "Enter 4 for Pepsi" << "\t" << "-/60 rupees" << endl
<< "Enter 4 for Mineral Water" << "\t" << "-/60 rupees" << endl
<< "Choice: ";
cin >> drink;
switch (drink)
{
case 1:
cout << "You Selected Coke" << endl;
case 2:
cout << "You selected Water" << endl;
case 3:
cout << "You Selected Sprite" << endl;
case 4:
cout << "You Selected Pepsi" << endl;
case 5:
cout << "You selected Mineral Water" << endl;
default:
cout << "You entered wrong digit" << endl;
}
}
else
{
cout << "You entered wrong chracter ! Please Enter the correct one" << endl;
}
}

if (amount < 60)


{
cout << "Out of money !" << endl;
}
else
{
amount = amount - 60;
cout << "Your Remaining Amount is: " << amount << endl;
}

system("pause");
return 0;
}
OUTPUT
TASK 13:
#include <iostream>
using namespace std;
int main ()
{
int a;
char b;
cout << "Enter a number: ";
cin >> a;
if (a<=9)
{
cout << a << endl;
}
else
{
a= a+55;
b=a;
cout << b << endl;
}

return 0;
}
OUTPUT:

TASK 15:
#include <iostream>
using namespace std;
int main ()
{
unsigned int a;
cout << "Enter: ";
cin >> a;
a = ~a;
cout <<"The maximum value of this variable is: " <<a;
}

OUTPUT:

TASK 17:
#include <iostream>
using namespace std;
int main ()
{
float unit;
float bill;
cout << "Enter number of Units: ";
cin >> unit;
if (unit <= 50)
{
bill = 0.75*unit;
}
else if (unit <= 150)
{
unit = unit - 50;
bill = (0.95*unit) + (50*0.75) ;
}
else if (unit <= 250)
{
unit = unit - 150;
bill = (1.20*unit) + (0.95*100) + (0.75*50);
}
else
{
unit = unit - 250;
bill = (1.50*unit) + (1.20*100) + (0.95*100) + (0.75*50);
}
bill = bill + (bill*20)/100;
cout << bill << " rupees.";
}

OUTPUT:

TASK 18:
PART A)
#include <iostream>
using namespace std;
int main()
{
int x;
int a;
int b;
int c;
int d;
cout << "Enter number: " << endl;
cin >> x;
d = x % 10;
x = x/10;
c = x % 10;
x = x / 10;
b = x % 10;
x = x / 10;
a = x % 10;
d = d + 7;
d = d % 10;
c = c + 7;
c = c % 10;
b = b + 7;
b= b % 10;
a = a + 7;
a = a % 10;
cout << "Encrypted code: " << c << d << a << b << endl ;
return 0;
}
PART 2)
#include <iostream>
using namespace std;
int main()
{
int x;
int a;
int b;
int c;
int d;
cout << "Enter number: " << endl;
cin >> x;
b = x % 10;
x = x / 10;
a = x % 10;
x = x / 10;
d = x % 10;
x = x / 10;
c = x % 10;
if( a < 7 )
a = a+ 10;
if( b < 7 )
b =b + 10;
if( c < 7 )
c = c + 10;
if( d < 7 )
d = d + 10;
a = a - 7;
b = b - 7;
c = c - 7;
d = d - 7;
cout << "Decrypted code: " << a << b << c << d << endl;
}

OUTPUT:

TASK 19:
Statement 1:
X = 3+4+5
The Precedence of this statement will be left to right as the operators are of equal precedence.
Statement 2:
x=y=z
The Precedence of this statement will be right to left as assigning operator perform right to left.
Statement 3:
Z*= ++y + 5
Solving the equation further,
Z = Z*(++y + 5)
So The Highest precedence here is to solve statement inside parenthesis.
And inside parenthesis the highest precedence is of urinary operators {pre increment in this case}
And then it will add the incremented value into 5
And then multiply with Z
And then assign new value to Z.
Statement 4:
a || b && c || d
The precedence of AND is higher than OR so it’ll executes first and make operation
A || (b&&c) || d
And then the precedence will be equal between all OR operations so, it’ll go left to right.

TASK 20:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int a;
cout << "Enter a number: ";
cin >> a;
cout << setw(10) << a << endl;
/* setw() will occupy the empty places as much given in parenthesis */
cout << setfill('#') << setw(10) <<a <<endl;
/* setfill will fill the spaces with chracter */
cout << setprecision(10) << showpoint<< a << endl;
/* showpoint is used to show the decimal points value even if its zero */

TASK 21:
The output will be:
5
5
6

5
6
6

S-ar putea să vă placă și