Write a program to display all perfect numbers between 1 - 100
This is a discussion on Write a program to display all perfect numbers between 1 - 100 within the Computer Science & IT Engineering forums, part of the CE : Technical Discussions category; Here's another programming question I've asked in interviews to CS/IT Toppers in interviews when they tell me they are interested ...
-
9th October 2008 10:50 AM #1
Write a program to display all perfect numbers between 1 - 100
Here's another programming question I've asked in interviews to CS/IT Toppers in interviews when they tell me they are interested in Maths & Programming :
Write a program to display all perfect numbers between 1 - 100 Computer Language Choice: Whatever, doesn't matter. I'm happy even if you can come up with an algorithm. PS: If you are interested in mathematics and don't know what Perfect Numbers are, you don't impress me.
-
-
9th October 2008 04:07 PM #2 CE - Newbie
Re: Write a program to display all perfect numbers between 1 - 100
2^(n-1)(2^n -1)
this means (2 raise to the power of (n-1)) into (2 raise to the power n)-1
this is the euclid equation for calculating the perfect numbers. The first value of the n is 2 because n-1 should be greater than 1 and n is a prime number i.e. 2,3,5,7 and so on.
use this formula to calculate the perfect numbers between 1 and any number.try yourself. This is the hint.
(P.S.= ASSUMED THAT U KNOW WHAT IS PERFECT NUMBER)
Last edited by c.deepak257; 9th October 2008 at 04:10 PM.
-
9th October 2008 04:18 PM #3
Re: Write a program to display all perfect numbers between 1 - 100
I'd not ask if I didn't know.
-
9th October 2008 04:26 PM #4 CE - Newbie
Re: Write a program to display all perfect numbers between 1 - 100
perfect number are those in which the whose divisors proper sum is equal to the number.for eg: 6 has 1,2,3 as the proper divisors.sum of 1,2, and 3 is 6.
-
14th October 2008 03:31 PM #5
Re: Write a program to display all perfect numbers between 1 - 100
Hi,
This program finds all perfect numbers within 1 to 100
I have hardoded n = 100!
The inner for loop finds all factors for a number
This is simple way any one would do, but if you think of speed, then you should use euclids formula.
Thanks & Regards
Sriram
Code:
#include <iostream>
using namespace std;
main()
{
int n = 100,sum = 0;
cout << "Searching for perfect number less than 100..." << endl;
for(int num = 1; num <= n; num++)
{
sum = 0;
for(int i = 1; i < num; i++)
{
if(!(num%i))
{
sum+=i;
}
}
if(sum == num)
cout << num << " Is perfect number" << endl;
}
}
Test run
>./a.out
Searching for perfect number less than 100...
6 Is perfect number
28 Is perfect number
-
17th November 2008 04:44 PM #6 CE - Newbie
Re: Write a program to display all perfect numbers between 1 - 100
it's funny i was asked this same question a week ago in C and it's been kinda haunting me... the truth of the matter is that i don't understand the maths behind it... i was never math savvy so when it gets to things like these i get confused really easy and my code ... let's not go there.. for now at least
though the question had a different twist, user would enter a number, that creates a table with n amount of rows to be filled by the user with his choice numbers to be tested as perfect numbers, y/n, and the non-perfect numbers need to be multiplied together.
i believe there's another way to do it with modulo (the perfect number calculation itself) but i'm sorta stuck...
here it is
Code:
// loop to fill table with numbers to be checked.
for (i = 0; i < nbrMax; i++)
{
scanf ("%d", &tableNbr[i]);
}
// calculations to find perfect numbers
sum = 0;
for (j = 0; j < nbrMax; j++)
{
for (k = 1; k < tableau[j]; k++)
{
if(tableau[j]%k==0)
{
sum += k;
}
}
}
-
17th November 2008 08:08 PM #7
Re: Write a program to display all perfect numbers between 1 - 100
Hi OrAnGeWorX what I could make out of your question is that user enters N numbers and you want the perfect numbers. You also want the product of non perfect numbers.I have tried something I hope you will go through it and reply me.
The code is :
Code:
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int a[MAXSIZE],n,i,j,sum;
long int prod=1;
clrscr();
cout<<"Enter the number of elements to be checked : ";
cin>>n;
cout<<"Enter the elements : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"The perfect nos are : \n";
for(i=0;i<n;i++)
{
sum=0;
for(j=1;j<a[i];j++)
if(a[i]%j==0)
sum+=j;
if(sum==a[i])
cout<<a[i]<<endl;
else
prod*=a[i];
}
cout<<"The product of non perfect numbers is : "<<prod;
getch();
}
Last edited by The_Big_K; 17th November 2008 at 08:12 PM.
Reason: Adding [code] tags
-
17th November 2008 08:18 PM #8
-
17th November 2008 10:29 PM #9 CE - Newbie
Re: Write a program to display all perfect numbers between 1 - 100

Originally Posted by
Ashutosh_shukla
Hi OrAnGeWorX what I could make out of your question is that user enters N numbers and you want the perfect numbers. You also want the product of non perfect numbers.I have tried something I hope you will go through it and reply me.

The code is :
Code:
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int a[MAXSIZE],n,i,j,sum;
long int prod=1;
clrscr();
cout<<"Enter the number of elements to be checked : ";
cin>>n;
cout<<"Enter the elements : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"The perfect nos are : \n";
for(i=0;i<n;i++)
{
sum=0;
for(j=1;j<a[i];j++)
if(a[i]%j==0)
sum+=j;
if(sum==a[i])
cout<<a[i]<<endl;
else
prod*=a[i];
}
cout<<"The product of non perfect numbers is : "<<prod;
getch();
}
Ashutosh_shukla, thanks for replying.. i should rephrase myself as far as the problem and coding this in C shouldn't be problematic (i guess)
The user enters N for number of integers he'd like to check if or not they are perfect.
stage 1: how many numbers do u want to check?
user enters up to 10 and hits enter
stage 2: program now waits for user to input his n numbers that will be saved in a table.
stage 3: program goes through table[0] to table[n-1] and calculates if integer in each position is perfect or not, display a message accordingly, table[n] = integer is / is not a perfect number and in that same loop calculate the product (multiplication) of the non perfect numbers
finally to display that last number.
Thanks in advance.
Marc
i'll try convertin this to C and see if that works.... having some problems with dev-c++, compiling is fine but when i execute, program is crashing with windows send report window..
Last edited by OrAnGeWorX; 17th November 2008 at 10:31 PM.
-
17th November 2008 10:30 PM #10
Re: Write a program to display all perfect numbers upto 1000
Hi I have got the definition of powerful no from wikipedia as follows:
A powerful number is a positive integer m that for every prime number p dividing m, p^2 also divides m. Equivalently, a powerful number is the product of a square and a cube, that is, a number m of the form m = a^2 * b^3, where a and b are positive integers.
The code that gives powerful nos less than 1000 is :
Code:
#include<iostream.h>
#include<conio.h>
int check_prime(int n)
{
for(int j=2;j<n;j++)
{
if(n%j==0)
{
return 0;
}
}
return 1;
}
void main()
{
int i,j;
cout<<"the powerful nos upto 100 are : \n";
for(i=1;i<=1000;i++)
{
for(j=2;j<=i;j++)
if(check_prime(j))
{
if((i%j==0 && i%(j*j)!=0)||(i%j!=0 && i%(j*j)==0))
break;
}
if(j>i)
cout<<i<<"\t";
}
getch();
}
Last edited by The_Big_K; 17th November 2008 at 11:17 PM.
LinkBacks (?)
-
18th November 2008, 12:24 AM
-
9th October 2008, 12:47 PM
-
9th October 2008, 11:18 AM
Similar Threads
-
By prabhat kumar in forum Computer Science & IT Engineering
Replies: 9
Last Post: 13th February 2010, 12:21 PM
-
By uday.bidkar in forum Computer Science & IT Engineering
Replies: 28
Last Post: 20th July 2009, 09:28 PM
-
By The_Big_K in forum CE - Quiz | Puzzles | Mathematics
Replies: 120
Last Post: 4th May 2009, 09:31 AM
-
By xheavenlyx in forum Computer Science & IT Engineering
Replies: 26
Last Post: 11th January 2009, 02:01 AM
-
By friendster7 in forum Chillax : Chit-Chat
Replies: 7
Last Post: 14th April 2008, 09:20 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules