0% found this document useful (0 votes)
69 views5 pages

C++ Midterm Practice

Uploaded by

aselalybaeva42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views5 pages

C++ Midterm Practice

Uploaded by

aselalybaeva42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

#include <iostream.h>
Using namespace std;
main ( )
{
int number;
int digit;
cout << “Please enter a 4 digit integer : ”;
cin >> number;
digit = number %10;
cout <<“The digit is: “ << digit << ‘\n’; // first digit; and then << ‘\
n’
number = number / 10;
digit = number % 10;
cout <<“The digit is: “ << digit << ‘\n’;
number = number / 10;
digit = number % 10;
cout <<“The digit is: “ << digit << ‘\n’;
number = number / 10;
digit = number % 10;
cout <<“The digit is: “ << digit;

}
2. IF
#include <iostream.h>
Using namespace std;
main ( )
{
int BobAge, AliceAge;
BobAge = 0;
AliceAge = 0;

cout<<“Please enter Bob’s age”;


cin >> BobAge;
cout<<“Please enter Alice’s age”;
cin >> AliceAge;

if (BobAge > AliceAge)


{
cout << “\n”<< “Bob’s age is greater then Alice’s age” ;
}
}

3. WHILE
sum = 0;
number = 1;
cout << “ Please enter the upper limit for which you want the sum ”;
cin >> UpperLimit;
while (number <= UpperLimit)
{
if (number % 2 == 0)
{
sum = sum + number;
number = number + 1;
}
}
cout << “ The sum of all even integer between 1 and ” << UpperLimit << “ is”
<< sum;

4. Factorial
#include <iostream.h>
Using namespace std;
main ( )
{
int number ;
int factorial ;
factorial = 1 ;
cout << “Enter the number of Factorial” ;
cin >> number ;
while ( number >= 1 )
{
factorial = factorial * number ;
number = number – 1 ;
}
cout << “Factorial is” << factorial ;
}
5. Do-While
#include<iostream>

using namespace std;

main()
{
char c ;
int tryNum = 1 ;
do
{
cout << "Please enter your guess by pressing a character key from a
to z " ;
cin >> c ;
if ( c == 'z' )
{
cout << "Congratulations! you guessed the right answer" ;
tryNum = 6 ;
}
else
tryNum = tryNum + 1 ;
} while ( tryNum <= 5 ) ;
}
5. switch
#include <iostream>
using namespace std;
main ( )
{
char grade ;
cout << " Please enter the grade of this
student " ;
cin >> grade ;
switch ( grade)
{
case 'A':
cout<<"Excellent “<<endl;
case 'B':
cout<<"Very Good“<<endl;
case 'C':
cout<<"Good“<endl;
case 'D':
cout<<"Poor“<<endl;
case 'F':
cout<<"Fail“<<endl;
}
}

You might also like