Advertisements
Advertisements
Question
Given below is the program with errors in it. Identify the errors. Rewrite the program and predict the output.
import java.util.*;
class
{
main()
{
int p=0, q=0;
Scanner sc = scanner (System.in);
System.out.print ("Enter p :");
p = nextInt();
System.out.print ("Enter q :");
p = nextInt();
if(p/q=0)
{
System.out.print(q "is a factor of" p);
}
else if
{
System.out.print (q "is NOT a factor of" p);
}
}
}Code Writing
Advertisements
Solution
import java.util.*;
class P3
{
void main()
{
int p=0, q=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter p:");
p = sc.nextInt();
System.out.print("Enter q:");
q = sc.nextInt();
if(p%q=0)
{
System.out.print(q+"is a factor of "+p);
}
else
{
System.out.print(q+" is NOT a factor of "+p);
}
}
}
To verify whether q is a factor of p or not.
shaalaa.com
Is there an error in this question or solution?
Chapter 1: Revision of Class 9 Syllabus - Exercises [Page 47]
