Showing posts with label for loop in java. Show all posts
Showing posts with label for loop in java. Show all posts

Monday, September 14, 2015

Generate Fibonacci Series

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cb_pr150;
import java.util.Scanner;
/**
 * Fibonacci Series Generate
 * @author Arif
 **/

public class Cb_pr150 {
    /**
     * @param args the command line arguments
     **/

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Enter the maximum no. of terms");
        Scanner ob=new Scanner(System.in);
        int ch=ob.nextInt();
        System.out.println("Fibonacci series: ");
        int a,b,sum,n;
        a=0;
        b=1;
        System.out.print(a + ", ");
        System.out.print(b + ", ");
        for(n=1;n<=ch;n++)
        {
            sum=a+b;
            System.out.print("Term-"+n+": "+sum + ", ");
            a=b;
            b=sum;
           
        }
    }
   
}

Generate Prime Numbers list between 1 and 100

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jv.prim;
/**
 * Generate Prime between 1 and 100
 * @author sm rabbi
 */

public class JvPrim {
    /**
     * @param args the command line arguments
     **/

    public static void main(String[] args) {
       
        int a,b,c;
        for(a=1;a<=100;a++)
        {
            c=1;
            for(b=2;b<=a/2;b++)
            {
                if(a%b == 0)
                {
                    c=0;
                    //System.out.println("not prime "+ a + " " + b + " " +a%b);
                    break;
                }
            }
           
            if(c==1)
            {
                System.out.println("prime= " +a);
            }
        }
    }
}
     

Wednesday, April 17, 2013

Add Numbers inside an Array : JAVA OOP



Write Java Source code: How to Add Numbers inside an Array using For Loop




Sample Output:

Enter the size of the input you want to enter: 5
Enter 5 numbers: 2 5 3 7 25
The sum of the numbers is:42

Source Code:





//java class


public class ArraySum
{
    public int sumOfArray(int[] array)
    {
        int sum = 0;
        for(int i=0; i<array.length; i++)
        {
            sum = sum + array[i];
        }

        return sum;
    }
}

//main class


import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the input you want to enter: ");
        int size = input.nextInt();
        int[] numArr = new int[size];
         
        System.out.print("Enter "+ size +" numbers: ");
        for(int i=0; i<numArr.length; i++)
        {
          numArr[i]=input.nextInt();
          
        }

        ArraySum access = new ArraySum();
        System.out.print("The sum of the numbers is:" + access.sumOfArray(numArr));       

    }
}

Tuesday, April 16, 2013

How to sort numbers in Bubble Sort : JAVA OOP

Write a Java Source Code: How to sort numbers in Bubble Sort

Sample Output:
Enter the size of the array: 10
Enter 10 numbers: 100 35 45 3 7 2 1 500 200 15
The Sorted Numbers: 1 2 3 7 15 35 45 100 200 500

Java Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//java class
public class BubbleSort
{
 public void bubbleSort(int[] arr){
     for(int i=0; i<arr.length; i++){
        for(int j=1; j<arr.length; j++){
            if(arr[j]< arr[j-1] ){
                int temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;           
            }
        }
     }
     for(int i=0; i<arr.length; i++)
     {
         System.out.print(arr[i] + " ");
     }
}
}
//main class
import java.util.Scanner;
public class Main
{
    
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];
        System.out.print("Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }
         
        BubbleSort access = new  BubbleSort();
    System.out.print("The Sorted numbers: ");
        access.bubbleSort(x);
    }
}

Friday, February 8, 2013

Week-3: 3.5: For Loop

For:

As we mentioned earlier, the programming you are doing now is sequential
programming. This means that flow is downward, from top to bottom, with every line of
code being executed, unless you tell Java otherwise.

You saw in the last section that one way to "tell" Java not to execute every line is by
using IF Statement to section off areas of code.

Another way to interrupt the flow from top to bottom is by using loops. A programming
loop is one that forces the program to go back up again. If it is forced back up again
you can execute lines of code repeatedly.

As an example, suppose you wanted to add up the numbers 1 to 10. You could do it quite
easily in Java like this:

int addition = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;

But you wouldn't really want to use that method if you needed to add up the numbers 1
to a 1000. Instead, you can use a loop to go over a line of code repeatedly until you've
reached 1000. Then you can exit the loop and continue on your way.

Visit for Details