EXERCISE 1: Write a program that asks the user to type the price without tax of one kilogram of tomatoes,the number of kilograms you want to buy and the tax in percent units. The program must write the total price including taxes.

#include<iostream>
using namespace std;
int main()
{
double price_without_taxes,weight,taxes,total;

cout<<"Type the price of 1 kg of tomatoes without taxes : ";cin>>price_without_taxes;
cout<<"How much tomatoes do you want (in kilogram) : ";cin>>weight;
cout<<"What is the tax in percent : ";cin>>taxes;

total= ((((price_without_taxes*taxes)/100)+ price_without_taxes)*weight);

cout<<"The total price is : "<<total<<endl;

return 0;
}

EXERCISE 2:

Write a program which asks for a student score. Score is a number from 0-100. Translate the score into grade according to the next limits:
score >= 90 ==> “A”
score >= 80 ==> “B”
score >= 70 ==> “C”
score >= 60 ==> “D”
anything else ==> “F”

if the score is 100 print “Perfect score!”.

#include <iostream>
#include <string>
#include <sstream>

using namespace std; //Sorry, I'm lazy, this is not a good practice

bool isNumber(string &inp)
//Function to check if a string consist only numbers.
{
    int i;
    if (inp[0] == '-') { //Handle "-" sign
        i = 1;
    } else {
        i = 0;
    }
    for( i ; i < inp.length() ; i++) { //Along the string
        for (int z = 0x30 ; z < 0x3A ; z++) { //With every numbers in the ASCII table.
            if (inp[i] == (char)z) break; //If we find a number go to the next char
            if (z == 0x39) return false; //if we reached number "9" and no hit, return false.
        } 
    }
    return true; //All char. were numbers, happy !
}

char getGrade(int &score)
//Convert score into grades.
{
    if ( score >= 90 ) return 'A';
    if ( score >= 80 ) return 'B';
    if ( score >= 70 ) return 'C';
    if ( score >= 60 ) return 'D';
    return 'F';
}

int main(int argc, const char* argv[])
{
    int    score;
    string score_str;

    cout << "Please provide your score:" << endl;
    while ( 1 ) {
        getline(cin, score_str);
        if (isNumber(score_str)) {
            stringstream(score_str) >> score; //Convert the string into number.
            if ( score > -1 && score < 101 ) { //Check limits break if we inside.
                break;
            }
        }
        cout << "!!! Score should be a number in 0-100 !!!" << endl; //Print error message if not.
    }
    cout << "Your grade: " << getGrade(score) << endl; //Put the result on the screen
    if ( score == 100 ) {
        cout << "Perfect score !" << endl; //And do the extra "tap" if the student is great.
    }
}

EXERCISE 3: A login validation in CPP

#include <iostream>
using namespace std;

int main() {
	string username, password;
	cout << "Welcome, please insert username and password" << endl;
	do{
	cout << "User Name: " << endl;
	cin >> username;
	if(username=="Said"){
		cout << "Username is correct" << endl;
	}
}
    while(username!="Said");

	do{
	cout << "Password: " << endl;
	cin >> password;
	if(password=="said.al"){
		cout << "Password is correct" << endl;
	}
}
    while(password!="said.al");
	cout << "You have no rights here!";
	return 0;
}

EXERCISE 4: Power of a number

#include <iostream>
using namespace std;

int main() {
	int num, exp, ris=0;
	cout << "Insert a number: ";
	cin >> num;
	cout << "Insert an exponent: ";
	cin >> exp;
	if(exp==0){
		ris=1;
	}
	else{
		ris=num;
		for(int x=1; x<exp; x=x+1)
		ris=ris*num;
	}
	cout << "Result done from said.al is: ': " << ris << "n";
	return 0;
}
Visited 55 times, 1 visit(s) today

Leave a Comment