0% found this document useful (0 votes)
25 views

Assignment - 2012-13 (P11A1PSP) : Section 1: Introduction To Programming

The document contains an algorithm to check if a number is a perfect number in 3 steps. It first initializes variables to hold the number, iterator and sum. It then uses a while loop to check all divisors and sums them. Finally it compares the sum to the original number and prints a message indicating if it is a perfect number or not. It also provides 3 C code examples to check if a single number, numbers in a range or numbers from 1 to 100 are perfect numbers using the same algorithm.

Uploaded by

cd4u
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Assignment - 2012-13 (P11A1PSP) : Section 1: Introduction To Programming

The document contains an algorithm to check if a number is a perfect number in 3 steps. It first initializes variables to hold the number, iterator and sum. It then uses a while loop to check all divisors and sums them. Finally it compares the sum to the original number and prints a message indicating if it is a perfect number or not. It also provides 3 C code examples to check if a single number, numbers in a range or numbers from 1 to 100 are perfect numbers using the same algorithm.

Uploaded by

cd4u
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 2012-13 (P11A1PSP)

Submission Date: 26/11/2012 11:00 AM

Section 1: Introduction to Programming


1. Explain following definitions with example: a. Algorithm b. Flowchart c. Compiler d. Interpreter e. Linker f. Loader 2. Write an algorithm and draw a flowchart for following defnitions: a. To find odd or even number. b. To check whether the given number is prime or not. c. To check whether the given number is palindrom or not. d. To sum of digit for given number 3. What is programming language? Explain its types with merits and demerits.

Section 2: Basics Concepts of C


1. Who is

Section 3: Input and Output Section 4:Control Statements


to check perfect number

Section 5:Functions Section 5: Arrays Section 6:Strings

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 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

You might also like