0% found this document useful (0 votes)
66 views12 pages

Program To Find The Maximum and Minimum of N Numbers Using Class

The document contains 5 C++ programs that demonstrate different ways to find maximum/minimum values, prime numbers, and perfect dividing numbers within a given range using classes and functions. The programs take a user-input integer limit, perform the calculations, and output the results. Classes and objects are used to encapsulate the code and make it more modular.

Uploaded by

nisha02t
Copyright
© Attribution Non-Commercial (BY-NC)
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)
66 views12 pages

Program To Find The Maximum and Minimum of N Numbers Using Class

The document contains 5 C++ programs that demonstrate different ways to find maximum/minimum values, prime numbers, and perfect dividing numbers within a given range using classes and functions. The programs take a user-input integer limit, perform the calculations, and output the results. Classes and objects are used to encapsulate the code and make it more modular.

Uploaded by

nisha02t
Copyright
© Attribution Non-Commercial (BY-NC)
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

1.

Program to find the maximum and minimum of N numbers using


class.

#include<iostream.h>
#include<conio.h>
class mm
{
int i,l,a[10],max,min;
public:
void read();
void display()
{
cout<<"\nMaximum number is "<<max<<endl;
cout<<"\nMinimum number is "<<min;
}
void func();
};
void mm::func()
{
max=a[0];
min=a[0];
for(int i=1;i<l;i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
display();
}
void mm::read()
{
cout<<"Enter the limit:";
cin>>l;
cout<<"\nEnter the numbers:";
for(i=0;i<l;i++)
{
a[i]=0;
cin>>a[i];
}
}

int main()
{
mm k;
clrscr();
k.read();
k.func();
getch();
return(0);
}
OUTPUT:

Enter the limit: 4

Enter the numbers: 12 10 47 879

Maximum number is 879

Minimum number is 10
2. Program to print prime numbers upto an integer N.

#include<iostream.h>
#include<conio.h>
void primen(int num);
int main()
{
int num;
int prime,i;
clrscr();
cout<<"Enter the limit:";
cin>>num;
cout<<"\nPrime numbers upto the limit "<<num<< " are "<<endl;
primen(num);
getch();
return (0);
}
void primen(int num)
{
int i,prime;
for(i=3;i<=num;i++)
{
prime=1;
for(int n=2;n<i;n++)
{
if(i%n==0)
{
prime=0;
break;
}
}
if(prime==1)
{
cout<<i<<endl;
}
}
}
OUTPUT:

Enter the limit: 12

Prime numbers upto the limit 12 are


3
5
7
11
3. Program to print prime numbers upto an integer N using class.

#include<iostream.h>
#include<conio.h>
class primen
{
int num,prime,i;
public:
void read();
void primec();
};
void primen::read()
{
cout<<"Enter the limit:";
cin>>num;
}
void primen::primec()
{
int i,prime;
for(i=3;i<=num;i++)
{
prime=1;
for(int n=2;n<i;n++)
{
if(i%n==0)
{
prime=0;
break;
}
}
if(prime==1)
{
cout<<i<<endl;
}
}
}
int main()
{
clrscr();
primen ob;
ob.read();
cout<<"Prime numbers are\n";
ob.primec();
getch();
return (0);
}
OUTPUT:

Enter the limit: 50

Prime numbers are


3
5
7
11
13
17
19
23
29
31
37
41
43
47
4. Program to print perfect dividing numbers upto an integer N.

#include<iostream.h>
#include<conio.h>
void perfect(int num);
int main()
{
int num;
int prime,i;
clrscr();
cout<<"Enter the limit:";
cin>>num;
cout<<endl<<"\nPerfect dividing numbers are\n";
perfect(num);
getch();
return (0);
}
void perfect(int num)
{
int sum,i;
for(int n=1;n<num;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
cout<<n<<endl;

}
}
}
OUTPUT:

Enter the limit: 50

Perfect dividing numbers are


6
28
5. Program to print perfect dividing numbers upto an integer N using
class.

#include<iostream.h>
#include<conio.h>
class perf
{
int num,i,sum;
public:
void read();
void perfect();
};
void perf::read()
{
cout<<"Enter the limit:";
cin>>num;
}
void perf::perfect()
{
int sum,i;
for(int n=1;n<num;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
cout<<n<<endl;
}
}
}
int main()
{
clrscr();
perf ob;
ob.read();
cout<<endl<<"\nPerfect dividing numbers are\n";
ob.perfect();
getch();
return (0);
}

OUTPUT:

Enter the limit: 1000

Perfect dividing numbers are


6
28
496

You might also like