OOPS PRACTICAL CLASS #1 Questions and Answer

 QUESTIONS:




ANSWERS:

1.) 

class HelloWorld {    

    // Your program begins with a call to main().     
   // Prints "Hello, World" to the terminal window.     

   public static void main(String args[])     {  
       
               System.out.println("Hello, World");    
       }
  }




2.)

import java.util.*;
class grade
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n;
        System.out.println("Enter the Marks: ");
        n=sc.nextInt();
        if(n>90 && n<=100)
        {
            System.out.println("The Grade is: O");

        }
        else if(n>80 && n<=90)
        {
            System.out.println("The Grade is: E");

        }
        else if(n>70 && n<=80)
        {
            System.out.println("The Grade is: A");

        }
        else if(n>60 && n<=70)
        {
            System.out.println("The Grade is: B");

        }
        else if(n>50 && n<=60)
        {
            System.out.println("The Grade is: C");

        }
        else if(n>40 && n<=50)
        {
            System.out.println("The Grade is: D");

        }
        else if(n<40)
        {
            System.out.println("The Grade is: F");

        }
        else
        {
            System.out.println("INVALID INPUT");

        }
    }
}



3.)

// Java program to find the sum of numbers in a given range

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int start, end;
Scanner sc = new Scanner(System.in);
start = sc.nextInt();
end = sc.nextInt();
int i, sum = 0;
for(i = start; i <= end; i++)
{
                        if(i>0)
                        {
sum = sum + i;
                        }
}
System.out.print(sum);
}
}



4.)

  1. import java.util.Scanner;  
  2. public class TableExample  
  3. {  
  4. public static void main(String args[])   
  5. {  
  6. Scanner sc = new Scanner(System.in);  
  7. System.out.print("Enter number: ");       
  8. //reading a number whose table is to be print  
  9. int num=sc.nextInt();  
  10. //loop start execution form and execute until the condition i<=10 becomes false  
  11. for(int i=1; i <= 10; i++)  
  12. {  
  13. //prints table of the entered number      
  14. System.out.println(num+" * "+i+" = "+num*i);  
  15. }  
  16. }  
  17. }  



5.)

USING FOR LOOP

 class Star {

public static void main(String[] args) {

int rows = 5;

for (int i = 1; i <= rows; ++i) {  //Outer loop for rows

for (int j = 1; j <= i; ++j) { //Inner loop for Col

System.out.print("* "); //Print *

}

System.out.println(); //New line

}

}

}


USING WHILE LOOP

import java.io.*;
 
class star{
    public static void main(String[] args)
    {
        int r = 1, c = 0, n = 5;
        // the while loop check the conditions until the
        // condition is false. if it is true then enter in
        // to loop and execute the statements
        while (r <= n) {
            while (c <= r - 1) {
                // printing the required pattern
                System.out.print("* ");
                c++;
            }
            r++;
            c = 0;
            // new line after each row
            System.out.println();
        }
    }
}




USING DO-WHILE LOOP
class Star{
public static void main(String args[]){
int row=1,column=1;
int x;
do{
x=5;
do{
System.out.print("");
x--;
}while(x>=row);
column=1;
do{
System.out.print("* ");
column++;
}while(column<=row);
System.out.println(" ");
row++;
}while (row<=5);
}
}

6.)

//Java Program to find the Factorial of a Number
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        //Declare and Initialize the variable
        System.out.println("Enter the number: ");
        int num=sc.nextInt();
        int i=1,fact=1;
        while(i<=num)
        {
            fact=fact*i;
            i++;
        }
        System.out.println("Factorial of the number: "+fact);  
     }   
}

7.)

import java.util.Scanner;
class Rev
{
public static void main(String[] arg)
{
int a,res=0,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
while(n!=0)
{
a=n%10;
res=(res*10)+a;
n=n/10;
}
System.out.println("reverse of a number is "+res);
}
}

8.)

import java.util.Scanner;

public class Main
{
   public static void main(String[] args)
   {
      int num, i, count=0;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter a Number: ");
      num = s.nextInt();
      
      for(i=2; i<num; i++)
      {
         if(num%i == 0)
         {
            count++;
            break;
         }
      }
      
      if(count==0)
         System.out.println("\nIt is a Prime Number.");
      else
         System.out.println("\nIt is not a Prime Number.");
   }
}


9.)


import java.util.Scanner;
class Perfect
{
	public static void main(String arg[])	
	{
	    long n,sum=0;
             	    Scanner sc=new Scanner(System.in);	   	 
	    System.out.println("Enter a number");
                   n=sc.nextLong();
	    int i=1;
	    while(i<=n/2)
	    {
	       if(n%i==0)
	       {
		sum+=i;
	       }
	      i++;
	    }
	if(sum==n)
	{
	System.out.println(n+" is a perfect number");
               } 
	else
	System.out.println(n+" is not a  perfect number"); 
	}
}

10.)

import java.util.Scanner; public class Armstrong { public static void main(String[] args) { int my_input, my_temp, my_remainder, my_result; my_result = 0; Scanner my_scanner = new Scanner(System.in); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); my_temp = my_input; while (my_temp != 0){ my_remainder = my_temp % 10; my_result += Math.pow(my_remainder, 3); my_temp /= 10; } if(my_result == my_input) System.out.println(my_input + " is an Armstrong number"); else System.out.println(my_input + " is not an Armstrong number"); } }


Previous Post Next Post