### ISC practical program

Write a program to input a number and check whether the number is a composite number or not. If the number is a composite number check if its sum of digits is prime or not and print appropriate messages.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.*; class Composite { int sum(int n ) // A function to return the sum of factors of a number { int s = 0; for ( int i = 1 ; i <= n/2 ; i++ ) { if (n%i==0 ) { s = s+i; //counting the sum of factors. } } return s; } int sumd(int n) //Function to return the sum of digits { int s=0; while(n>0) { s = s+n%10; n = n/10; } return s; } public static void main() { Composite ob = new Composite(); int n, s; Scanner sc = new Scanner(System.in); System.out.println("Enter the Number " ); n = sc.nextInt(); s = ob.sumd(n); /*The sum of factors of a prime number is = 1 * where as the sum of factors of a composite number is > 1*/ if(ob.sum(n)<=1) System.out.println("The number is not a composite number"); else { if(ob.sum(s)==1) System.out.println("The number is a composite number where as the sum of digits of the number is a prime number."); else System.out.println("The number and the sum of digits of the number are both composite numbers"); } } } |

Output:

## Leave a Reply

You must be logged in to post a comment.