Page 321 - CA_Blue( J )_Class9
P. 321
default: System.out.println("Wrong Choice");
}
}
}
Question 5. [15]
Write a program to input a number and print the number is composite number or not.
Ans. import java.util.*;
class composite_number
{
public static void main ()
{
Scanner sc= new Scanner (System.in);
int n,i,count=0;
System.out.print("Enter a number: ");
n=sc.nextInt();
for (i=1; i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count>2)
System.out.println(n+" is Composite Number");
else
System.out.println(n+" is not Composite Number");
}
}
Question 6. [15]
Write a Java program to input a number and print the first factor of the number apart from 1.
Ans. import java.util.*;
class first_factor
{ public static void main()
{
Scanner sc= new Scanner(System.in);
int n,i;
System.out.print("Enter a number: ");
n=sc.nextInt();
for (i=2; i<=n;i++)
{
if(n%i==0)
{
break;
}
}
System.out.println("First factor of "+n + " is : "+i);
}
}
Question 7. [15]
Write a program to input the number of lines and print the following triangle using for loop.
$
&&
$$$
&&&&
$$$$$
Model Test Paper 319

