Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Advanced Programming

Week 1: Lab Session 1 Summer 2019

Q1. Write a C++ program to read and then Add two numbers type integer and display the
result.
#include <iostream>
using namespace std;

int main()

{ int r=0;
do{

int firstNumber, secondNumber, sumOfTwoNumbers;

cout << "Enter two integers: ";


cin >> firstNumber >> secondNumber;

sumOfTwoNumbers = firstNumber + secondNumber;

cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
cout<< "\n\n Do you want to try again? \n\n Enter 0 to continue";
cin>> r;
}
while(r==0);

Q2. Modify your previous C++ program to enter and Add two numbers type float and display
the result .
#include <iostream>
using namespace std;

int main()

{ int r=0;
do{

Lab Session 1 Abdellatif Tchantchane & Toth Tamas 1


float firstNumber, secondNumber, sumOfTwoNumbers;

cout << "Enter two numbers: ";


cin >> firstNumber >> secondNumber;

sumOfTwoNumbers = firstNumber + secondNumber;

cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
cout<< "\n\n Do you want to try again? \n\n Enter 0 to continue";
cin>> r;
}
while(r==0);

Q3. Write a C++ to evaluate the following expression

Q4. Operators
a) State some binary arithmetic operators
 addition, subtraction, multiplication, exponentiation (**), division, and modulus
(%).
b) State some unary arithmetic operators
unary minus(-)
increment(++)
decrement(- -)
NOT(!)
Addressof operator(&)
sizeof()

c) State some relational operators cout << (5>3)


Greater than or equal to
Less than = to
==

Lab Session 1 Abdellatif Tchantchane & Toth Tamas 2


!=

d) State some boolean logical operator cout << (5>3) && (5<3)
Greater than or equal to
Less than = to
&&
&^
e) Difference between logical and relational operators
Lol I’ll do this soon

f) State some bitwise operators. e.g. cout << (7|1) <<endl;

Q5. Bitwise operators


int a = 7, b = 11;
explain the values
a&b =
a|b =
~a =
a^b =
b<<1 =
b>>1 =
a&1 =
(bit shift 100<<1)

Q6. Write a C++ code that enters an integer and output whether the integer is odd or not
(state which operators have you used)

Using bitwise operator for odd or even

#include <iostream>
using namespace std;

int main()

{ int r=0;
do{

int x, odd, even;

Lab Session 1 Abdellatif Tchantchane & Toth Tamas 3


cout << "Enter your number: ";
cin >> x;
if(x & 1) // '&' is a bit-wise AND operator also
add a exit
printf("%d is ODD\n", x);
else
printf("%d is EVEN\n", x);

}
while(r==0);

Q7. Write a C++ code to check whether the year entered from console is a leap year or not
(Leap year: 365+1=366 days).
To determine whether a year is a leap year (it has 366 days):
1. If the year is evenly divisible by 4.
2. If the year is evenly divisible by 100
3. If the year is evenly divisible by 100, then it should also be divisible by 400

Q8. Write a C++ code to read an int (between 1 to 12 ) and use Swicth case Statement to print
the name of the month (1 for January, ...)

Q9. Write a C++ code to calculate the Factorial of an integer. Read the number k from
console
0! = 1
k! = 1*2*3*…k
use a for statement and then write a recursive function.

Q10. Write a recursive function to calculate the Factorial of an integer. Read the number k
from console
0! = 1
k! = 1*2*3*…k

Q11. Explain the errors


#include <iostream>
using namespace std;
add(int [ ] , int );

int main()
Lab Session 1 Abdellatif Tchantchane & Toth Tamas 4
{ const int size=4;
int array[size]={1,2,3,4};
add (array, size);
cout << array[1]<<endl;
return 0;
}

add (const int array[], int size)


{ array[1] = array[1] +10; }

Q12. Write C++ functions for calculating the value each of the functions at some value of
x using the first 10 terms of the Taylor expansion. Display the error between the value
using their corresponding math functions and your calculated values.

Q13. Write a C++ code to Solving a quadratic equation: ax2 + bx + c= 0 (a≠0)

Lab Session 1 Abdellatif Tchantchane & Toth Tamas 5


Q14. Read N numbers (floats or integers) in an array by reading them from a text file. Find
a) their average.
b) Minimum
c) And Maximum

Q15. Read N numbers (floats or integers) in an array by reading them from a text file.
- Print them to the console and write them in reverse order.
- Write whether the array is in ascending order or in descending order or neither.

e.g 5 7 9 12 ascending; 12 9 7 5 descending 9 12 5 7 neither

Lab Session 1 Abdellatif Tchantchane & Toth Tamas 6

You might also like