Advertisements

Aatul Palandurkar

Posts Tagged ‘Java Programming

Objectives :

  • Binary to Decimal Conversion
  • Converting Binary to Decimal
  • Write a program to convert binary to decimal
  • Write a program to convert binary number to decimal format
  • Write a program to convert binary to decimal using Scanner class
  • Write a program to convert binary to decimal, take input using Scanner class

Java Program :


import java.util.Scanner;

public class BinaryToDecimal {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter binary number: ");
String binary = scanner.nextLine();
int decimal = binaryToDecimal(binary);
System.out.println("Decimal equivalent of "+ binary +" is "+ decimal);
}

private static int binaryToDecimal(String binary) {
final int base = 2;
int decimal = 0;
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '0') {
decimal += 0 * Math.pow(base, binary.length() - i - 1);
} else if (binary.charAt(i) == '1') {
decimal += 1 * Math.pow(base, binary.length() - i - 1);
} else {
System.out.println("Invalid Binary Number");
System.out.println("Binary Number Contains only 0's or 1's");
System.exit(0);
}
}
return decimal;
}
}

Output :
Enter binary number: 1110
Decimal equivalent of 1110 is 14

Advertisements

Objectives :

  • Decimal to Binary Conversion
  • Converting Decimal to Binary
  • Converting Decimal Number to Binary format
  • Write a program to convert decimal to binary
  • Write a program to convert decimal number to binary format
  • Write a program to convert positive decimal number to binary format
  • Write a program to convert decimal to binary using Scanner class
  • Write a program to convert decimal to binary, take input using Scanner class

 

Java Program to convert Decimal to Binary : 

public class DecimalToBinary {
 
 public void toBinary(int number){
 int binary[] = new int[25];
 int n = 0;
 // Convert Decimal to Binary
 while(number > 0){
 binary[n++] = number%2;
 number = number/2;
 }
 // Print Binary number
 for(int i = n-1;i >= 0;i--){
 System.out.print(binary[i]);
 }
 }
 
 public static void main(String[] args){
 DecimalToBinary obj = new DecimalToBinary();
 obj.toBinary(14);
 }
}

Output :

1110

 

Java Program to convert Decimal to Binary using Scanner class :


import java.util.Scanner;

public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int[14];
int n = 0;

// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}

// Print Binary Number
for(int i = n-1;i >= 0;i--){
System.out.print(binary[i]);
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num= scanner.nextInt();
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(num);

}
}

Output :

Enter a number: 14

1110

Objectives :

  • Write a Java program to find prime number upto N number
  • Write a Java program to print prime number upto N number
  • Write a Java program to find prime number upto N number using Command Line Arguments
  • Write a Java Program to find prime number upto N number via Command Line Arguments
  • Write a Java program to find prime number upto N number using Scanner class

 

Java Program / Code :

Method 1 : Java Program to find Prime Number upto N number using Scanner class


import java.util.Scanner;
class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
Scanner s=new Scanner(System.in);
System.out.println(“Enter number : ”);
n=s.nextInt();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}

Method 2 : Java Program to find Prime Number upto N number using Scanner class and writing function to find prime number

Method isPrime() for checking if a number is prime or not

public class PrimeNumber{
 public boolean isPrime(int num) {
 if ( num < 2 ){
 return false;
 }
 for (int i = 2; i <= Math.sqrt(num); i++) {
 if ( num % i == 0 ) {
 return false;
 }
 }
 return true;
 }
}


// Mock Test Class to test above code
import java.util.Scanner;

public class Demo
{
 public static void main(String[] args) 
 {
 Scanner scanner = new Scanner(System.in);

 System.out.println("Please enter a number: ");
 int num = scanner.nextInt();
 
 if ( num < 2 ) {
 System.out.println("\n There are no Prime Numbers available");
 System.exit(0);
 }
 System.out.println("\n Prime Numbers from 1 to "+ num);
 PrimeNumber primeNum = new PrimeNumber();

 for (int i = 3; i <= num; i++) {
 if ( primeNum.isPrime(i) ) {
 System.out.print(", " + i);
 }
 }
 } 
}

Method 3 : Java Program to find Prime Number upto N number using Command line input


class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
n=Integer.parseint(args[0]);
for(int i=2;i&lt;n;i++)
{
p=0;
for(int j=2;j&lt;i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac PrimeNumber.java
  2. Interpretation : C:\JavaPrograms>java PrimeNumber 20

Output :

2

3

5

7

11

13

17

19

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Objectives :

  • Write a program to compute sum of digits of a given number.
  • Write a program to find sum of digits of a given number.
  • Write a program to calculate sum of digits of a given number.
  • Write a program to compute sum of digits of a given integer number.
  • Write a program to compute sum of digits of a number entered via Command Line.
  • Write a program to compute sum of digits of a given number. Take input from Command Line.
  • Write a program to compute sum of digits of a given number. Take input using Scanner class.

Following is the Java Program to compute Sum of Digits of a given integer number;

Method 1 : Java Program to find Sum of Digits when number is entered from command line


class SumOfDigits
{
public static void main(String args[])
{
int n;
int a=0;
int sum=0;

//taking integer number from command line and parsing the same
n=Integer.parseInt(args[0]);

while(n!=0)
{
a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
  2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output : Sum of digits: 15

Method 2 : Java Program to find Sum of Digits if input is taken using Scanner class


import java.util.Scanner;

public class SumOfDigits {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n;
int a=0;
System.out.print("Enter a positive number: ");
n = in.nextInt();

if (n <= 0)
System.out.println("You have entered a negative number.");
else {
int sum = 0;

while (n != 0) {

a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}
}

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
  2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output :

Trial 1 : With positive number

Enter a positive number: 12345

Sum of digits: 15

Trial 2 : With negative number

Enter a positive number: -12345

You have entered a negative number.

TIOBE programming community index is a measure of popularity of programming languages. TIOBE index for February 2013 says Java is the number one programming language in the world.

The index is based on the calculation from number of search engine results which includes Google, Blogger, Yahoo!, Wikipedia, Baidu, YouTube, Bing and Amazon for queries containing the name of the language, for this TIOBE has their own algorithms.

Following image shows the long term trends for top 10 languages :

TIOBE Trends

TIOBE Trends

I think Java will always be on top.

I Love Java

Source : TIOBE


Advertisements

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8,341 other followers

Tweets

%d bloggers like this: