0% found this document useful (0 votes)
50 views

Programs

The document provides 15 code samples that demonstrate various C++ programming concepts and techniques. The code samples include programs to calculate the area and perimeter of a rectangle, find the best score from three given scores, determine if a character is uppercase, lowercase, a digit or other, display a word for a given character, calculate the sum of digits in a number, calculate the sum of the first N natural numbers, find the average height of a group of students, display all prime numbers below 100, check if a number is a palindrome, display all palindrome numbers between 100 and 200, check if a number is an Armstrong number, display all Armstrong numbers below 1000, display all perfect numbers below 1000, display the multiplication table of a number,

Uploaded by

Anamika T Anil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Programs

The document provides 15 code samples that demonstrate various C++ programming concepts and techniques. The code samples include programs to calculate the area and perimeter of a rectangle, find the best score from three given scores, determine if a character is uppercase, lowercase, a digit or other, display a word for a given character, calculate the sum of digits in a number, calculate the sum of the first N natural numbers, find the average height of a group of students, display all prime numbers below 100, check if a number is a palindrome, display all palindrome numbers between 100 and 200, check if a number is an Armstrong number, display all Armstrong numbers below 1000, display all perfect numbers below 1000, display the multiplication table of a number,

Uploaded by

Anamika T Anil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Sample Programs from Chapter 1

1. Write a C++ program to find the area and perimeter of a rectangle.


Code
#include <iostream>
using namespace std;
int main()
{
float length, breadth, peri, area;
cout << "Enter the length and breadth of rectangle: ";
cin >> length >> breadth;
peri = 2*(length + breadth);
area = length * breadth;
cout << "Perimeter = " << peri << endl;
cout << "Area = " << area << endl;
return 0;
}

2. Write a C++ program to find the best CE score from the three given scores (PRACTICAL).
Code
#include <iostream>
using namespace std;
int main()
{
short int ce1, ce2, ce3, final_ce;
cout<<"Enter three CE scores: ";
cin>>ce1>>ce2>>ce3;
if (ce1>ce2)
final_ce=ce1;//Will be executed, if the condition is true
else
final_ce=ce2;//Will be executed, if the condition is false
if (ce3>final_ce)
final_ce=ce3; //No else block for this if
cout<<"Final CE Score is "<<final_ce;
return 0;
}

3. Write a C++ program to check whether the character is uppercase letter, lowercase
letter, digit or other characters.
Code
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character: ";
cin>>ch;
if (ch>='A' && ch<='Z')
cout<<"Uppercase letter";
else if (ch>='a' && ch<='z')
cout<<"Lowercase letter";
else if (ch>='0' && ch<='9')
cout<<"Digit";
else
cout<<"Other character";
return 0;
}

4. Write a C++ program to display a word for a given character.


Code
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a, b, c or d: ";
cin>>ch;
switch(ch)
{
case 'a': cout<<"Abacus"; break;
case 'b': cout<<"Binary"; break;
case 'c': cout<<"Computer"; break;
case 'd': cout<<"Debugging"; break;
default : cout<<"Invalid input!!";
}
}

5. Write a C++ program to find the sum of digits of a number (PRACTICAL).


Code
#include <iostream>
using namespace std;
int main()
{
int num, sum=0, dig;
cout<<"Enter a number: ";
cin>>num;
while (num>0)
{
dig=num%10;
sum=sum+dig;
num=num/10;
}
cout<<"Sum of the digits of the input number = "<<sum;
return 0;
}
6. Write a C++ program to find the sum of the first N natural numbers (PRACTICAL).
Code
#include <iostream>
using namespace std;
int main()
{
int n, sum=0;
cout<<"Enter the limit: ";
cin>>n;
for(int i=1; i<=n; i++)
sum=sum+i;
cout<<"Sum of the first "<<n<<" natural numbers = "<<sum;
return 0;
}

7. Write a C++ program to find the average height of a group of students.


Code
#include <iostream>
using namespace std;
int main()
{
float hgt, sum=0, avg_hgt;
short n=0;
char ch;
do
{
cout<<"Enter the height: ";
cin>>hgt;
n++;
sum=sum+hgt;
cout<<"Any more students (Y/N)? ";
cin>>ch;
}while (ch=='Y' || ch=='y');
avg_hgt=sum/n;
cout<<"Average Height = "<<avg_hgt;
return 0;
}

8. Write a C++ program to display all prime numbers below 100.


Code
#include<iostream>
using namespace std;
int main()
{ short int n, i, flag;
cout<<"Prime numbers below 100 are...\n";
for(n=2; n<=100; n++) //Outer loop
{
flag=1;
for(i=2; i<=n/2; i++) //Inner loop
if(n%i==0)
{
flag=0;
break;//Takes the control outside the iiner loop
}
if(flag==1) cout<<n<<'\t';
}
return 0;
}

9. Write a C++ program to check whether a given number is palindrome or not


(PRACTICAL).
Code
# include<iostream>
using namespace std;
int main()
{
int num,d,temp,rev=0;
cout<<"Enter a Number : ";
cin>>num;
temp=num;
while(temp>0)
{
d=temp%10;
rev=(rev*10)+d;
temp=temp/10;
}
if(num==rev)
cout<<"Number is a Palindrome";
else
cout<<"Number is not a Palindrome";
return 0;
}

10. Write a C++ program to display all palindrome numbers between 100 and 200.
Code
#include<iostream>
using namespace std;
int main( )
{
int n, rev, t, d;
cout<<"Palindrome Numbers between 100 and 200\n ";
for(n=100; n<=200; n++)
{
rev=0;
t=n;
while(t>0)
{
d=t%10;
rev=(rev*10)+d;
t=t/10;
}
if(rev==n)
cout<<n<<'\t';
}
return 0;
}

11. Write a C++ program to check whether a given number is Armstrong number or not.
Code
#include<iostream>
using namespace std;
int main()
{
int n,t,d,s;
cout<<"Enter a Number : ";
cin>>n;
t=n;
s=0;
while(n!=0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(t==s)
cout<<t<<" is an Armstrong Number";
else
cout<<t<<" is not an Armstrong Number";
return 0;
}

12. Write a C++ program to display all Armstrong numbers below 1000.
Code
# include<iostream>
using namespace std;
int main()
{
int i,s,n,d;
cout<<"Armstrong Numbers below 1000\n";
for(i=1;i<1000;i++)
{
s=0;
n=i;
while(n!=0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(i==s) cout<<i<<"\t";
}
return 0;
}

13. Write a C++ program to display all perfect numbers below 1000.
Code
#include <iostream>
using namespace std;
int main()
{
int n,i,sum;
cout<<"Perfect Numbers below 1000\n";
for(n=1;n<1000;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
cout << n << "\n";
}
return 0;
}

14.Write a C++ program to display the multiplication table of a number.


Code
# include<iostream>
using namespace std;
int main()
{
int n,i;
cout<<"Enter a Number : ";
cin>>n;
cout<<"Multiplication Table of "<<n<<"\n";
for(i=1;i<=10;i++)
cout<<i<<" x "<<n<<" = "<<i*n<<"\n";
return 0;
}

15. Write a C++ program to prepare electricity bill for a group of consumers.
The previous meter reading and current reading are the inputs. The payable
amount is to be calculated using the following criteria:
Up to 300 units :
Rs. 5.00/- per unit
Up to 350 units :
Rs. 5.70/- per unit
Up to 400 units :
Rs. 6.10/- per unit
Up to 500 units :
Rs. 6.70/- per unit
Above 500 units :
Rs. 7.50/- per unit
The program should provide facility to input the details of any number of
consumers as the user wants.
Code
#include <iostream>
using namespace std;
int main()
{
float previous, current, units, charge, t ;
char ch;
start:
cout << "Enter the previous meter reading : ";
cin>>previous;
cout << "Enter the current meter reading : ";
cin >>current;
units=current-previous;
if(units<=300)
charge=units*5.00;
else if(units<=350)
{
t=units-300;
charge=300*5.00+t*5.70;
}
else if(units<=400)
{
t=units-350;
charge=300*5.00+50*5.70+t*6.10;
}
else if(units<=500)
{
t=units-400;
charge=300*5.00+50*5.70+50*6.10+t*6.70;
}
else
{
t=units-500;
charge=300*5.00+50*5.70+50*6.10+100*6.70+t*7.50;
}
cout<<"Units consumed :"<<units;
cout<<"\nCharge : "<<charge;
cout<<"\nAny more consumer(Y/N)?";
cin>>ch;
if(ch=='Y'||ch=='y')
goto start;
return 0;
}

You might also like