0% found this document useful (0 votes)
25 views

Second Class Test Question&Solution

The document contains 11 questions that provide examples of Java programs to solve various problems using loops. The problems include finding the second highest marks from student scores, finding the smallest number with 4 factors, and printing different patterns using loops. Complete Java programs with explanations are provided for each question.

Uploaded by

kiyemi8123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Second Class Test Question&Solution

The document contains 11 questions that provide examples of Java programs to solve various problems using loops. The problems include finding the second highest marks from student scores, finding the smallest number with 4 factors, and printing different patterns using loops. Complete Java programs with explanations are provided for each question.

Uploaded by

kiyemi8123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

The example question explains the loop better

1. Write a java program that accepts marks of 10 students and find the second highest
marks among all students.

import java.util.*;

class Q1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int hMarks=0, secondMarks=0;
for(int i =0;i<=10;i++)
{
System.out.println("Enter marks of students ");
int m = sc.nextInt();
if(hMarks<m)
{
secondMarks = hMarks;
hMarks = m;
}
else if(secondMarks<m)
{
secondMarks = m;
}
}
System.out.println("Second highest marks is "+secondMarks);
}
}

2. Write a java program that finds the smallest positive number that has exactly 4
factors (dividers)

import java.util.*;
public class Q2
{
public static void main(String[] args)
{
int no = 2;
while(true)
{
int count = 0;
for(int i =1;i<=no;i++)
{
if(no%i==0)
count++;
}
if(count==4)
{
System.out.println("Smallest number that has exctaly 4 factors:
"+no);
break;
}
no++;
}
}
}

3. Print the patterns using a loop.

11111
10001
10001
10001
11111

import java.util.*;

class Q3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if(i==1||i==n||j==1||j==n)
System.out.print("1 ");
else
System.out.print("0 ");
}
System.out.println();
}
}
}

4. Print the patterns using a loop.

A
BC
DEF
GHIJ

import java.util.*;

public class Q4
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
char ch ='A';
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=i;j++)
{
System.out.print(ch++ +" ");
}
System.out.println();
}
}
}

5. Print the patterns using a loop.

1
121
12321
1234321
123454321

import java.util.*;
public class Q5
{

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int k = 1;k<=i;k++)
{
System.out.print(k+" ");
}
for(int l = i-1;l>=1;l--)
{
System.out.print(l+" ");
}
System.out.println();
}
}
}
6. Print the patterns using a loop.

1
212
32123
4321234
543212345

import java.util.*;
public class Q6
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int k = i;k>=1;k--)
{
System.out.print(k+" ");
}
for(int l = 2;l<=i;l++)
{
System.out.print(l+" ");
}
System.out.println();
}
}
}

7. Print the patterns using a loop


.
*234*
1*3*5
12*45
1*3*5
*234*

import java.util.*;
public class Q7
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if(j==i || j==n-i+1)
System.out.print("* ");
else
System.out.print(j+" ");
}
System.out.println();
}
}
}

8. Print the patterns using a loop.

12345
54321
12345
54321
12345

import java.util.*;
public class Q8
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if(i%2==1)
System.out.print(j+" ");
else
System.out.print((n-j+1)+" ");
}
System.out.println();
}
}
}

9. Print the patterns


1 2 2 3 3 3 4 4 5 5 5 5 5 6 6 7 7 7 7 7 7 7 8 8 9 9 9 9 9 9 9 9 9 ……….. Up to n number

import java.util.*;
public class Q9
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of character: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
if(i%2==1)
{
for(int j = 1;j<=i;j++)
{
System.out.print(i+" ");
}
}
else
System.out.print(i+" "+i+" ");
}
}
}

10. Write a java program that accepts a number from the keyboard and represents a
number as Sum of Two Prime Numbers.

import java.util.*;
public class Q10
{

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
for(int i =2;i<=n/2;i++)
{
int j = n-i;
int c = 0;
for(int k = 2;k<=i/2;k++)
{
if(i%k==0)
{
c = 1;
break;
}
}
for(int k = 2;k<=j/2;k++)
{
if(j%k==0)
{
c = 1;
break;
}
}
if(c==0)
System.out.println(n+" is sum of two prime numbers "+i+" and
"+j);
}
}
}

11. Write a java program that accepts a binary number from the keyboard handprint
the first complement of this number.

import java.util.*;
class Q11
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive binary number");
int n = sc.nextInt();
String s = "";
for(int i = n;i!=0;i/=10)
{
int r = i%10;
if(r==0)
s = "1"+s;
else
s = "0"+s;
}
System.out.println("Complement of "+n+" is "+s);
}
}

or

import java.util.*;
class Q11
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive binary number");
int n = sc.nextInt();
String s = "";
for(int i = n;i!=0;i/=10)
{
int r = i%10;
s = (++r%2)+s;
}
System.out.println("Complement of "+n+" is "+s);
}
}

You might also like