Monday, 2 November 2015

Java program to find sum of all integers greater than 1 to 100 that are divisible by 7


Write a program to find sum of all integers greater than 1 and less than 100 that are divisible by 7.

Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class SumOfDigit {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int result = 0;  
           for (int i = 1; i <= 100; i++) {  
                if (i % 7 == 0)  
                     result += i;  
           }  
           System.out.println("Output of Program is : " + result);  
      }  
 }  

 Output  
 Output of Program is : 735  


Enjoy Programming.

Generate Random numbers


Write a program to generate 5 random numbers between 1 to 100, and it should not follow with decimal point.

Input:- 5

Output:- Any 5 random numbers between 1 to 100.


Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class GenerateRandomNumber {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           for (int i = 1; i <= 5; i++) {  
                System.out.println((int) (Math.random() * 100));  
           }  
      }  
 }  

Output  
 11  
 76  
 27  
 84  
 90  

Enjoy Programming

Reversed pyramid using for loops and decrement operator


This Java pyramid example shows how to generate pyramid or triangle like given below using for loop.

12345
1234
123
12
1


Sample Program:-

 /*  
  This Java Pyramid example shows how to generate pyramid or triangle  
  like given below using for loop.  
  12345  
  1234  
  123  
  12  
  1  
  *   
  *  
  */  
 public class ReversePyramid {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           for (int i = 5; i > 0; i--) {  
                for (int j = 0; j < i; j++) {  
                     System.out.print(j + 1);  
                }  
                System.out.println("");  
           }  
      }  
 }  


 Output  
 12345  
 1234  
 123  
 12  
 1  


Enjoy Programming

Pyramid of stars using nested for loops



Its a very common program asked to do during our course of Java.This program is very good to understand how loops works in Java.


Input:- 5

Ouptut:-    *
                **
                ***
                ****
                *****


Sample Program:-

 /*  
  This Java Pyramid example shows how to generate pyramid or triangle like  
  given below using for loop.  
  *   
  **   
  ***   
  ****   
  *****   
  */  
 public class JavaPyramid {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int n = 5;  
           for (int i = 1; i <= n; i++) {  
                for (int j = 0; j < i; j++) {  
                     System.out.print("*");  
                }  
                // generate a new line  
                System.out.println("");  
           }  
      }  
 }  


Output  
 *  
 **  
 ***  
 ****  
 *****  



Enjoy Programming

Sunday, 1 November 2015

Palindrome Number in Java


Palindrome Number:- If a number is equal to its reversed number,then the given number is Palindrome number.
For example, 121 is a palindrome number.

Input:- { 1221, 23, 343, 112, 222, 524 }

Ouput:- 1221 is a palindrome
             23 not a palindrome
             343 is a palindrome
             112 not a palindrome
             222 is a palindrome
             524 not a palindrome


Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class PalindromeExample {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // array of numbers to be checked  
           int numbers[] = new int[] { 1221, 23, 343, 112, 222, 524 };  
           // iterate through the numbers  
           for (int i = 0; i < numbers.length; i++) {  
                int number = numbers[i];  
                int reversedNumber = 0;  
                int temp = 0;  
                // reverse the number  
                while (number > 0) {  
                     temp = number % 10;  
                     number = number / 10;  
                     reversedNumber = reversedNumber * 10 + temp;  
                }  
                if (numbers[i] == reversedNumber)  
                     System.out.println(numbers[i] + " is a palindrome");  
                else  
                     System.out.println(numbers[i] + " not a palindrome ");  
           }  
      }  
 }  


Output  
 1221 is a palindrome  
 23 not a palindrome   
 343 is a palindrome  
 112 not a palindrome   
 222 is a palindrome  
 524 not a palindrome  



Enjoy Programming :)

Fibonacci Series


Fibonacci Series or Fibonacci numbers are the number in the following integer sequence

        0,1,1,2,3,5,8,13,21,34,55,89,144.......

The first two numbers in Fibonacci series are 0 and 1 and each subsequent number is sum of previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by

       Fn=Fn-1+Fn-2, where F0=0 and F1=1


Input:- Generate Fibonacci series upto Limit 10

Ouptut:- 0 1 1 2 3 4 8 13 21 34


Sample Program:-


 /**  
  * @author Dixit  
  *   
  */  
 public class FibonacciExample {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // number of elements to generate in a series  
           int limit = 10;  
           long[] fibonacciSeries = new long[limit];  
           // create first 2 series elements  
           fibonacciSeries[0] = 0;  
           fibonacciSeries[1] = 1;  
           // create the Fibonacci series and store it in an array  
           for (int i = 2; i < limit; i++) {  
                fibonacciSeries[i] = fibonacciSeries[i - 1]  
                          + fibonacciSeries[i - 2];  
           }  
           // print the Fibonacci series numbers  
           System.out.println("Fibonacci Series upto the limit :" + limit);  
           for (int i = 0; i < limit; i++) {  
                System.out.print(fibonacciSeries[i] + " ");  
           }  
      }  
 }  



 Output  
 Fibonacci Series upto the limit :10  
 0 1 1 2 3 5 8 13 21 34 


Enjoy Programming

Determine If Year Is Leap Year



Leap Year:- In simple words ,Leap year is a year which are multiple of 4.It is also known as intercalary year or bissextile year.

Input:- 2004

Output:- Its a Leap year.


Sample Program:-


 /**  
  * @author Dixit  
  *  
  */  
 public class LeapYear {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           //year we want to check  
           int year = 2004;  
           //if year is divisible by 4, it is a leap year  
           if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))  
           System.out.println("Year " + year + " is a leap year");  
           else  
           System.out.println("Year " + year + " is not a leap year");  
      }  
 }  



 Output  
 Year 2004 is a leap year  


Enjoy Programming