Showing posts with label java.util.Scanner. Show all posts
Showing posts with label java.util.Scanner. 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;
           
        }
    }
   
}

Saturday, September 12, 2015

Calculator using java

/*
* Write a C program for performing as calculator which allows all Arithmetic Operators with operands from
* terminal
 */
package cb_pr176;
import java.util.Scanner;
/**
 **
 * @author Arif
 **/
public class Cb_pr176 {
    /*
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner inp=new Scanner(System.in);
    int a,b;
    float c = 0;
    char ch;
    System.out.println("Enter Your 1st number:");
    a=inp.nextInt();
    System.out.println("Enter your 2nd number:");
    b=inp.nextInt();
    System.out.println("Enter your choice, example: +, -, *, /");
    ch=inp.next().charAt(0);
    if (ch=='+')
        c=a+b;
    else if(ch=='-')
        c=a-b;
    else if(ch=='/')
        c=(float)a/(float)b;
    else if(ch=='*')
        c=a*b;
    System.out.println("The Result is:" + c);
         
    }
  }

Calcualate Simple Interest using java

/*
*Write a C program to find the Simple Interest. Take input for principle amount, rate of interest and time
*from terminal.
*Simple Interest is the money paid by the borrower to the lender, based on the Principle Amount, Interest
*Rate and the Time Period.
*Simple Interest is calculated by, SI= P * T * R / 100 formula.
*Where, P is the Principle Amount.
*T is the Time Period.
*R is the Interest Rate.
*/
package cb_pr175;
import java.util.Scanner;

public class Cb_pr175 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner inp=new Scanner(System.in);
        int pa,ye;
        float rate,si;
        System.out.println("Enter your principal amount");
        pa=inp.nextInt();
        System.out.println("Enter your number of year");
        ye=inp.nextInt();
        System.out.println("Enter your rate of interest");
        rate=inp.nextFloat();
        si=(rate*pa)*ye;
        System.out.println("Your total interest is " + si);
       
    }

}

Sunday, March 22, 2015

Swap Digits from an Integer Number Using Constructor with parameter

Write a Java Program to swap digits from a given integer number. Use Constructor with parameter. Use necessary method and instance, if required.

Reference:

1.
Course Book(The Complete Reference Java j2se) ; Page 119

2.
http://c-programming-sourcecode.blogspot.com/2014/12/c-program-to-swap-numbers.html?updated-min=2014-12-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=41

Source Code:

package Swap;
import java.util.Scanner;

class Swapnum
{
int number;
Swapnum(int digit)
{
number=digit;
}
int get_Swap(){
int swa = 0;
int remainder = 0;
do{
remainder = number%10;
swa = swa*10 + remainder;
number = number/10;
}while(number > 0);
return swa;
}
}
public class Swap {

public static void main(String args[]) {
Scanner input =new Scanner(System.in);
int number;
System.out.println("Please enter number to be Swap using Parameterized Constructor: ");
number = input.nextInt();
Swapnum r =new Swapnum(number);
System.out.println("After Swap : "+r.get_Swap());
}
}

Count Vowels from a String, Use Constructor

Write a Java Program to Count the Number of Vowels in a given string. Use Constructor, Instances and Method, if necessary. Do not use any parameter with Constructor.

Reference: 
1.  Course Book(The Complete Reference Java j2se) ; Page 118

2. http://c-programming-sourcecode.blogspot.com/2014/12/count-number-of-vowels.html?updated-min=2014-12-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=41

Java Source Code:

package vowel;
import java.util.Scanner;
class CountVowels
{
int count =0;
String str;
CountVowels()
{
str="Bangladesh" ;
}
void get()
{
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' || ch == 'A' ||
ch == 'E' || ch == 'I' || ch == 'O' ||
ch == 'U')
count++;

}
System.out.println("Enter String is " + str);
System.out.println("Number of vowel of "+str+ " is : " + count);
}
}

public class vowel{
public static void main(String[]args){
CountVowels vol = new CountVowels();
vol.get();
}
}

Tuesday, April 16, 2013

Greatest Common Divisor or GCD : JAVA OOP

Write Java Source Code on Printing the Greatest Common Divisor or GCD in Recursion

Sample Output:
Enter the base number: 100
Enter the power: 45
The GCD of 2 numbers is: 5

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
//java class
public class GCD
{
    public static int gcd(int num1, int num2)
    {
        if(num2 == 0)
        {
            return num1;
        }
        else
        {
            return gcd(num2, num1%num2);
        }
    }
}
//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 base number: ");
      int num1 = input.nextInt();
      System.out.print("Enter the power: ");
      int num2 = input.nextInt();
      GCD access = new GCD();
      System.out.print("The GCD of 2 numbers is: " + access.gcd(num1, num2));
    }
}