Sei sulla pagina 1di 7

Simple C++ Programs to Implement Various Control Structures.

If statement b. Switch case statement and do while loop c. For loop d. While loop
a. Ex 1A:
if .. else statement
An electricity board charges the following rates to domestic users ti discourage large
consumption of energy: FOR the first 100 units - 60P per unit For next 200 units - 80P per unit
Beyond 300 units - 90P per unit All users are charged a minimum of Rs.50.00. if the total amount
is more than Rs.300.00 than an additional surcharge of 15% is added Write a C++ program to
read the names of users and number of units consumed and print out the charges with names

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
cout<<"\n\n\n\tElectricity Board Charges\n";
cout<<"\n\tTo Discourage Large Consumption of energy\n\n";

char name[20];
cout<<"\n\nEnter USER name :-";
cin>>name;

cout<<"\n\nEnter Number of Units Consumed:-";


float unit;
cin>>unit;

//MANIPULATIONfloat tc;
if(unit<=100)
tc=unit*0.40;
elseif(unit<=300)
tc=unit*0.50;
else
tc=unit*0.60;

float surchase=0;
if(tc>250)
surchase=tc*0.15;

float total_cost;
total_cost = 500 + surchase + tc;

cout<<"\n\nYOUR BILL AMOUNT IS "<<total_cost;

getch();
}

2. Check Whether Number is Even or Odd using if


else
#include <iostream>
using namespace std;

int main()
{
int n;

cout << "Enter an integer: ";


cin >> n;

if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";

return 0;
}

3.Sum of Natural Numbers using for loop


#include <iostream>
using namespace std;

int main()
{
int n, sum = 0;

cout << "Enter a positive integer: ";


cin >> n;

for (int i = 1; i <= n; ++i) {


sum += i;
}

cout << "Sum = " << sum;


return 0;
}

4. While loop
C++ Program to compute factorial of a number
#include <iostream>
using namespace std;

int main()
{
int number, i = 1, factorial = 1;

cout << "Enter a positive integer: ";


cin >> number;

while ( i <= number) {


factorial *= i; //factorial = factorial * i;
++i;
}

cout<<"Factorial of "<< number <<" = "<< factorial;


return 0;
}

Enter a positive integer: 4

Factorial of 4 = 24
5. Do-while
C++ program to add numbers until user enters 0
#include <iostream>
using namespace std;

int main()
{
float number, sum = 0.0;

do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);

cout<<"Total sum = "<<sum;

return 0;
}

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: -4

Enter a number: 2

Enter a number: 4.4


Enter a number: 2

Enter a number: 0

Switch
C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or
Divide Using switch...case
# include <iostream>
using namespace std;

int main()
{
char op;
float num1, num2;

cout << "Enter operator either + or - or * or /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch(op)
{
case '+':
cout << num1+num2;
break;

case '-':
cout << num1-num2;
break;

case '*':
cout << num1*num2;
break;

case '/':
cout << num1/num2;
break;

default:
// If the operator is other than +, -, * or /, error
message is shown
cout << "Error! operator is not correct";
break;
}

return 0;
}

Enter operator either + or - or * or divide : -

Enter two operands:

3.4

8.4

3.4 - 8.4 = -5.0

Potrebbero piacerti anche