Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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;
//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;
getch();
}
int main()
{
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
int main()
{
int n, sum = 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;
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);
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: 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;
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;
}
3.4
8.4