Find out the perfect number using c program






Code 1:
1. C program to check perfect number

#include<stdio.h>
int main(){
  int n,i=1,sum=0;

  printf("Enter a number: ");
  scanf("%d",&n);

  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);

  return 0;
}

Sample output:
Enter a number: 6
6 is a perfect number

Code 2:
1. C program to find perfect numbers
2. C perfect number code
3. Perfect number program in c language

#include<stdio.h>
int main(){
  int n,i,sum;
  int min,max;

  printf("Enter the minimum range: ");
  scanf("%d",&min);

  printf("Enter the maximum range: ");
  scanf("%d",&max);

  printf("Perfect numbers in given range is: ");
  for(n=min;n<=max;n++){
    i=1;
    sum = 0;

    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }

    if(sum==n)
      printf("%d ",n);
  }

  return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6

Code 3:
3. C program to print perfect numbers from 1 to 100

#include<stdio.h>
int main(){
  int n,i,sum;
 
  printf("Perfect numbers are: ");
  for(n=1;n<=100;n++){
    i=1;
    sum = 0;

    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }

    if(sum==n)
      printf("%d ",n);
  }

  return 0;
}

Output:
Perfect numbers are: 6 28





Definition of perfect number or What is perfect number? 

Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number.

Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128








10. Write a c program to add two numbers without using addition operator.
11. Write a c program to subtract two numbers without using subtraction operator.
15. Write a c program to solve quadratic equation.
18. Write a c program which passes structure to function.
28. Write a c program which takes password from user.
29. Write a scanf function in c which accept sentence from user.
30. Write a scanf function in c which accept paragraph from user.

74 comments:

Anonymous said...

thank u for ur program.

vernika said...

This site is really good I visit this site eneryday.Good questions.The best part is it provides explanation to every question.Best site.
Thanks alot

Anonymous said...

this site is undoubtedly a super duper excellent site to let anyone quench his thirst for c programming logics and concepts! hats offff!

Anonymous said...

IT IS BETTER TO PUT OUTPUT OF PARTICULAR PROGRAM....

Anonymous said...

this is a great site had much information for beginners to experienced .....

Unknown said...

super,best site for all new learners

Raj said...

lets work out this with example now suppose
for n=1
1<1 does not satisfy
for n=2
1<2 satisfies
2%1==0 satisfies
sum=0+1=1
i++(so i is 2 now)
2<2 does not satisfy
for n=3
while 2<3 satisfies
3%2==0 does not satisfy
for n=4
while 2<4 satisfies
2%4==0 satisfies
sum=1+2=3
i=3(i++ i.e i=2+1=3)
for n=5
while 3<5 satisfies
5%3==0 does not satisfy
for n=6
while 3<6 satisfies
6%3==0 satisfies
sum=3+3=6
So here Sum==n satisfies so 6 is a perfect number...
Rocked it everyone can easily understand now

Anonymous said...

It would be better to initialize Sum with 1 and i with 2.

DonKiller said...

brilliant

sandeep said...

thanks

Priyanka said...

luv dis site...\m/\m/\m/

Anonymous said...

nice blog for studentsthx agian

clover said...

give the answer for question --------------10. Write a c program to add two numbers without using addition operator.???

LBEF SMU DE's IT student Zone said...

Thank you very much. This tutorial have helped me complete my university Assignments and Lab reports in time.

Anonymous said...

Thank u.....

Anonymous said...

Best site for a computer engineering student like me. Thanks a lot.....
Parveen Arora

(( * sofi *)) said...

really i am very much thankful to u...its very much helpful to me..thanks a lot.

Anonymous said...

sir should we have to take the sum of values of i for the perfect no...

bakul said...

This site is really good.Good questions.The best part is it provides explanation to every question. site had much information for beginners

Parth said...

Plz tell me how to use this site???????

DrunkenChimp said...

nice concept used

Satish kumar said...

/* reverse of the value in using the program*/
#include
#include
void main()
{
int a[15][15],n,i,j;
printf("Entr the number of the n=>");
scanf("%d",&n);
printf("Enter the number you want to insert=>");
for(i=0;i0;i--)
{
for(j=n;j>0;j--)
{
printf("%d",a[i][j]);
}
getch();
}
}
____________________________________________________________________________________________________________________
output
_______
inter the value of n=2
value= 42,43,44,46;
output is=46,44,43,42 is reverse order;

Unknown said...

these are the question in c asked by the interviewer's so friends utilize this site.
this is a great job

Anonymous said...

Nice one raj

Unknown said...

nice

Anonymous said...

In code 2,how about is "time complexity" ?
Thanks.

Unknown said...

In code2 when i declared i,sum values at the starting it is not showing the results do u know the reason or there is any rule for for loop to give values in that loop

Unknown said...

No time complexity

Unknown said...

i wish allah will make yr dreams true

Anand said...

Code 1.

You can reduce the number of iterations by initializing 'sum with 1 and i with 2'.
And why to check 'n%i == 0' upto 'i < n'. Check only upto 'i <= n/2'.

Anonymous said...

Excuse me, 6 is perfect number because 1+2+3 = 1*2*3, BUT with number 28 when i calculate : 1+2+4+7+14 = 28 and 1*2*4*7*14 = 784 <=== the sum of left doesn't equal with the multiply of right like 6=1+2+3=1*2*3 ????
I need to find all numbers include >=2 digits which have the sum = the multiplication, i wonder is it perfect number ? and how to find it ?
thanks !!

Anonymous said...

This site is realy very good

Anonymous said...

i love your website thank you for your help

Anonymous said...

thanks a lot. :)

Anonymous said...

READ POL_TYPE,term,amount;
IF(pol_type is endownment)
Printf(“2. Money back”);
Printf(“3. Term assurance”)
If (term>=25){
Bonus=(amount/1000)*60;
WRITE BONUS
ENDIF
If (term>=20 && term<25)
Bonus=(amount/1000)*55;
WRITE BONUS
ENDIF
If (term>=10 && term<20)
Bonus=(amount/1000)*50;
WRITE BONUS
ENDIF
If (term<10){
Bonus=(amount/1000)*45;
WRITE BONUS
ENDIF
ENDIF
IF(pol_type is Money back)
If (term>=25)
Bonus=(amount/1000)*55;
WRITE BONUS
ENDIF
If (term>=20 && term<25)
Bonus=(amount/1000)*50;
WRITE BONUS
ENDIF
If (term>=10 && term<25)
Bonus=(amount/1000)*45;
WRITE BONUS
ENDIF
If (term<10){
Bonus=(amount/1000)*45;
IF(pol_type is Term assurance)
WRITE ““bonus not applicable”)
END





Anonymous said...

#include
main()
{
int q[10]={0},i,front=-1,rear=-1,max=10,n,item;
printf("\n\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(rear=0)
{
printf("The deleted item =%d",q[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
}
else
printf("Underflow\n");
break;

case 3:
if((front==-1)&&(rear==-1))
printf("The queue is empty\n");
else
{
printf("The elements of the queue are :");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
break;

case 4:
break;

default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}

Anonymous said...

#include
#include
int main(void)
{
int salesperson[10][2];
int index,totalsales=0;
float minsales,incentiveamount,averagesales=0;
for(index=0;index<10;index++)
{
printf("enter the salesperson id and his sales: \n");
scanf("%d%d",&salesperson[index][0],&salesperson[index][1]);
totalsales+=salesperson[index][1];
}
averagesales=(float)totalsales/(float)10;
minsales=(averagesales*2)/(float)3;
printf("\n Salesperson ID \t Incentive Amount \n");
printf("\n_____________________________________\n");
for(index=0;index<10;index++)
{
if(salesperson[index][1]>minsales)
{
incentiveamount=(float)(salesperson[index][1]-minsales)*(float)0.2;
printf("%d \t \t\t %f \n", salesperson[index][0],incentiveamount);
}
}
getch();
return 0;
}

Anonymous said...

pseudocode for Queue
Begin
Accept the N elements

INSERT Operation(insertion in stack)(to back)

Repeat for N-1
Insert the first element to Queue[0]
Insert the second element to Queue[1]
"
"
"
Insert the last element to Queue[N-1] "





REMOVE Operation(deletion from stack)(from front)

Repeat for N-1
REMOVE the first element from Queue[N-1]
REMOVE the second element from Queue[N-2]
"
"
"
REMOVE the last element from Queue[0] "

End

Anonymous said...

pseudocode for Queue(lab3_3.4)
Begin
Accept the N elements

INSERT Operation(insertion in stack)(to back)

Repeat for N-1
Insert the first element to Queue[0]
Insert the second element to Queue[1]
"
"
"
Insert the last element to Queue[N-1] "





REMOVE Operation(deletion from Queue)(from front)

Repeat for N-1
REMOVE the first element from Queue[N-1]
REMOVE the second element from Queue[N-2]
"
"
"
REMOVE the last element from Queue[0] "




DISPLAY Operation(Display Queue Elements

Repeat for N-1
WRITE the first element from Queue[0]
WRITE the second element from Queue[1]
"
"
"
WRITE the last element from Queue[N-1] "





No. of Element in queue

Repeat for NULL(iteration until the element stored in a queue reaches to null value)
Count++
END of Loop
WRITE Count
End


Anonymous said...

stack

pseudocode for Stack

Begin
Accept the N elements

PUSH Operation(insertion in stack)

Repeat for N-1
PUSH the first element to Stack[0]
PUSH the second element to Stack[1]
"
"
"
PUSH the last element to Stack[N-1] "





POP Operation(deletion from stack)

Repeat for N-1 to 0
POP the last element from Stack[N-1]
POP the second_last element from Stack[N-2]
"
"
"
POP the first element from Stack[0] "

End










Anonymous said...

linked list
pseudocode for LINKED LIST

Deleting a Node from the Linked List (assuming values in Data are unique)

Function Delete(Value)

Pointer : Found 'Pointer for finding required Node to Delete

Found = NULL 'Assuming required Node not found

IF there are no Nodes in the List then
WRITE error message that can't delete a Node

ELSE
Go to memory location pointed by Start pointer 'Begin at the Start (first Node)
Start Loop and run until Next points to NULL 'Check each Node until Next pointer points to NULL

IF value in Data of current Node is equal to Value then 'If we find our required Node
Found = current Node 'Mark the current Node with Found pointer

Exit Loop 'No need to finish the loop

ENDIF

Go to Node pointed by Next pointer of current Node

End Loop

IF the required Node is the first Node in the List then
Point the Start pointer to NULL 'Node deleted

ELSE

Point Next of previous Node to the Node pointed by Next of Found 'Node deleted (Previous.Next = Found.Next)
ENDIF
ENDIF
End Function

Anonymous said...

linear search
pseudocode for sequential search


Begin
Accept the N elements
READ Element X to search

count=0

Label1 Repeat for index=0 to N-1
IF(Element[index]=X)
WRITE Element X found
ELSE
count++
Goto Label1:
ENDIF
END LOOP

IF(count>1)
WRITE Element X not found
ENDIF

End








Anonymous said...

binary search
Begin
ACCEPT N Elements
READ Element X to search

IF (range contains only one element):
Look for desired value
IF found
WRITE Element X found

Else:

1. Get midpoint of range(N)
2. Determine which half of range contains
the value
3. Search that half of the range using binary
search
Look for desired value
IF found
WRITE Element X found
ENDIF
ENDIF
End

Anonymous said...

INSERTION SORT

READ numberofelements, array [];
FOR i=0 to numberofelements-1
J=i
WHILE j>0 and array[j]<array[j-1]
SWAP array[j] and array[j-1]
j—
WRITE array []

Anonymous said...

#include
int main()
{

int a[10],i,n,m,c=0,l,u,mid;

printf("Enter the size of an array: ");
scanf("%d",&n);

printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

printf("Enter the number to be search: ");
scanf("%d",&m);

l=0,u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])

{
c=1;
break; }
else if(m<a[mid])
{
u=mid-1; }
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");

else
printf("The number is found.");
getch ();

return 0;
}

Anonymous said...

/*proram to calculate the pay of a worker*/
#include
#include
float pays(int hours,float rate);
int main()
{
int hours,itrvar,num;
float rate;
float pay=0,totalpay=0;

printf("enter the number of workers");
scanf("%d",&num);

for(itrvar=0;itrvar0)/*condition to check hourly rate should be greater than 0*/
{
pay=pays(hours,rate);/*call of pays function*/
printf("Pay at %f rupees/hr for %d is %f\n",rate,hours,pay);
totalpay=totalpay+pay;
}/*end of if*/
else
{
printf("Total pay is=%f",totalpay);
exit(0);
}/*end of else*/
}/*end of loop*/

printf("Total pay is=%f",totalpay);
}/*end of main()*/
float pays(int hours,float rate)
{
float pay;
if(hours<40)/*check for number of hours worked for calculating pay*/
pay=hours*rate;
else if(hours>=40&&hours<60)
pay=hours*1.5*rate;
else if(hours>=60)
pay=hours*2.0*rate;
return pay;
}/*end of pays()*/

Anonymous said...

/*proram to calculate the pay of a worker*/
#include
#include
float pays(int hours,float rate);
int main()
{
int hours,itrvar,num;
float rate;
float pay=0,totalpay=0;

printf("enter the number of workers");
scanf("%d",&num);

for(itrvar=0;itrvar0)/*condition to check hourly rate should be greater than 0*/
{
pay=pays(hours,rate);/*call of pays function*/
printf("Pay at %f rupees/hr for %d is %f\n",rate,hours,pay);
totalpay=totalpay+pay;
}/*end of if*/
else
{
printf("Total pay is=%f",totalpay);
exit(0);
}/*end of else*/
}/*end of loop*/

printf("Total pay is=%f",totalpay);
}/*end of main()*/
float pays(int hours,float rate)
{
float pay;
if(hours<40)/*check for number of hours worked for calculating pay*/
pay=hours*rate;
else if(hours>=40&&hours<60)
pay=hours*1.5*rate;
else if(hours>=60)
pay=hours*2.0*rate;
return pay;
}/*end of pays()*/

Anonymous said...

file pointer
#include
#include
#include

int main()
{
FILE *fp1, *fp2;
char file1[10], file2[10], data[100];

printf("\n Enter name of source file: ");
scanf("%s",&file1);

printf("\n Enter name of destination file: ");
scanf("%s",&file2);

if((fp1 = fopen(file1,"r"))==NULL)
{
printf("\n %s not found\n\n", file1);
getch();
exit(0);
}

fscanf(fp1, "%s", data);

if((fp2 = fopen(file2, "w"))==NULL)
{
printf("\n %s not found\n\n", file2);
getch();
exit(0);
}

fprintf(fp2, "%s", data);

getch();
return 0;
}

Anonymous said...

#include
#include
int main()
{
FILE *p,*q;

char b[10];
char a;
printf("enter the file name.txt");
scanf("%s",&b);
p=fopen(b,"r");
if (p==NULL)
{
printf("\n cannot locate the file");
exit ();
}
q=fopen("muddesir.txt","w");
if(q==NULL)
{
printf("\n cannot locate the file");
fclose(q);
exit ();
}
while(1)
{
a=fgetc(p);
if(a==EOF)
break;
else

fputc(a,q);

}
fclose(p);
fclose(q);
getch ();


}

Anonymous said...

#include
#include

int Factorial(int Num)
{
int loop;

for(loop=Num; loop>0; loop--)
Num*=loop;

return Num;
}

int main()
{
int n, r, Choice=0;
float Result=0.0f;

printf("\n Enter n and r values: ");
scanf("%d%d",&n, &r);

printf("\n Enter:\n\n 1 for nCr\n\n 2 for nPr\n\n Your choice: ");
scanf("%d", &Choice);

if(Choice == 1)
{
printf("\n nCr = ");
Result = (Factorial(n)/(Factorial(r) * (Factorial(n-r))));
}

else if(Choice == 2)
{
printf("\n nPr = ");
Result = Factorial(n)/Factorial(n-r);
}

else
{
printf("\n Illegal choice!");
exit(0);
}

printf("%.2f",Result);

getch();
return 0;
}

Anonymous said...

Module for AcceptValues:
1. FOR loop1=1 -> n
1.1 FOR loop2=1 -> 7
1.1.1 READ Data[loop1][loop2]
2. State[25] = {all names of states}
3. Sex[2] = {‘M’,’F’}
Module for Census:
1. FOR loop1=1 -> n
1.1 IF Data[4][loop1] = State[index1]
1.2 Print State[index1]
1.3 FOR loop2= 1 -> n
1.3.1 Index2 = 0
1.3.2 IF Data[1][loop2] = Sex[index2]
1.3.3 Print Sex[loop2]
1.3.4 FOR loop3= 1 -> n
1.3.4.1 SWITCH Data[7][loop3]
1.3.4.1.1 CASE 1 : Hindu++
1.3.4.1.2 CASE 2: Muslim++
1.3.4.1.3 CASE 3: Christian++
1.3.4.1.4 CASE 9: Others++
1.3.5 Print Hindu, Muslim, Christian, Others
1.3.6 index2++
1.4 index1++

Main() function:
1. Begin
2. GOTO AcceptValues
3. GOTO Census
4. End program

Anonymous said...

/****************

Student database

*****************/

#include
#include

/* Sorting data using bubble sort */

int* Sort(int* Marks, int Num)
{
int temp, loop1, loop2;
for(loop1=0; loop1 Marks[loop2+1])
{
temp=Marks[loop2];
Marks[loop2]=Marks[loop2+1];
Marks[loop2+1]=temp;
}
return Marks;
}

/* Searching the details based on name */

int SearchByName(char* Name, char* Value, int n)
{
int loop;

for(loop=0; loop4)
printf("\n Illegal choice!!");

else
{

switch(Choice)
{
case 1: printf("\n Enter name to search: "); /* Searching by name */
scanf("%s",Value);
temp1= SearchByName(Name, Value, Num);
if(temp1!=-1)
printf("\n Name: %s\n ID: %d\n Marks: %d\n\n", Name[temp1], ID[temp1], Marks[temp1]);
else
printf("\n Name not found");
break;

case 2: printf("\n Enter ID to search: "); /* Searching by ID */
scanf("%d", &temp2);
temp1= Search(ID, temp2, Num);
if(temp1!=-1)
printf("\n Name: %s\n ID: %d\n Marks: %d\n\n", Name[temp1], ID[temp1], Marks[temp1]);
else
printf("\n ID not found");
break;

case 3: printf("\n Students record with rank\n\n"); /* Printind data rankwise */
for(loop=0; loop<Num; loop++)
printf("\n Rank: %d\n Name: %s\n ID: %d\n Marks: %d\n\n", loop+1, Name[loop], ID[loop], Marks[loop]);
break;

case 4: printf("\n Enter ID to search: "); /* Searching name by ID */
scanf("%d", &temp2);
temp1= Search(ID, temp2, Num);
if(temp1!=-1)
printf("\n Name: %s\n\n",Name[temp1]);
else
printf("\n ID not found");
break;
}
}

getch();
return 0;
}

Anonymous said...

I tried code 2 using for loop instead of while loop, but it is not working. What could be the possible reason. I replaced i=1;
sum = 0;

while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
by
for(i=1;i<n;++i)
{
if(n%i==0)
sum+=i;
}
The compiler shows no errors but blank space after the line "The perfect numbers are:". Please reply.

Anonymous said...

I forgot to mention I also intialised sum to 0 in the beginning.

Unknown said...

#include
#include
void mail ()
{
int n,i,sum;
clrscr();
printf("perfect number\n");
for(n=1;n<=100;n++)
{
i=1;
sum=0;
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%\n",n);
}
getch();
}

Unknown said...
This comment has been removed by the author.
Unknown said...

Write a C program to read through an array of any type using pointers. Write a C program to scan through this array to find a particular value.

Anonymous said...

these are the question in c asked by the interviewer's so friends utilize this site.
this is a great job

computerFlashes said...

#include
int is_perfect(int);
int main()
{
int x;
printf("Enter any number\n");
scanf("%d",&x);
if(is_perfect(x))
printf("perfect\n");
else
printf("not perfect");



}
int is_perfect(int x)
{

int i=1;
int sum=0;
while(i<x)
{

if(x%i==0)
sum=sum+i;
}
if(sum==0)
return 1;
else
return 0;


}

tiger said...

Create a program to calculate the salary and bonus based on sales of a staff.

(i) In main() :

(ii) In function get_bonus(...) :

- Ask the user to enter satff id, salary and units sold.

- Call function get_bonus(...) and pass units sold and salary to calculate

the bonus amount.

- Call function get_nett_salary(...) and pass salary and bonus amount to

calculate the nett salary.

- Call function display(...) and pass staff id, salary, units sold, bonus

amount and nett salary to be displayed on screen.

- get the units sold and salary from main() to calculate the bonus amount

by refering to the following table.

- return the bonus amount to main().

UNITS SOLD BONUS

> 1000 20% of salary

501 - 1000 10% of salary

(ii) In function get_nett_salary(...) :

(iv) In function display(...)

- get the bonus amount and salary from main() to calculate the nett salary.

- return the nett salary to main()

- get staff id, salary, units sold, bonus amount and nett salary from main.

- display all information on screen.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DATA ENTRY

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter staff id : 1234

Enter staff salary : RM 3500.00

Enter total units sold : 750

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SALARY SLIP

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Staff ID : 1234

Staff salary : RM 3500.00

Units sold : 750

Bonus : RM 350.00

Nett Salary : RM 3850.00

Press any key to continue

tiger said...

can someone help me with this question?

suresh said...

how to get all program at once plz

kavya said...

I cant understand the logic of perfect number please expain it

Unknown said...

please provide me pdf of all c program

KKP said...

in n=4 why didnt i increased to 4 after i++

KKP said...

in n=4 why didnt i increased to 4 after i++

Free Mente said...

When we type 0 ...it says 0 is a perfect number ...but actually 0 is not a perfect number :/ how to fix it ??

Iamateur said...

Hello Code 1 to find perfect number does not work. It gives different output. It seems like a garbage value.I tried it on different compilers too.

Unknown said...

//Addition of two numbers without using Addition operator//

#include
#include
void main()
{
int a,b;
printf("Enter 2 no");
scanf("%d%d",&a,&b);
while(b)
{
a++;
b--;
}
printf("sum %d",a);

}


www.pcprogramblog.wordpress.com

Unknown said...

//Addition of two numbers without using Addition operator//

#include
#include
void main()
{
int a,b;
printf("Enter 2 no");
scanf("%d%d",&a,&b);
while(b)
{
a++;
b--;
}
printf("sum %d",a);

}


www.pcprogramblog.wordpress.com

Unknown said...

Write a program to find out first n perfect number where n is the input from user

Solar Systems in India said...

What is perfect specifier for adding of two numbers???

Rupesh said...

Share market with certificate

Rupesh said...

Share market with certificate