In this article Loop programs using class we give the some loop programs using class such as Fibonacci series, reverse number, Armstrong number, star pattern etc.
Loop programs using class
/* To show Fibonacci series
0 1 1 2 3 5 8 13 21
*/
#include<iostream.h>
#include<conio.h>
class FiboSeries
{
private:
int n; // data member
public:
void getdata() // member function
{
cout<<“\n\n Enter the number of terms: “;
cin>>n; //5
}
void show();
};
void FiboSeries::show()
{
int f=0,s=1,temp=0;
cout<<“\n\n Febonacci Series: \n”;
cout<<“\t”<<f<<“\t”<<s; // 0 1 1 2 3 5 8
while(n>0) // 0>0
{
temp=f+s; // 8
cout<<“\t”<<temp;
f=s; // 5
s=temp; // 8
n–; // 0
}
}
void main()
{
FiboSeries obj;
clrscr();
obj.getdata();
obj.show();
getch();
}
Output:
Enter the number of terms: 7
0 1 1 2 3 5 8 13 21
// To show reverse number
#include<iostream.h>
#include<conio.h>
class RevNo
{
private:
int n; // data member
public:
void getnum() // member function
{
cout<<“\n\n Enter the number: “;
cin>>n;
}
void show();
};
void RevNo::show()
{
int dig=0,rev=0;
while(n>0) // 35 3 0
{
dig=n%10; // 5 3
rev=dig+(rev*10); // 5 53
n=n/10; // 3 0
}
cout<<“\n\n Reverse Number: “<<rev;
}
void main()
{
RevNo p;
clrscr();
p.getnum();
p.show();
getch();
}
Output:
Enter the number: 567
Reverse Number: 765
// To show Palindrome number
#include<iostream.h>
#include<conio.h>
class Palindrome
{
private:
int n; // data member
public:
void getnum() // member function
{
cout<<“\n\n Enter the number: “;
cin>>n;
}
void show();
}p;
void Palindrome::show()
{
int dig=0,rev=0,temp=n;
while(n>0) // 35 3 0
{
dig=n%10; // 5 3
rev=dig+(rev*10); // 5 53
n=n/10; // 3 0
}
cout<<“\n\n Reverse Number: “<<rev;
if(temp==rev)
cout<<“\n\n”<<rev<<” is a Palindrome Number.”;
else
cout<<“\n\n”<<temp<<” is a not Palindrome Number.”;
}
void main()
{
clrscr();
p.getnum();
p.show();
getch();
}
Output:
Enter the number: 151
151 is a Palindrome Number.
// To show Armstrong number
#include<iostream.h>
#include<conio.h>
class Armstrong
{
private:
int n; // data member
public:
void getnum() // member function
{
cout<<“\n\n Enter the number: “;
cin>>n;
}
void show();
}p;
void Armstrong::show()
{
int dig=0,sum=0,temp=n;
while(n>0) // 35 3 0
{
dig=n%10; // 5 3
sum=sum+(dig*dig*dig); // 5 53
n=n/10; // 3 0
}
cout<<“\n\n Cube and sum of digit: “<<sum;
if(temp==sum)
cout<<“\n\n”<<sum<<” is a Armstrong Number.”;
else
cout<<“\n\n”<<temp<<” is a not Armstrong Number.”;
}
void main()
{
clrscr();
p.getnum();
p.show();
getch();
}
Output:
Enter the number: 153
Cube and sum of digit: 153
153 is a Armstrong Number.
// To find factorial of given number
#include<iostream.h>
#include<conio.h>
class Factorial
{
private:
int n; // data member
public:
void getnum() // member function
{
cout<<“\n\n Enter the number: “;
cin>>n;
}
void show();
}p;
void Factorial::show()
{
long int fact=1, temp=n;
while(n>0)
{
fact=fact*n;
n–;
}
cout<<“\n\n”<<temp<<“! = “<<fact;
}
void main()
{
clrscr();
p.getnum();
p.show();
getch();
}
Output:
5! = 120
// To find prime numbers between 1 to 100
#include<iostream.h>
#include<conio.h>
class PrimeNo
{
public:
void show() // member function
{
int i,d,cnt=0,sum=0; // local variabls
cout<<“\n\n Prime Numbers between 1 to 100: \n\n”;
for(i=1;i<=100;i++) // 5
{
d=2;
while(i>d)
{
if(i%d==0)
{
break;
}
else
{
d++;
}
}
if(i==d)
{
cout<<“\t”<<i;
cnt++;
sum=sum+i;
}
}
cout<<“\n\n Total Prime numbers between 1 to 100: “<<cnt;
cout<<“\n\n Sum of 1st 25th prime numbers: “<<sum;
}
}p;
void main()
{
clrscr();
p.show();
getch();
}
Output:
Prime numbers:
2 3 5 7 ….
Total Prime numbers between 1 to 100: 25
Sum of 1st 25th prime numbers: 1060
// Program to get the ASCII value of the capital, small letters and digits
#include<iostream.h>
#include<conio.h>
class ASCIICode
{
public:
void show() // member function
{
char i; // local veriable
cout<<“\n\n Capital Letters: \n”;
for(i=65;i<=90;i++)
{
cout<<“\t”<<i;
}
cout<<“\n\n Zero to Nine digits: \n”;
for(i=48;i<=57;i++)
{
cout<<“\t”<<i;
}
cout<<“\n\n Small Letters: \n”;
for(i=97;i<=122;i++)
{
cout<<“\t”<<i;
}
}
}obj;
void main()
{
clrscr();
obj.show();
getch();
}
Output:
Capital Letters:
A B C D ……
Zero to Nine digits:
0 1 2 3 4 5 6 7 8 9
Small Letters:
a, b, c, d, …..
/* Star Pattern
*
* *
* * *
* * * *
* * * * *
*/
#include<iostream.h>
#include<conio.h>
class StarP1
{
public:
void show() // member function
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<“* “;
}
cout<<“\n”;
}
}
}p;
void main()
{
clrscr();
p.show();
getch();
}
/* Star Pattern
*
* *
* * *
* * * *
* * * * *
*/
#include<iostream.h>
#include<conio.h>
class StarP1
{
public:
void show() // member function
{
int i,j,s;
for(i=1;i<=5;i++)
{
for(s=i;s<5;s++)
{
cout<<” “;
}
for(j=1;j<=i;j++)
{
cout<<“*”;
}
cout<<“\n”;
}
}
}p;
void main()
{
clrscr();
p.show();
getch();
}
/* Star Pattern
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
*/
#include<iostream.h>
#include<conio.h>
class StarP1
{
public:
void show() // member function
{
int i,j,s;
for(i=1;i<=5;i++)
{
for(s=i;s<5;s++)
{
cout<<” “;
}
for(j=1;j<=i;j++)
{
cout<<“*”;
}
for(j=2;j<=i;j++)
{
cout<<“*”;
}
cout<<“\n”;
}
}
}p;
void main()
{
clrscr();
p.show();
getch();
}
/* Star Pattern
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
*/
#include<iostream.h>
#include<conio.h>
class StarP1
{
public:
void show() // member function
{
int i,j,s;
for(i=1;i<=5;i++)
{
for(s=i;s<5;s++)
{
cout<<” “;
}
for(j=1;j<=i;j++)
{
cout<<“*”;
}
for(j=2;j<=i;j++)
{
cout<<“*”;
}
cout<<“\n”;
}
for(i=4;i>=1;i–)
{
for(s=i;s<5;s++)
{
cout<<” “;
}
for(j=1;j<=i;j++)
{
cout<<“*”;
}
for(j=2;j<=i;j++)
{
cout<<“*”;
}
cout<<“\n”;
}
}
}p;
void main()
{
clrscr();
p.show();
getch();
}
/* Star Pattern
*
* *
* * *
* * * *
* * * * *
*/
#include<iostream.h>
#include<conio.h>
class StarP1
{
public:
void show() // member function
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<“* “;
}
cout<<“\n”;
}
}
}p;
void main()
{
clrscr();
p.show();
getch();
}
/* Star Pattern
a
a b
a b c
a b c d
a b c d e
*/
#include<iostream.h>
#include<conio.h>
class StarP1
{
public:
void show() // member function
{
char i,j;
for(i=97;i<=101;i++)
{
for(j=97;j<=i;j++)
{
cout<<j<<” “;
}
cout<<“\n”;
}
}
}p;
void main()
{
clrscr();
p.show();
getch();
}
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining