Java: Learning the Basics

Situation: Find the greater number between two numbers.

import java.util.*; 
public class Greaternumber
{
          private int num1, num2; 
          Scanner scr = new Scanner(System.in); 
          {
                  System.out.print("\n\t Enter first number: "); 
                  num1=scr.nextInt();

                  System.out.print("\n\t Enter first number: ");
                  num2=scr.nextInt(); 
          }
          public void check()
          {
                  if(num1>num2)
                  {
                          System.out.print("\n\t Greater number is " + num1);
                  }
                  else
                  {
                         System.out.print("\n\t Greater number is " + num2);
                  }
          }
          public static void main(String[] a)
          {
                  Greaternumber obj=new Greaternumber();
                  obj.accept();
                  obj.check();
          }
}

Situation: Display the sum of ten numbers plugged in by the users.

import java.util.*; 
public class Tennumbers
{
          private int num, counter, sum; 
          Scanner scr = new Scanner(System.in); 

          public void accept()
          {
                  do
                  {
                             System.out.print("\n\t Enter first number: "); 
                             num=scr.nextInt(); 

                             sum=sum+num;
                             counter++;
                   } while(counter<=10);
          }
          public void display()
          {
                  System.out.print("\n\t Sum of the nos: " + sum); 
          }           
          public static void main(String[] a)
          {
                  Tennumbers obj=new Tennumbers();
                  obj.accept();
                  obj.display();
          }
}

Situation: Find the factorial of a given number.

import java.util.*; 
public class Factorial
{
          private int num, fact;
          Scanner scr = new Scanner(System.in); 

          public void accept()
          {
                  System.out.print("\n\t Enter first number: "); 
                  num1=scr.nextInt(); 
          }
          public void calculate()
          {
                  fact=1;
                  
                  while(num>0)
                  {
                           fact=fact*num; 
                           num--;
                  }
          }
          public void display()
          {
                  System.out.print("\n\t Factorial " + fact); 
          }           
          public static void main(String[] a)
          {
                  Factorial obj=new Factorial();
                  obj.accept();
                  obj.calculate();
                  obj.display();
          }
}

Situation: Check if a number is a palindrome or not.

import java.util.*; 
public class Palindrome
{
          private int num, org, rev=0, rem; 
          Scanner scr = new Scanner(System.in); 

          public void accept()
          {
                  System.out.print("\n\t Enter first number: ");
                  num=scr.nextInt(); 
          }
          public void calculate()
          {
                  org=num;
      
                  while(num>0)
                  {
                        rem=num%10;
                        rev=rev*10+rem;
                        num=num/10;
                  }
          }
          public void display()
          {
                  if (rev==org)
                  {
                        System.out.print("\n\t Palindrome); 
                  }
                  else
                  {
                        System.out.print("\n\t Palindrome);
                  }
          }           
          public static void main(String[] a)
          {
                  Palindrome obj=new Palindrome();
                  obj.accept();
                  obj.calculate();
                  obj.display();
          }
}
Advertisements

One thought on “Java: Learning the Basics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s