Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a.

Similar presentations


Presentation on theme: "1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a."— Presentation transcript:

1 1 Programming 2 Overview of Programming 1

2 Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a

3 The value that printed is int delta = 0, x = 3; if (x / 2 = = 1) delta = delta + x; x - -; if (x / 2 = = 0) delta = delta + x; x - -; Cout<<delta;

4 What is value of x when y is 15.0? x = 25.0; if (y != (x-10.0) ) x=x-10.0; else x=x / 2.0;

5  What is the value is assigned to x when y is 15.0? if (y < 15.0) if (y>= 0.0) x=5*y; else x = 2*y; else x= 3 * y;

6 Programming Examples Write a program to ask the user for the width and length of a piece of land and then tell him how many orange trees he can grow on it. Given that each orange tree requires 4 m 2.

7 7 Example The Air Force has asked you to write a program to label aircrafts as military or civilian. Your program input is the plane’s speed and its estimated length. For planes traveling faster than 1100 km/hr, you will label those shorter than 52 m “military”, and longer as “Civilian”. For planes traveling less than 1100, you will issue an “aircraft unknown” statement.

8 Find the output if (x > 10) z= z*m; if ( y< 0) p = p *5; cout<<z<< p; if (x > 10) z= z*m; else if ( y< 0) p = p *5; cout<<z<< p; For x=15, y=-5, z=2, p =3 and m=10, find the output of the following code

9  A factory has some workers and it divides its workers into three skill levels. Unskilled workers receive L.E. 8.15 per hour, semiskilled workers receive L.E. 12.55 an hour, and skilled workers L.E. 18.60 an hour. Write a program to calculate the worker’s weekly pay formatted to two decimal places. Your program input should consist of the hours worked and a skill level indicator for the worker. Then the wage should be displayed.

10 int p, d=4, a=20; if ((d==4) && (a>24)) { p=d+a; Cout<< “ Low Risk ” ;} else { p=a-d; Cout<< “ Low Risk ” ;} Cout<< “ the p is =“<<p; Trace

11 Write a program to get the third side of a triangle (a), given the lengths of the other two sides (b, and c), and the angle  using the formula a 2 = b 2 + c 2 – 2bc cos  b rad_angle = alpha * PI / 180; a = sqrt(pow(b,2) + pow(c,2) – 2 * b * c * cos(rad_angle); c  a complex math example-2

12 If--Else for a mail order Write a program to calculate the total price of a certain purchase. There is a discount and shipping cost:  The discount rate is 25% and the shipping is 10.00 if purchase is over 100.00.  Otherwise, The discount rate is 15% and the shipping is 5.00 pounds.

13 What is the value of variable z after the following code is executed? int w = 5, x = 7, y = 9, z = 1; if (x % y > = 2 + w) { z+ +; if (y – 3 * w > = - x) z - -; else z + +; } else { z = - 1; }

14  int z;  z = 10 + 8 / 3 * 2 + 10 % 3;  switch (z) {  case 23 : cout<<“ Very High”; break;  case 21 : cout<<“ High “; break;  case 18 : cout<<“ Med “; break;  case 15 : cout<<“ Low “; break;  case 10 : cout<<“ Very Low “; break;  default : cout<<“ None “; break;  }

15 #include main( ) { int x=2, y=2, k=1; do { y = y * x; Cout<<“k = “ <<k<<“ y =\n”<< y; y = y – k; Cout<<“k = “ <<k<<“ y =\n”<< y; k = k + 1; } while (k < 4); }

16 Example  Write a function to ask the user for a positive integer, and then display its factorial. Given that factorial(n) is 1 X 2 X 3 …… x n

17 int I = 12, J = 0; while ( I > 8) { I = I -1; J = J + I; } Cout<< I <<J;

18 Trace length = 10; count = 2; while ( count < 5 ) { if ( length >= 10 ) length = length – 2; else length = length * count; count++; } cout<< “ The length is ”<< length;

19 Trace m = 1; prod = 1; do { prod = prod * m ; m = m + 1 ; } while ( m < 6 ) ; cout<< “ prod is “ << prod ;

20 n = 0 ; sum1 = 0 ; sum2 = 0 ; while ( n < 10 ) { if ( n % 3 = = 0 ) sum1 = sum1 + n ; if ( n % 2 = = 0 ) sum2 = sum2 + n ; n++; } Cout<< “ sum1 = ”<< sum1 ; Cout<< “ sum2 = ” << sum2 ;

21 Write a program to calculate and display a table of n vs n 3, as shown below: 1 2 8 3 27 4 64 5 125 6 216 7 343 8 512 9 729 10 1000

22 Write a C program that display the number of odd numbers between its two integers: Integer 1integer 2results 1-21 151 241 0105

23 To produce the output: 8173571, the condition of while loop should be: int N = 8; do{ cout<<N; N = N * 2 + 1; } while ( ? ? ? ?); N = 85) N > 143 6) none of the above

24  Write a program that reads 100 integer numbers, determines how many positive and negative values have been read, and computes the average of the input values.

25  A shop need to keep an inventory of its 100 items for sale. Write a console application to input the item code, price and quantity of each item. Then the program displays a neat table of all items (code, price and quantity) with two asterisks ** next to items whose price > 100 pounds, and one asterisk * next to items whose price is between 50 and 100 pounds.

26 for(k=0; k<5; k++) { cout<<“**”; if(k %3 <=2) { k++; } else { cout<<k; }

27  Write a C program that reads in 30 integer numbers and then print out their sum and average

28 for (int N = 1; N < 10; N++) { if ( N % 2 == 0) cout<<“X”; else if ( N % 3 == 0) cout<<“Y”; else cout<<“Z”; } cout<<“End”;

29 Find the output int x= 5, y= 2; char op = ‘*’; switch (op) { case ‘+’ : x = x+y; case ‘-‘ : x=x- y; break; case ‘*’ : x= x * y ; case ‘/’ : x = x / y; break; default : x =x + 1; break; } cout<<x << y;

30 j = 10; for (i=1; i<=5; ++i) { cout<<i<< j; j = j-2; }

31 31 for ( m=1 ; m<=9 ; m=m+2 ) { x = pow ( m, 2 ); cout<<m<< x; } Find the output

32 Write a program that reports the speed of the wind. The program input is 100 speed of wind: Wind speed Content Below 25Not strong 25 - 38Strong wind 39 - 54Gale 55 - 72Whole gale above 72Hurricane

33 Find output n = 5; j = 1; while ( j < 4 ) { if ( n >= 10 ) n = n – 2; else n = n * j; j++; } cout<< “ The n is <<n;

34 Locate 10 syntax errors in the code and write a correction for each error #include float main int x=2, n; switch ( 1 <= x < 10 ) case 2: x ++: case 3: x - -: for ( n=1, n<10, n++ ) { scanf( %d “, n, a ) ; b = = 3a + 1 ) % ( a + ( 18 - a % 3 ) ; cout<<(n x) }

35  Write a program that reads a number in range 0-100 and print ‘F’ if the number is less than 50, ‘P’ if the number is in the range 50-59, and ‘S’ if the number is greater than 65.

36 Write a program that reads the value of x and calculates the values of y and z using the following formulas:,

37 Find the output for (i=1; i<5; i++) { for (j=0; j<I; j++) cout<<i+j; }

38 Write a C program using functions to ask the user to enter two real numbers and read them. Then, your program will display the following menu: Select an operation: 1. Addition 2. Multiplication 3. Power The user will select the operation by entering a number from 1 to 3. After that your program will display the entered numbers with the performed operation using functions and the result up. If the selected operation is invalid, display “Invalid operation”.

39 Example  Write a C++ program to calculate the following formula for any given n. The program should read n from the user.

40 Trace the following code void main() { int a = 2; int b = 2; int c = 3; b = mult( a, c ) ; cout<<a<<b <<c ; c = squares( a, b ) ; cout<<a<<b <<c ;

41 int mult (int x, int y ) { int z = x * y ; return z; } int squares ( int p, int q ) { int r = p * p + q * q ; return r ; }

42  A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for example, 1980). However, since the introduction of the Gregorian calendar on October 15, 1582, a year is not a leap year if it is divisible by 100 (for example, 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). Write a C Program that accepts an int argument called “year” and print out if the year is a leap year or not.


Download ppt "1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a."

Similar presentations


Ads by Google