You're online now.

Hurray! you are online now.

Write a program to check given number perfect or not

A number is called perfect number, positive integer number that is equal to the sum of the its possible factor. 
For example: 
6 is a perfect number or not. 
The possible factors of 6 is 1, 2, 3. So these are the possible factors of 6 So now add(sum) these factors 1 + 2 + 3 = 6 
6 is a perfect number.

// check perfect number in c
    #include<stdio.h>
    int main(){
    	int n;
    	printf("Enter the Number : ");
    	scanf("%d", &n);
    	int sum = 0;
    	for(int i=1; i<n; i++){
    		if(n%i==0){
    			sum = sum + i;
    		}
    	}
    	if(sum == n){
    		printf("%d is the Perfect Number",n);
    	}else{
    		printf("%d is Not Perfect Number",n);
    	}
    	return 0;
    }        
  1. Initialized a variable n, which stores the value of given by user.
  2. Initialized a another variable sum. Which stores the sum of factors of given number. Here we initial assigned the value is zero (0).
  3. Start the for loop, in for loop we initialized a variable i which is initial assigned the value is one (1), this loop is work until the given condition is will be false. Condition is (i < n). When this condition is true then repeat the step 4.
  4. If (n % i == 0) is true the go to and execute this piece of code (sum = sum + i).
  5. After complete this process we compare the given value n is and sum if the both are equal then we print given number is perfect number otherwise print given number is not perfect number.

Algorithm

1. Start
2. Declared n as integer
3. Read n from the user
4. Set sum = 0
5. Iterate from 1 to n and go to step 6
6. Check n % i == 0 then step 7
7. Set sum = sum + i
    [End loop]
8. if sum == n
    PRINT "perfect number"
9. else
    PRINT "not a perfect number"
10. End

Pseudocode

1. INCLUDE stdio.h
2. FUNCTION main()
    1. DECLARED n AS INTEGER
    2. PRINT "Enter the Number: "
    3. READ n FROM USER
    4. SET sum = 0
    5. FOR i to n - 1
        1. IF n % i == 0
            SET sum = sum + i
    6. IF sum == n
        PRINT "perfect number"
    7. ELSE
        PRINT "not perfect number"
    8. RETUTRN 0
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...