Thursday, 16 June 2016

Program to find the sum of first n Natural numbers.

Explanation :
If number = 5 then the output will be 1+2+3+4+5 i.e, equal to 15.

Code:

import java.util.Scanner;
public class Sum
{
       public static void main(String[] args)
       {
            int n,i=1,sum=0;
           Scanner sc=new Scanner(System.in);
           System.out.print("Enter Number :");
           n=sc.nextInt();
          do
         {
            sum=sum+i;
            i +=1;
          } while(i<=n);
            System.out.print("Sum of First " + n + " Numbers = "+sum);
         }
  }

Program to find the Sum of Elements in an Array.

Code:

public class SumOfArray
{
      public static void main(String args[])
     {
          int[] array = {10, 20, 30, 40, 50, 10};
          int sum = 0;
          for( int num : array)
         {
              sum = sum+num;
           }
          System.out.println("Sum of array elements is:"+sum);
     }
}

Program to find the Sum Of Digits of a given Number.

Explanation :
If number = 123 then Output will be 1+2+3=6.

Code:

import java.util.*;
public class SumOfDigits
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a number to calculate sum of digits");
         int number = sc.nextInt();
         int sum = 0;
         int input = number;
         while (input != 0)
       {
           int lastdigit = input % 10;
           sum += lastdigit;
           input /= 10;
       }
          System.out.printf("Sum of digits of number %d is %d", number, sum);
          sc.close();
     }
}

Program to find the Prime numbers till a certain range.

Explanation :
If range=100 then the output will be
2
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.

Code:

import java.util.Scanner; 
public class Prime
{
   public static void main (String[] args) 
   { 
        Scanner scanner = new Scanner(System.in); 
         int i =0; int num =0; 
         //Empty String 
         String primeNumbers = ""; 
         System.out.println("Enter the value of n:"); 
         int n = scanner.nextInt(); 
         for (i = 1; i <= n; i++)
        { 
             int counter=0; 
             for(num =i; num>=1; num--)
            {
                if(i%num==0) 
               { 
                    counter = counter + 1; 
                }
            }
            if (counter ==2)
           { 
                   primeNumbers = primeNumbers + i + " "; 
            } 
        } 
      System.out.println("Prime numbers from 1 to"+ n+ " are :");
      System.out.println(primeNumbers);
    } 
}

Program to know whether a number is Prime or not.

Code:

import java.util.Scanner;
public class Prime
{  
   public static void main(String args[])
   {  
      int i,m=0,flag=0;
      Scanner sc=new Scanner(System.in);   
      int n=sc.nextInt();
      m=n/2;    
      for(i=2;i< =m;i++)
     {    
        if(n%i==0)
       {    
          System.out.println("Number is not prime");    
          flag=1;    
          break;    
        }    
      }    
      if(flag==0)    
         System.out.println("Number is prime");    
    }  
 }

Program to print Fibonacci series using Recursion.

Explanation :
In Fibonacci series, next number is the sum of previous two numbers.
For example: If count =10 then output will be 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
Recursion means calling a method within itself until it returns a value.

Code:

public class Fibonacci

    static int n1=0,n2=1,n3=0;   
    static void printFibonacci(int count)
   {   
        if(count>0)
       {   
         n3 = n1 + n2;   
         n1 = n2;   
         n2 = n3;   
         System.out.print(" "+n3);  
         printFibonacci(count-1);   
       }   
    }   
    public static void main(String args[])
   {   
      int count=10;   
      System.out.print(n1+" "+n2);//printing 0 and 1   
      printFibonacci(count-2);//n-2 because 2 numbers are already printed  
    } 
}

Program to print Fibonacci Series till a given range.

Explanation :
In Fibonacci series, next number is the sum of previous two numbers.
For example: If count =10 then output will be 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

Code:

public class Fibonacci
{
    public static void main(String args[ ])
   {
       Scanner sc= new Scanner(System.in);  
       int n1=0,n2=1,n3,i;
       count = sc.nextInt();  
       System.out.print(n1+" "+n2);//printing 0 and 1  
  
       for(i=2;i<count;++i)  
       {  
            n3=n1+n2;  
            System.out.print(" "+n3);  
            n1=n2;  
            n2=n3;  
         }  
     }

 

Program to find Factorial of a number using Recursion.

Explanation :
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120.
Recursion means calling a method within itself until it returns a value.

Code:

class Factorial
{  
    static int factorial(int n)
   {    
      if (n == 0)    
         return 1;    
      else    
         return(n * factorial(n-1));    
     }    
     public static void main(String args[])
     {  
      int i,fact=1;  
      int number=4;   
      fact = factorial(number);   
      System.out.println("Factorial of "+number+" is: "+fact);    
     }  
}

 

Program to find Factorial of a number.

Explanation :
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120

Code:

 class Factorial{
 public static void main(String args[]){
  int i,fact=1;
  int number=5;
  for(i=1;i<=number;i++){  
      fact=fact*i;  
  }  
  System.out.println("Factorial of "+number+" is: "+fact);  
 }
}

Program to find the reverse of a number.

Code:

import java.util.Scanner; 
class ReverseNumber 
    public static void main(String args[]) 
    {
        int n, reverse = 0;
       System.out.println("Enter the number to reverse"); 
       Scanner in = new Scanner(System.in); 
        n = in.nextInt(); 
       while( n != 0 ) 
      { 
              reverse = reverse * 10; 
              reverse = reverse + n%10; 
              n = n/10; 
       } 
        System.out.println("Reverse of entered number is "+reverse); 
     }
 }

Program to know whether a number is Palindrome or not.

Explanation :
Palindrome means "a number which equal to it's reverse".
(If n=151 then it is a Palindrome as it's reverse is also 151).

Code:

class Palindrome
{
       public static void main(String args[])
      {
           int num = Integer.parseInt(args[0]);
           int n = num;
           int reverse=0,remainder;
           while(num > 0)
           {
                remainder = num % 10;
                reverse = reverse * 10 + remainder;
                num = num / 10;
            }
            if(reverse == n) System.out.println(n+" is a Palindrome ");
            else System.out.println(n+" is not a Palindrome");
       }
}

Output:  java Palindrome 1331
                1331 is a Palindrome


Program to know whether a number or a String is Palindrome or not using Recursion.

Explanation :
Palindrome means "a number which equal to it's reverse".
(If n=151 then it is a Palindrome as it's reverse is also 151).
Recursion means calling a method within itself until it returns a value.

Code:

import java.util.Scanner;
class PalindromeCheck
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the String for check:");
        String string = sc.nextLine();
        if(isPal(string))
            System.out.println(string + " is a palindrome");
        else
            System.out.println(string + " is not a palindrome");
    }
   
    public static boolean isPal(String s)
    {  
        if(s.length() == 0 || s.length() == 1)
              return true;
        if(s.charAt(0) == s.charAt(s.length()-1))
              return isPal(s.substring(1, s.length()-1));
        return false;//This statement will be not executed if String is palindrome
    }
}

Program to know whether a number is Armstrong or not using Recursion.

Explanation :
Recursion means calling a method within itself until it returns a value.
(If n=153 then Armstrong = 1*1*1 + 5*5*5 + 3*3*3 i.e,
Armstrong =1+125+27=153.)

Code:

import java.util.*;
class armstrong
{
     public static void main(String args[])
    {
      int i,n,sum,m;
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter a number : ");
      n=sc.nextInt(); 
      armstrong obj= new armstrong();
      m=obj.checknum(n); 
      if(n==m)
         System.out.println("It is a armstrong number");
      else
         System.out.println("Not a armstrong number");
     }
     int checknum(int n)
     {
        if(n==0)
             return 0;
         else
             return (int)Math.pow(n%10,3)+ checknum(n/10);
     }
}

Program to print Armstrong numbers within a range.

Explanation :
If i=1000, then all the Armstrong numbers below 1000 will be printed.
(If n=153 then Armstrong = 1*1*1 + 5*5*5 + 3*3*3 i.e,
Armstrong =1+125+27=153.)

Code:

import java.util.*;
public class Armstrong
{
     public static void main(String[] args)
    {
        int n, count = 0, b, sum = 0;
        System.out.print("Enter the Range Limit:");
        Scanner sc=new Scanner(System.in);
        int input=sc.nextInt();
        System.out.print("Armstrong numbers from 1 to "+input+" are:");
        for(int i = 1; i <= input; i++)
       {
                       n = i;
                       while(n > 0)
                      {
                           b = n % 10;
                           sum = sum + (b * b * b);
                           n = n / 10;
                      }
                      if(sum == i)
                     {
                         System.out.print(i+" ");
                     }
          sum = 0;
         }
     }
}

Program to know whether a number is Armstrong or not.

Explanation :
If n=153 then Armstrong = 1*1*1 + 5*5*5 + 3*3*3 i.e,
Armstrong =1+125+27=153.

Code:

import java.util.Scanner;
public class Armstrong
{
    public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       int num = sc.nextInt(); 
       int n = num; 
       int arm=0,remainder; 
       while(num > 0)
       {
           remainder = num % 10; 
           arm = arm + (int)Math.pow(remainder,3); 
           num = num / 10; 
        } 
           if(arm == n) System.out.println(n+" is an Armstrong Number"); 
           else System.out.println(n+" is not a Armstrong Number"); 
     }
}

Program to concatenate two strings.

Conditions :
1)If s1 or s2 is null then print "Error".
2)If s1=abc ,s2=de then print "deabcde".
3)If s1=xy,s2=abc then print "xyabcxy".
4)If s1=abc,s2=xyz then print "axbycz".

Code:

public class WeaveingString
{
  public static void main(String[] args)
  {
     System.out.println(weaveingStrings("abc", "xyz")); 
   }
  
  public static String weaveingStrings(String s1, String s2) 
 {
    String s3=" ";
    if(s1=="" || s2 =="")
    {
       s3=(String)(s3+"Error");
       return s3;
     }
    else if(s1.length()>s2.length())
    {
       s3=(String)s2+s1+s2;
       return s3; 
     }
     else if(s1.length()<s2.length())
    {
       s3= (String)s1+s2+s1;
       return s3;
      }
      else
     {
        char i1[]=s1.toCharArray();
        char i2[]=s2.toCharArray();
        for(int i=0;i<s1.length();i++)
       {
            s3+=(char)i1[i];
            s3+=(char)i2[i];
        }
         return s3;
       }
    }
}