Search This Blog

#2. Logics like prime no etc

// Even or odd number

File Name : Even.java

import java.io.*;
class Even
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String a;
System.out.println("\n Enter any number to test even or odd");
a=br.readLine();
int n=Integer.parseInt(a);
if(n%2==1)
{
System.out.println("\n odd no :" +n);
}
else
{
System.out.println("\n even no :" +n);
}
}
}

From command prompt:
compiling the program : javac Even.java
Running the program : java Even


// To print the characters from A to Z

File Name : CharTest.java

import java.io.*;
class CharTest
{
public static void main(String arg[])throws IOException
{
char ch1='A';
while(ch1<='Z')
{
System.out.println("\b " +ch1);
ch1++;
}
}
}


//To print the Multiplication Table

FileName : MulTable.java

import java.io.*;
class MulTable
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Which table u want");
String fb=br.readLine();
int f=Integer.parseInt(fb);
int n=11;
System.out.println("Multiplication Table \n");
for(int i=1;i<n;i++)
{
int s=f*i;
System.out.println(f+" X "+i+" = "+s);

}
System.out.println("Existing............");
System.out.println("Existing............");
System.out.println("Existing............");
System.out.println("Done................");
}
}

//To print the Amgstrong numbers

FileName : Amgstrong.java

import java.util.Scanner;
public class Amgstrong
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any value");
int n=sc.nextInt();
int n1=n;
int sum=0;
while(n>0)
{
int r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
System.out.println("Sum of Cubes of Digits is called Amgstrong "+sum);
if(n1==sum)
{
System.out.println("The given Number is Amgstrong" +sum);
}
else
{
System.out.println("The given number is not Amgstrong" +sum);
}
}
}


// To print the Factorial of a given

FileName : Factorial.java 

import java.util.Scanner;
public class Factorial
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value to find factorial");
int n=sc.nextInt();
int fact=1;
int i=1;
while(i<=n)
{
fact=fact*i;
i=i+1;
}
System.out.println("Factorial="+fact);
}
}

//To check whether the given number is palindrome or not

FileName : palindrome.java


import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any value");
int n=sc.nextInt();
int n1=n;
int rev=0;
while(n>0)
{
int r=n%10;
rev=(rev*10)+r;
n=n/10;
}
System.out.println("Reverse="+rev);

if(n1==rev)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome:");
}
}
}


//To check whether the given is prime or not

FileName : Prime.java

import java.util.Scanner;
public class Prime
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value to check prime or not");
int n=sc.nextInt();
int fcount=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
fcount++;
}
if(fcount==2)
System.out.println("Prime=" +n);
else
System.out.println("Not prime="+n);
}
}

No comments:

Post a Comment