0% found this document useful (0 votes)
102 views7 pages

C++ Exp 1

This document provides examples of C++ programs that implement various basic control structures: 1. An if/else statement to calculate electricity bill charges based on consumption amount. 2. An if/else statement to check if a number is even or odd. 3. A for loop to calculate the sum of natural numbers up to a given value. 4. A while loop to calculate the factorial of a given number. 5. A do-while loop to continually add user-input numbers until a 0 is entered. 6. A switch-case statement to create a basic calculator that can add, subtract, multiply or divide two numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views7 pages

C++ Exp 1

This document provides examples of C++ programs that implement various basic control structures: 1. An if/else statement to calculate electricity bill charges based on consumption amount. 2. An if/else statement to check if a number is even or odd. 3. A for loop to calculate the sum of natural numbers up to a given value. 4. A while loop to calculate the factorial of a given number. 5. A do-while loop to continually add user-input numbers until a 0 is entered. 6. A switch-case statement to create a basic calculator that can add, subtract, multiply or divide two numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like