Vous êtes sur la page 1sur 21

Practice Problems

Selective

Structure

if if-else Switch

Repetitive

Structure

while do-while for

1-2

while

loop vs do-while

In while loop when the while condition is true than the loop works. In do-while the loop will always run at least once and then check the condition and continue the loop if condition is true.

1-3

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

// code10.cpp // Using the do/while repetition structure. #include <iostream> using std::cout; using std::endl; // function main begins program execution int main() { int counter = 1; // initialize counter do { loop-continuation cout << counter << " "; // display counter } while ( ++counter <= 10 ); // end do/while

Notice the pre-increment in test.

cout << endl;


return 0; // indicate successful termination } // end function main

10

While

loop

int i=0; while (i<=10) i++;

for loop reduces line of code 3 line of code using while loop is equal to one line of code using for loop

For

loop

for( int i=0; i<=10;i++)


1-5

palindrome is a number or a text phrase that reads the same backward as forward.
For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a C++ program that reads in a five-digit integer and determines whether it is a palindrome. [Hint: Use the division and modulus operators to separate the number into its individual digits.]

1-6

#include<iostream> using namespace std; void main() { long number=0; cout<<"\n\nENTER THE NUMBER : "; cin>>number; long temp1=number%10000; long digit1=number/10000; long temp2=temp1%1000; long digit2=temp1/1000; long temp3=temp2%100; long digit3=temp2/100; int digit5=temp3%10; int digit4=temp3/10; long result=((digit5*10000)+(digit4*1000)+(digit3*100)+(digit2* 10)+digit1); if(result==number) cout<<"Yes it is a palindrome"<<endl; else cout<<"No it is not a palindrome"<<endl; }

1-7

worker is paid at the hourly rate of $8 per hour for the first 35 hours worked. Hereafter overtime is paid at 1.5 times the hourly rate for the next 25 hours worked and 2 times the hourly rate for further hours worked. Write a C++ program to input the number of hours worked per week, calculate and output the overtime paid.

1-8

int total_hours=0, normal_hours=0,bonus_hours=0,ebonus_hours=0; cout<<"Enter the hours :"; cin>>total_hours; if(total_hours>60) { normal_hours=35; bonus_hours=25; ebonus_hours=total_hours-60; } else if(total_hours>35) { normal_hours=35; bonus_hours=total_hours-35; ebonus_hours=0; } else { normal_hours=total_hours; bonus_hours=0; ebonus_hours=0; } cout<<"Normal pay : $"<<normal_hours*8<<endl; cout<<"Overtime pay : $"<<(bonus_hours*8*1.5)+(ebonus_hours*8*2)<<endl; cout<<"Total pay : $"<<(normal_hours*8)+(bonus_hours*8*1.5)+(ebonus_hours*8*2)<<endl; }
1-9

Write

a program that take input from a user and determines that if value enter by a user if less than 50 or greater than 100, Prints number entered is invalid, and when number enter is between 50-100 it is a valid number.

1-10

int value=0; cin>>value; if(value<50) cout<<value invalid; if(value >100)


cout<<value invalid; cout<<you entered<<value<<end1; return 0;

1-11

1-12

1-13

Using

switch case write C++ program for a calculator


Ask for an operation Ask for two numbers Perform the calculation specified on the numbers

1-14

1-15

1-16

1-17

for ( int i = 0 ; i < 4 ; i++ ) { for ( int j = i ; j < 4 ; j++ ) cout << "* "; for ( int k = i - 1 ; k >= 0 ; k-- ) cout << "- "; cout << endl; }
Output **** *****-*---

1-18

1-19

Write a C++ program to print out all Armstrong numbers between 1 and 500 A number is called Armstrong number if sum of cubes of each digit of the number is equal to the number itself. For example 153=(1*1*1)+(5*5*5)+(3*3*3)

1-20

void main() { int i=1,a,b,c; cout<<"ARMSTRONG NUMBERS BETWEEN 1 to 500 ARE \n; while (i<=500) { a=i%10; /*Extract Last Digit */ b=i%100; b=(b-a)/10; /*Extract First Digit */ c=i/100;/*Extract first Digit*/ if ((a*a*a)+(b*b*b)+(c*c*c)==i) cout<<i; i++; } }

1-21

Vous aimerez peut-être aussi