Pratik Ghelani: java

Pratik Ghelani

Breaking

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, May 10, 2020

6:24 AM

Implementation of Caesar ciphar in java

Input:-

import java.util.Scanner;
public class main {
public static void main(String[] args) {
int i,key = 0;
char pt[] = new char[50];
char ct[] = new char[50];
Scanner s = new Scanner(System.in);
System.out.print("Enter the Plain text:");
String str = s.next();
pt = str.toCharArray();
System.out.print("Enter the key:");
key = s.nextInt();
System.out.println("---Encryption---");
System.out.print("Ciphertext:");
for(i = 0 ; i<str.length() ; i++)
{
if(pt[i]>=65 && pt[i]<=90){
ct[i] = (char)(pt[i] + key);
if(ct[i]<65){
ct[i] = (char)(ct[i] + 26);
}
if(ct[i]>90){
ct[i] = (char)(ct[i] - 26);
}
}
else
{
ct[i] = (char)(pt[i] + key);
if(ct[i]<97){
ct[i] = (char)(ct[i] + 26);
}
if(ct[i]>122){
ct[i] = (char)(ct[i] - 26);
}
}
System.out.print(ct[i]);
}
System.out.println();
System.out.println("---Decryption---");
System.out.print("Original Plaintext:");
for(i = 0 ; i<str.length() ; i++)

{
if(ct[i]>=65 && ct[i]<=90){
pt[i] = (char)(ct[i] - key);
if(pt[i]<65){
pt[i] = (char)(pt[i] + 26);
}
if(pt[i]>90){
pt[i] = (char)(pt[i] - 26);
}
}
else
{
pt[i] = (char)(ct[i] - key);
if(pt[i]<97){
pt[i] = (char)(pt[i] + 26);
}
if(pt[i]>122){
pt[i] = (char)(pt[i] - 26);
}
}
System.out.print(pt[i]);
}
System.out.println();
}
}


Output:-



Saturday, February 9, 2019

2:03 AM
Program:-

import java.util.Scanner;
class Name
{
     private String First_name;
     private String Middle_name;
     private String Last_name;
     public void setName(String First,String Middle,String Last)
     {
         First_name = First;
         Middle_name = Middle;
         Last_name = Last;
     }
           public String getFirst()
           {
                return First_name;
            }
            public String getMiddle()
           {
                return Middle_name;
            }
            public String getLast()
           {
                return Last_name;
            }
    public void FirstMiddleLast()
    {
         System.out.println(First_name+" "+Middle_name+" "+Last_name);
    }
    public void LastFirstMiddle()
    {
         System.out.println(Last_name+" "+First_name+" "+Middle_name);
    }
}
class NameTest
{
   public static void main(String[] args)
    {
       Name a = new Name();
       String b,c,d;
       System.out.print("Enter First name:");
       Scanner sc= new Scanner(System.in);
       b=sc.nextLine();
       System.out.print("Enter Middle name:");
           c=sc.nextLine();
       System.out.print("Enter Last name:");
            d=sc.nextLine();
        a.setName(b,c,d);
        System.out.println("Full name is:");
         a.FirstMiddleLast();
         a.LastFirstMiddle();
   }

       
 
Output:- 


Monday, January 28, 2019

6:55 AM

Write A Java Program to Check if a Given Integer is Odd or Even.

Program:-

import java.util.Scanner;
public class problem1
{
public static void main(String[] args)
{
     int a;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the any number :");
        a = s.nextInt();
        if(a % 2 == 0)
        {
            System.out.println(""+a+" is Even ");
        }
        else
        {
            System.out.println(""+a+" is Odd ");
}
    }

}

Output:-






Friday, January 25, 2019

7:44 AM

Write a java program Create a student class with student_id, subject_code and marks which takes input using method getdata() and display result using putdata() method.


Program:-

import java.util.Scanner;
public class student {
long student_id;
int subject_code,marks;
Scanner s=new Scanner(System.in);
void getdata()
{
System.out.println("Enter the student_id :");
student_id=s.nextLong();
System.out.println("Enter the subject_code:");
subject_code=s.nextInt();
System.out.println("Enter the marks:");
marks=s.nextInt();
}
void putdata()
{
System.out.println(" student_id :"+ student_id);
System.out.println("subject_code:"+subject_code);
System.out.println("marks:"+marks);
}
public static void main(String[] args) {
student s1=new student();
s1.getdata();
s1.putdata();
}
}

Output:-



7:42 AM

Write a java program to print N numbers in revers order.

Program:-

import java.util.Scanner;
public class p5ex{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int i,j,n;
System.out.println("enter the size of array:");
n=s.nextInt();
System.out.println("revers value is:");
for(i=n;i>0;i--)
{
System.out.println(i);
}
}

}

Output:-






7:39 AM

Write a java program to revers the given digit.

Program:-

import java.util.Scanner;
public class p5b {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n;
System.out.println("enter the number:");
n=s.nextInt();
int r=0;
while(n!=0)
{
r=r*10;
r=r+n%10;
n=n/10;
}
System.out.println("revers number is:"+r);
}

}

Output:-




7:38 AM

Write a java program to print first N numbres in ascending and dscending order,where N is the user input.

Program:-

import java.util.Scanner;
public class p5a{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int i,j,n;
System.out.println("enter the size of array:");
n=s.nextInt();
int a[]=new int[n];
System.out.println("enter the element:");
for(i=0;i<n;i++) {
a[i]=s.nextInt();
}
for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) {
if(a[i]>a[j]) {
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("ascending order:");
for(i=0;i<n;i++){
System.out.print(a[i]+ " ");

}
System.out.println();
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]<a[j]){
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("descending order:");
for(i=0;i<n;i++){
System.out.print(a[i]+ " ");
}
System.out.println();
}

}

Output:-




7:35 AM

Write a program java make calculator (Addition, Subtraction, Multiplication and Division).

Program:-

import java.util.Scanner;
public class p4b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b, c, d;
System.out.print("Enter the numbers: ");
a = sc.nextInt();
System.out.print("Enter the numbers: ");

b = sc.nextInt();
Programming With Java
System.out.println("1. Add(+): ");
System.out.println("2. SUB(-): ");
System.out.println("3. MUL(*): ");
System.out.println("4. DIV(/): ");
System.out.print("Enter the choice :- \t");
c = sc.nextInt();
switch (c) {
case 1:
d = a + b;
System.out.println("add:-\t " + d);
break;
case 2:
d = a - b;
System.out.println("Sub:- \t" + d);
break;
case 3:
d = a * b;
System.out.println("mul:- \t" + d);
break;
case 4:
d = a / b;
System.out.println("Div: - \t" + d);
break;
default:
System.out.println("Wrong input:-");
}
}

}

Output:-






7:32 AM

Write a java program which takes two numbers as a input and perform following operation using switch:

Program:-

import java.util.Scanner;
public class p4{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int a,b;
System.out.print("Enter the First numbers: ");
a =sc.nextInt();
System.out.print("Enter the Second numbers: ");
b = sc.nextInt();
if(a>b)
{
System.out.println(a+"is big");
}
else
{
System.out.println(b+" is big");
}
}

}

Output:-





7:26 AM

Write a java program to find maximum of three numbers.

Program:-

public class p3b {
public static void main(String[] args)
{
int a[]=new int[3];
a[0]=20;
a[1]=40;
a[2]=60;
int size,max,i;
size=a.length;
max=a[0];
for(i=1;i<size;i++)
{
if(max<a[i])
{
max=a[i];
}
}
System.out.println("maximum is:"+max);
}

}

Output:-




Tuesday, January 22, 2019

Monday, January 21, 2019