You will be given a positive integer N, and two single-digit integers p and q, w
ID: 3802053 • Letter: Y
Question
You will be given a positive integer N, and two single-digit integers p and q, where p != q and both are greater than 1. You must output all of the integers from 1 to N inclusive, separated by a comma ','. However, any integer divisible by p or q should be replaced by the text OUT and any integer whose decimal representation contains digit p or q should be replaced by the text THINK. Integers for which both of the preceding statements are true should instead be replaced by the text OUTTHINK. Input: A single line on standard input per test case: N p q Output: The comma-separated sequence as described above (only numbers and uppercase chars), with no leading or trailing spaces. programe in C++ langauge
Explanation / Answer
import java.util.*;
class chegg2
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter n");
int n=scan.nextInt();
System.out.println("Enter p");
int p=scan.nextInt();
System.out.println("Enter q");
int q=scan.nextInt();
int a[]=new int[100];
int r=0,temp=0,num=0;
for(int i=1;i<=n;i++)
{
temp=0;
if(i<12)
{
if(i%p!=0&&i%q!=0)
System.out.print(i+",");
if(i%p==0||i%q==0)
{
if(i==p||i==q)
{
System.out.print("OUTTHINK,");
} else
System.out.print("OUT,");
}
}
if(i>11)
{
r=num=i;
while(r>0)
{
a[temp]=r%10;
temp++;
r=r/10;
}
for(int j=temp-1;j>=0;j--)
{
if(a[j]==p||a[j]==q)
{
if(num%p==0||num%q==0)
{
System.out.print("OUTTHINK,");
}
else
System.out.print("THINK,");
break;
}
if(j==0)
{
if(i%p==0||i%q==0)
{
System.out.print("OUT,");
}
else
System.out.print(num+",");
}
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.