Perfect number, a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128.
Code::
import java.util.*;
public class PerfectNumber
{
public static void main(String[] nt)
{
Scanner in= new Scanner(System.in);
System.out.print("Enter any Number: ");
int n=in.nextInt();
int temp=n;
int sum=0;
for(int i=1;i<n;i++)
{
if(temp%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
System.out.println("Yes it is a Perfect Number");
}
else
{
System.out.println("No it is not a Perfect Number");
}
}
}
Output::
Question:: Implement a Java-method that prints out the day of the week for a given day (1..31), month (1..12) and year.
Code::
import java.util.*;
public class DayFromDate
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Date:");
int d=in.nextInt();
System.out.print("Enter Month:");
int m=in.nextInt();
System.out.print("Enter Year:");
int y=in.nextInt();
int x=(y-1900)*365+(y-1900)/4;
if(y%4==0&&m<=2)
{
x--;
}
switch(m)
{
case 12:x+=30;
case 11:x+=31;
case 10:x+=30;
case 9:x+=31;
case 8:x+=30;
case 7:x+=31;
case 6:x+=31;
case 5:x+=30;
case 4:x+=31;
case 3:x+=28;
case 2:x+=31;
}
x=x+d;
x=x%7;
String weekday;
switch(x)
{
case 0:weekday="Sunday"; break;
case 1:weekday="Monday"; break;
case 2:weekday="Tuesday"; break;
case 3:weekday="Wednesday"; break;
case 4:weekday="Thrusday"; break;
case 5:weekday="Friday"; break;
case 6:weekday="Saturday"; break;
default:System.out.println("Invalid day number");return;
}
System.out.println("Weekday is "+weekday);
}
}
Simpler way:
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter Date:");
int d=in.nextInt();
System.out.print("Enter Month:");
int m=in.nextInt();
System.out.print("Enter Year:");
int y=in.nextInt();
Calendar c=Calendar.getInstance();
c.set(Calendar.DATE, d);
c.set(Calendar.MONTH,(m-1));
c.set(Calendar.YEAR,y);
String[] days= {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
System.out.println("Day is : "+days[c.get(Calendar.DAY_OF_WEEK)-1]);
}
}
Output::
Code::
import java.util.*;
public class MultipleString {
public static void main(String[] nt) {
Scanner in =new Scanner(System.in);
System.out.print("Enter Number of Strings:");
int n=in.nextInt();
in.nextLine(); //for flushing the stream
String a[]=new String[n];
System.out.println("Enter Strings:");
for(int i=0;i<n;i++)
{
a[i]=in.nextLine();
}
System.out.println("Entered Strings:");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
Output::
Binary Search Tree Construction and Various Traversals:
Code::
import java.util.*;
class node
{
node right,left;
int d;
node()
{
d=0;
right=left=null;
}
node(int a)
{
d=a;
right=left=null;
}
}
class tree
{
node root;
tree()
{
root=null;
}
void insert(int data)
{
root=insert(root,data);
}
node insert(node n,int data)
{
if(n==null)
{
n=new node(data);
}
else if(data<=n.d)
{
n.left=insert(n.left,data);
}
else
{
n.right=insert(n.right,data);
}
return n;
}
void inorder()
{
inorder(root);
System.out.println();
}
void inorder(node s)
{
if(s!=null)
{
inorder(s.left);
System.out.print(s.d+" ");
inorder(s.right);
}
}
void preorder()
{
preorder(root);
System.out.println();
}
void preorder(node s)
{
if(s!=null)
{
System.out.print(s.d+" ");
preorder(s.left);
preorder(s.right);
}
}
void postorder()
{
postorder(root);
System.out.println();
}
void postorder(node s)
{
if(s!=null)
{
postorder(s.left);
postorder(s.right);
System.out.print(s.d+" ");
}
}
}
public class BstTree
{
public static void main(String [] nt)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Number of Elements");
int n=in.nextInt();
tree r=new tree();
System.out.println("Enter Elements:");
for(int i=0;i<n;i++)
{
r.insert(in.nextInt());
}
System.out.print("Inorder: ");
r.inorder();
System.out.print("Postorder: ");
r.postorder();
System.out.print("Preorder: ");
r.preorder();
}
}
Output::
Question:: A secret code encrypts a message by putting it in an array and reading down the columns (blanks are replaced by asterisks and full stops are added to fill up the array).
Write a program that encrypts an input string.
Example:
Input: “Lets go to the sandwich shop today”
Output: “Lohdsoe*ewhdtt*ioasoscpy**ah*.gtn*t.”
Code::
import java.util.*;
public class Encrypt
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
String a;
System.out.print("Enter Message: ");
a=in.nextLine();
if(a.length()%6!=0)
{
int x=a.length()%6;
for (int u=1;u<=6-x;u++)
{
a=a+'.'; //completing string
}
}
String x=a.replace(' ', '*'); //replacing space with *
int n=x.length()/6;
char[][] e=new char[n][6]; //array to get encrypted message
int i=0,k=0;
while(k!=x.length())
{
for(int j=0;j<6;j++)
{
e[i][j]=x.charAt(k);
k++;
}
i++;
}
System.out.println();
System.out.print("Encrypted Message: ");
for(int j=0;j<6;j++)
{
for(int s=0;s<i;s++)
{
System.out.print(e[s][j]);
}
}
System.out.println();
System.out.println();
System.out.println();
}
}
Output::
Following Code Will print the Rhombus Structure :
Code::
import java.util.*;
public class Rhombus
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Number of Rows: ");
int n=in.nextInt();
int count=1,c,space=0;
for(int i=1;i<2*n;i++)
{
if(i<=n)
{
c=i;
space++;
}
else
{
c=2*n-i;
space--;
}
for(int spc=n-space;spc>0;spc--)
{
System.out.print(" ");
}
for(int j=0;j<count;j++)
{
System.out.print(c);
if(j<count/2)
{
c--;
}
else
{
c++;
}
}
if(i<n)
{
count=count+2;
}
else
{
count=count-2;
}
System.out.println();
}
}
}
Output::
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients.
Pascal Triangle:
Code::
import java.util.*;
public class PascalTriangle
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Number of Rows: ");
int n=in.nextInt();
int [][] a=new int[n+1][];
for(int i=0;i<=n;i++)
{
a[i]=new int[i+1]; //jagged array
}
for(int i=0;i<=n;i++)
{ a[i][0]=1;
a[i][i]=1;
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for(int i=0;i<=n;i++)
{
for(int spc=2*n-i;spc>0;spc--)
{
System.out.print(" ");
}
for(int j=0;j<=i;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Output::