Write a program to check whether a number is prime or perfect number.
Used function
Used function
- #include <stdio.h>
- #include <math.h>
- int Prime(int num);
- int Perfect(int num);
- int main()
- {
- int num;
- printf("Enter a number to check Prime or Perfect number : ");
- scanf("%d", &num);
- if(Prime(num)) {
- printf("%d is Prime number.\n", num);
- }
- else {
- printf("%d is not Prime number.\n", num);
- }
- if(Perfect(num)) {
- printf("%d is Perfect number.\n", num);
- }
- else {
- printf("%d is not Perfect number.\n", num);
- }
- return 0;
- }
- int Prime(int num)
- {
- int i;
- for(i=2; i<=num/2; i++) {
- if(num%i == 0)
- {
- return 0;
- }
- }
- return 1;
- }
- int Perfect(int num)
- {
- int i, sum, n;
- sum = 0;
- n = num;
- for(i=1; i<n; i++) {
- if(n%i == 0) {
- sum += i;
- }
- }
- return (num == sum);
- }
Used recursion
- #include <stdio.h>
- #include <math.h>
- int Prime(int i, int num);
- int Perfect(int i, int num,int sum);
- int main()
- {
- int num;
- printf("Enter a number to check Prime or Perfect number : ");
- scanf("%d", &num);
- if(Prime(2,num)) {
- printf("%d is Prime number.\n", num);
- }
- else {
- printf("%d is not Prime number.\n", num);
- }
- if(Perfect(1,num,0)) {
- printf("%d is Perfect number.\n", num);
- }
- else {
- printf("%d is not Perfect number.\n", num);
- }
- return 0;
- }
- int Prime(int i, int num)
- {
- if(i > num / 2){
- return 1;
- }
- if(num % i == 0){
- return 0;
- }
- Prime(i+1,num);
- }
- int Perfect(int i, int num,int sum)
- {
- if(i == num){
- return (num == sum);
- }
- if(num % i == 0){
- Perfect(i+1,num,sum+i);
- }else{
- Perfect(i+1,num,sum);
- }
- }
একটি মন্তব্য পোস্ট করুন