C Programming by Jena Sisir Kumar;

C Programming by Jena Sisir Kumar;

Author:Jena, Sisir Kumar; [Jena, Sisir Kumar]
Language: eng
Format: epub
Publisher: CRC Press LLC
Published: 2021-11-21T00:00:00+00:00


By analyzing the above Program 9.5, we conclude that if we solve multiple works inside a single function then the program size increases and is prone to errors. Debugging of the above program is also difficult. To avoid this difficulty, we can use the concept of a function. We can separate the above functionality into three functions as shown in Figure 9.7.

Figure 9.7 Separating functionality using different functions.

Program 9.5

1. #include<stdio.h> 2. void main() 3. { 4. int N, i, sum=0, count=0; 5. printf("
Enter a number: "); 6. scanf("%d", &N); 7. 8. /*Code to check whether the number is prime or not*/ 9. for(i=1;i<=N;i++) 10. { 11. if(N%i==0) 12. count=count+1; 13. } 14. if(count==2) 15. printf 16. else 17. printf("
THE NUMBER IS NOT A PRIME NUMBER"); 18. /*Code to check whether the number is a perfect number or not*/ 19. 20.for(i=1;i<=N/2;i++) 21. { 22. if(N%i==0) 23. sum=sum+i; 24. } 25. if(sum==N) 26. printf("
THE NUMBER IS A PERFECT NUMBER"); 27. else 28. printf("
THE NUMBER IS NOT A PERFECT NUMBER"); 29. }

Output:

Run-1

Enter a number: 28

THE NUMBER IS NOT A PRIME NUMBER

THE NUMBER IS A PERFECT NUMBER

Run-2

Enter a number: 43

THE NUMBER IS A PRIME NUMBER

THE NUMBER IS NOT A PERFECT NUMBER



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.