Вы находитесь на странице: 1из 29

/*Assigment 1 Prog 1*/

import java.util.*;
class getDiscount
{
public static void main (String args[ ])
{
Scanner sc=new Scanner(System.in);
double dis = 0.0d,S = 0.0d;
System.out.print("\nEnter Customer Details:- ");
System.out.print("\nName: ");
String name = sc.nextLine();
System.out.print("Address: ");
String add = sc.nextLine ();
System.out.print("Amount of purchase: ");
double amt = sc.nextDouble();
System.out.println("Type of purchase:- ");
System.out.print("Enter 'L' for Laptop and 'D' for Desktop: ");
char chh = sc.next().charAt(0);
chh = Character.toUpperCase(chh);
switch (chh)
{
case 'L':
if (amt <= 25000)
{
dis = 0.00;
S = amt - dis;
}
else if (amt >= 250001 && amt <= 57000)
{
dis = 0.05 * amt;
S = amt - dis;
}
else if (amt >= 57001 && amt <= 100000)
{
dis = 0.075 * amt;
S = amt - dis;
}
else
{
dis = 0.1 * amt;
S = amt - dis;
}
System.out.println("Name = " +name);
System.out.println("Address = " +add);
System.out.println("Purchase Amount = " +amt);
System.out.println("Discount Amount = " +dis);
System.out.println("Payable Amount = " +S);
break;
case 'D':
if (amt <= 25000)
{
dis = 0.05 * amt;
S = amt - dis;

1|ATANU BHANDARY
}
else if (amt >= 25001 && amt <= 57000)
{
dis = 0.076 * amt;
S = amt - dis;
}
else if (amt >= 57001 && amt <= 100000)
{
dis =0.1 * amt;
S= amt-dis;
}
else
{
dis = 0.15 * amt;
S = amt - dis;
}
System.out.println("Name = " +name);
System.out.println("Address = " +add);
System.out.println("Purchase Amount = " +amt);
System.out.println("Discount Amount = " +dis);
System.out.println("Payable Amount = " +S);
break;
default:
System.out.println("Wrong Choice !!!");
}
}
}
/*Assigment 1 Prog 2*/
import java.util.*;
class papermill
{
public static void main (String args[ ])
{
Scanner sc=new Scanner(System.in);
double d = 0.0d,S = 0.0d;
System.out.print("\nEnter Customer Details:- ");
System.out.print("\nName: ");
String name = sc.nextLine();
System.out.print("Address: ");
String add = sc.nextLine ();
System.out.print("Length of the Paper Roll: ");
int length = sc.nextInt();
System.out.print("Amount of purchase: ");
double amt = sc.nextDouble();
System.out.println("Type of customer:- ");
System.out.print("Enter 'D' for Dealer and 'R' for Retailer: ");
char ch = sc.next().charAt(0);
ch = Character.toUpperCase(ch);
switch (ch)
{
case 'D':
if (length <= 0)
{
2|ATANU BHANDARY
d = amt*0.1;
S = amt - d;
}
else if (length >= 11 && length <= 20)
{
d = amt*0.15;
S = amt - d;
}
else
{
d = amt*0.20;
S = amt - d;
}
System.out.println("\nCustomer Details:- ");
System.out.println("Name = " +name);
System.out.println("Address = " +add);
System.out.println("Length of paper roll purchase =" +length);
System.out.println("Purchase Amount = " +amt);
System.out.println("Discount Amount = " +d);
System.out.println("Payable Amount = " +S);
break;
case 'R':
if (length <= 0)
{
d = amt*0.088;
S = amt - d;
}
else if (length >= 11 && length <= 20)
{
d = amt*0.10;
S = amt - d;
}
else
{
d = amt*0.15;
S = amt - d;
}
System.out.println("\nCustomer Details:- ");
System.out.println("Name = " +name);
System.out.println("Address = " +add);
System.out.println("Length of paper roll purchase =" +length);
System.out.println("Purchase Amount = " +amt);
System.out.println("Discount Amount = " +d);
System.out.println("Payable Amount = " +S);
break;
default:
System.out.println("Wrong Choice !!!!!!");
}
}
}
/**Assignment 1 Prog 3*/
public class over
{
3|ATANU BHANDARY
void check(int a, int b)// for amicable numbers
{
int s=0,x,i,j;
for(i=1;i<a;i++)
{
if(a%i==0)
s=s+i;
}
x=0;
for(j=1;j<b;j++)
{
if(b%j==0)
x=x+j;
}
if(s==b && x==a)
System.out.println(a+" & "+b+" are amicable");
else
System.out.println(a+" & "+b+" are not amicable");

}
void check(int num)// check for spy
{
int temp,sum,prdt,r;
prdt=1;
sum=0;
for(temp=num;temp!=0;temp/=10)
{
r=temp%10;
prdt*=r;
sum+=r;
}
if(sum==prdt)
System.out.println(num+" is a spy number.");
else
System.out.println(num+" is not a spy number.");
}
public static void main(String args[])
{
over obj = new over();
obj.check(220,284);
obj.check(1124);
over ob = new over();
ob.check(120,184);
ob.check(112);

}
}
/**Assignment 1 Prog 4*/
import java.util.*;
public class student
{
String Name;
int age;
4|ATANU BHANDARY
double m1,m2,m3,maximum,average;
public student(String s,int a,double x1,double x2,double x3)
{
Name = s;
age = a;
m1 = x1;
m2 = x2;
m3 =x3;
maximum=0.0d;
average=0.0d;
}
public void accept()
{
Scanner sc= new Scanner(System.in);
System.out.println("\n Enter Student's Details : ");
System.out.print("Enter Student's Name : ");
Name =sc.nextLine();
System.out.print("Enter Student's age : ");
age =sc.nextInt();
System.out.print("Enter Marks of subject 1 : ");
m1 =sc.nextDouble();
System.out.print("Enter Marks of subject 2 : ");
m2 =sc.nextDouble();
System.out.print("Enter Marks of subject 3 : ");
m3 =sc.nextDouble();
}
public void compute()
{
average =(m1+m2+m3)/3.0;
maximum =m1>m2?(m1>m3?m1:m3):(m2>m3?m2:m3);
}
public void display()
{
System.out.println("\n Student's Details : ");
System.out.println("Student's Name : " +Name);
System.out.println("Student's age : " +age);
System.out.println("Marks of subject 1 : "+m1);
System.out.println("Marks of subject 2 : "+m2);
System.out.println("Marks of subject 3 : "+m3);
System.out.println("Maximun Mark : "+maximum);
System.out.println("Avrage Marks : "+average);
}
public static void main(String args[])
{
student ob = new student("",0,0.0d,0.0d,0.0d);
ob.accept();
ob.compute();
ob.display();
}
}
/**Assignment 1 Prog 5*/
import java.util.Scanner;
public class travel
5|ATANU BHANDARY
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter distnce travelled :");
int dis = sc.nextInt();
System.out.print("Enter traveller's age :");
int age = sc.nextInt();
double fare=0.00d;
if(age<=10)
{
if(dis<=100)
fare = 50.00;
else if(dis<=300)
fare = 250.00;
else
fare = 350.00;
}
else
{
System.out.print("Enter traveller's gender :");
char gender = sc.next().charAt(0);
if(gender =='m' || gender=='M')
{
if(dis<=100)
fare = 150.00;
else if(dis<=300)
fare = 500.00;
else
fare = 750.00;
}
else
{
if(dis<=100)
fare = 100.00;
else if(dis<=300)
fare = 400.00;
else
fare = 600.00;
}
}
System.out.print("Fare charge is "+fare);
}
}
/**Assignment 1 prog 6*/
import java.util.*;
public class prime
{
public boolean prime(int n)
{
int c=0,i;
for(i=1;i<=n;i++)
{
6|ATANU BHANDARY
if(n%i==0)
c++;
}
if(c==2)
return(true);
else
return(false);
}
public void prime(long x)
{
long temp;
int r;
System.out.print("\n Prime digits of "+x+" are ");
for(temp=x;temp!=0;temp/=10)
{
r=(int)temp%10;
if(prime(r)==true)
System.out.print(" "+r);
}
}
public void prime(int m, int n)
{
if(prime(m)==true && prime(m)==true && Math.abs(m - n) == 2)
System.out.println(m+" and "+n+ " are Twin Prime");
else
System.out.println(m+" and "+n+ " are not Twin Prime");
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
prime ob =new prime();
System.out.print("\nEnter a number to test prime :");
int k=sc.nextInt();
if(ob.prime(k)==true)
System.out.println(k+ " is prime");
else
System.out.println(k+ " is not prime");
System.out.print("\nEnter a number :");
long l=sc.nextLong();
ob.prime(l);
System.out.print("\nEnter two numbers to test twin prime :");
int m=sc.nextInt();
int n=sc.nextInt();
ob.prime(m,n);
}
}
/**Asst 1 Prog 7*/
import java.util.*;
class ElecticBill
{
String n;
int units;
double bill;
7|ATANU BHANDARY
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
n=sc.nextLine().toUpperCase();
System.out.print("Enter number of units consumed: ");
units=sc.nextInt();
}
void calculate()
{
double ch=0.00d, sc=0.00d;
if(units<=100)
ch=units*2.0;
else if(units<=300)
ch= (100*2.0) + (units-100)*3.0;
else
{
ch = (100*2.0) +(200*3.0)+ (units-300)*5.0;
sc = ch *0.025;
}
bill = ch+sc;
}
void display()
{
System.out.println("\nName of Customer :"+ n);
System.out.println("Number of units consumed :"+units);
System.out.println("Bill amount :"+bill);
}

public static void main(String args[])


{
ElecticBill obj=new ElecticBill();
obj.accept();
obj.calculate();
obj.display();
}
}
/**Assignment 1 Prog 8*/
import java.util.*;
public class numberlist
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int i, n,snn=0,spe=0,spo=0;
System.out.println("\n Enter numbers and 0(zero) to end ");
for(i=1;;i++)
{
n=sc.nextInt();
if(n==0)
break;
else if(n<0)
snn+=n;
8|ATANU BHANDARY
else
{
if(n%2==0)
spe+=n;
else
spo+=n;
}
}
System.out.println("\nSum of negatine numbers = "+snn);
System.out.println("Sum of positive even numbers = "+spe);
System.out.println("Sum of positive odd numbers = "+spo);
}
}
/**Assignment 1 Prog 9*/
import java.util.Scanner;
class conversion
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter 1 for Binary to Decimal conversion ");
System.out.println("Enter 2 for Decimal to Hexadecomal conversion ");
System.out.print("Enter your choice : ");
int ch= s.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter a binary number:");
int binary=s.nextInt();
int decimal=0,p=0;
while(binary!=0)
{
decimal+=((binary%10)*(int)Math.pow(2.0,p));
binary/=10;
p++;
}
System.out.println("Decimal equvalent is "+decimal);
break;
case 2:
System.out.print("Enter a decimal number : ");
int num =s.nextInt();
int rem=0;
String hexa="";
for(int temp=num;temp>0;temp/=16)
{
rem=temp%16;
if(rem<=9)
hexa=rem+hexa;
else
hexa=(char)(rem+55)+hexa;
}
System.out.println(" Hexadecimal equivalent is " +hexa);
break;
9|ATANU BHANDARY
default:
System.out.println("Wrong Choice !!!!");
}
}
}
/**Assignment 1 Prog 10*/
import java.util.Scanner;
public class specialno
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter number : ");
int N = sc.nextInt();
if(N>999999999)
System.out.println("\n Beyond 9 digits");
else
{
int rev=0,r, temp;
int tote=0, toto=0,totz=0;
for(temp=N;temp!=0;temp/=10)
{
r=temp%10;
rev=rev*10+r;
if(r==0)
totz+=1;
if(r%2==0)
tote+=1;
if(r%2!=0)
toto+=1;
}
System.out.println("\nTotal of even digits = "+tote);
System.out.println("Total of odd dogits = "+toto);
System.out.println("Reverse of "+N+" = "+rev);
System.out.println("Total of zero(0) digits = "+totz);
}
}
}
/**Asst 2 Prog 1*/
import java.util.*;
public class ASST2PROG1
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("\n Press 1 for Niven number test ");
System.out.println(" Press 2 for Neon number test ");
System.out.print(" Enter Your choice :");
int ch= sc.nextInt();
switch(ch)
{
case 1:

10 | A T A N U B H A N D A R Y
System.out.print("Please enter number to be
tested:");
Long inputNo = sc.nextLong();
//Calculate sum of digits of inputNo
long temp = inputNo;
int sumOfDigits = 0;
while (temp > 0) {
long rem = temp % 10;
sumOfDigits += rem;
temp = temp / 10;
}
//Check if inputNo is divisible by sum of its digits
if (inputNo % sumOfDigits == 0) {
System.out.println(inputNo + " is a Niven
Number");
} else {
System.out.println(inputNo + " is not a Niven
Number");
}
break;
case 2:
System.out.print("\nEnter the number to be checked.:
");
int num=sc.nextInt();
int square=num*num;
int sum=0;
while(square!=0)//Loop to find the sum of digits.
{
int a=square%10;
sum=sum+a;
square=square/10;
}
if(sum==num)
{
System.out.println(num+" is a Neon Number.");
}
else
{
System.out.println(num+" is not a Neon
Number.");
}
break;
default:
System.out.println("Wrong Choice!");
}
}
}
/**ASST 2 PROG 2*/
import java.util.*;
public class ASST2PROG2
{
public static void main(String args[])
{
11 | A T A N U B H A N D A R Y
Scanner OBJ=new Scanner(System.in);
System.out.println("\nPRESS 1 TO PRINT TRIBONACCI SERICES UPTO N
TERMS ");
System.out.println("PRESS 2 TO FINDING ALL LEAP YEARS BETWEEN A
RANGE OF YEARS ");
System.out.print("ENTER YOUR CHOICE: ");
int ch=OBJ.nextInt();
switch(ch)
{
case 1:
System.out.print("ENTER THE LIMITS( VALUE OF n): ");
int mx=OBJ.nextInt();
int prev, next, third, fourth, n;
prev=0;
next=0;
third=1;
System.out.println("Tribonacci Series upto "+mx +"
terms ");
System.out.print(prev+" "+next+" "+third);
for(n=4;n<=mx;n++)
{
fourth=third+prev+next;
System.out.print(" "+fourth);
prev=next;
next=third;
third=fourth;
}
break;
case 2:
System.out.print("ENTER THE LOWER RANGE :");
int L=OBJ.nextInt();
System.out.print("ENTER THE UPPER RANGE :");
int U=OBJ.nextInt();
for(int y=L;y<=U;y++)
{
if(y%4==0 && y%100!=0 ||y%400==0)
System.out.println(y+" is leap year");
}
break;
default :
System.out.println("WRONG CHOICE!");
}
}
}
/**ASST 2 PROG 3*/
import java.util.*;
public class ASST2PROG3
{
public static int test(int num)
{
int i, flag=0;
for (i = 2; i < num; i++)
{
12 | A T A N U B H A N D A R Y
if (num % i != 0)
flag = 1;
else
{
flag = 0;
break;
}
}
if (flag == 1 || num == 2 || num == 1)
return (1);
else
return (0);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("\nPRESS 1 TO PRINT PATTERN 1 ");
System.out.println("PRESS 2 TO PRINT PATTERN 2 ");
System.out.print("ENTER YOUR CHOICE: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
int i, j;
int num = 1;
for (i = 0; i < 5; i++)
{
System.out.println();
for (j = 0; j <= i; j++)
{
while (test(num)==0)
{
num++;
}
System.out.print((num++) +"\t");
}
}
break;
case 2:
int a,b,c,height=5;
for (a=1;a<=height;a++)
{
for(c=height-1;c>=a;c--)
System.out.print(" ");
for(b=1;b<=a;b++)
System.out.print(b+" ");
System.out.println();
}
for(a=height-1;a>=1;a--)
{
for(c=height-1;c>=a;c--)
System.out.print(" ");
for(b=1;b<=a;b++)

13 | A T A N U B H A N D A R Y
System.out.print(b+" ");
System.out.println();
}
break;
default :
System.out.println("WRONG CHOICE!");
}
}
}
/*Asst 2 Prog 4*/
import java.util.*;
public class ASST1PROG4
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=0;
System.out.println("\nPRESS 1 FOR SERIES 1 ");
System.out.println("PRESS 2 FOR SERIES 2 ");
System.out.print("ENTER YOUR CHOICE: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
double x=0.0d,s=1.0d;
System.out.print("\n Enter Number of terms : ");
n=sc.nextInt();
System.out.print(" Enter value of x : ");
x=sc.nextDouble();
for(int i=2;i<=n;i++)
{
s=s+(Math.pow(x,i)/i);
}
System.out.println(" Sum of 1st Series = "+s);
break;
case 2:
System.out.print("\n Enter value of n : ");
n=sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i+=2)
{
int k = 0;
for (int j = 1; j <= i; j+=2)
{
k += j;
}
sum += k;
}
System.out.println(" Sum of 2nd Series = "+sum);
break;
default :
System.out.println("WRONG CHOICE!");
}
14 | A T A N U B H A N D A R Y
}
}
/**ASST 2 PROG 5*/
import java.util.*;
public class Asst2Prog5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter lower limit :");
int m = sc.nextInt();
System.out.print("Enter upper limit :");
int n = sc.nextInt();
System.out.println("PRESS 1 TO PRINT ALL ARMSTRONG NUMBER
BETWEEN "+m+" AND "+n);
System.out.println("PRESS 2 TO PRINT ALL PALINDROME NUMBER
BETWEEN "+m+" AND "+n);
System.out.print("ENTER YOUR CHOICE: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
int a,c;
for(int i=m;i<=n;i++)
{
int no_of_digits = String.valueOf(i).length();
int sum = 0;
int value = i;
for (int temp = 1; temp <= no_of_digits; temp++)
{
int digit = value % 10;
value = value / 10;
sum = sum + (int) Math.pow(digit, no_of_digits);
}
if (sum == i)
System.out.println(i+" is armstrong
number");
}
break;
case 2:
for (int i = m; i <= n; i++)
{
int rev=0;
for(int temp=i;temp > 0;temp/=10)
{
rev = (rev * 10) +(temp % 10);
}
if (rev == i)
{
System.out.println(i + " is palindrome
number");
}
}
15 | A T A N U B H A N D A R Y
break;
default :
System.out.println("WRONG CHOICE!");
}
}
}
/**ASST 2 PROG 6*/
import java.util.*;
public class Asst2Prog6
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("\nPRESS 1 FOR KRISHNAMURTHY NUMBER TEST ");
System.out.println("PRESS 2 FOR PRONIC NUMBER TEST ");
System.out.print("ENTER YOUR CHOICE: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\nENTER A NUMBER TO TEST :");
n=sc.nextInt();
int s=0;
int temp=n;
while(temp!=0)
{
int rim = temp%10;
int fact = 1;
for(int i=1;i<=rim;i++)
{
fact=fact*i;
}
s=s+fact;
temp=temp/10;
}
if(s==n)
System.out.println(n+" is a Krishnamurthy
Number.");
else
System.out.println(n+" is not a Krishnamurthy
Number.");
break;
case 2:
System.out.print("\nENTER A NUMBER TO TEST :");
n = sc.nextInt();
int flag = 0;
for(int i=1; i<=n; i++)
{
if(i*(i+1) == n)
{
flag = 1;
break;
16 | A T A N U B H A N D A R Y
}
}
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic
Number.");
break;
default :
System.out.println("WRONG CHOICE!");
}
}
}

/*Asst 2 Prog 7*/


import java.util.*;
public class Asst2Prog7
{
public static long fact(int m)
{
long f=1;
for(int j=1;j<=m;j++)
f=f*j;
return(f);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,r;
System.out.print("\n ENTER VALUR OF n :");
n=sc.nextInt();
System.out.print(" ENTER VALUR OF r :");
r=sc.nextInt();
System.out.println("\nPRESS 1 FIND PERMUTATION OF "+n+" AND
"+r);
System.out.println("PRESS 2 FIND COMBINATION OF "+n+" AND "+r);
System.out.print("ENTER YOUR CHOICE: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
long p = fact(n)/fact(n-r);
System.out.println("PERMUTATION ="+p);
break;
case 2:
long c = fact(n)/(fact(n-r)*fact(r));
System.out.println("COMBINATION ="+c);
break;
default:
System.out.println("Wrong Choice!");
}
}
}
17 | A T A N U B H A N D A R Y
/*ASST 2 Prog 8*/
import java.util.*;
public class ASST2PROG8
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=0;
System.out.println("\nPRESS 1 FOR Print All Prime factors of a
given number ");
System.out.println("PRESS 2 FOR Print sum of first and last
digits of a number ");
System.out.print("ENTER YOUR CHOICE: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter a Number : ");
n=sc.nextInt();
System.out.print("The Prime Factors of "+n+" are :
");
int i=2;
while(n>1)
{
if(n%i == 0)
{
System.out.print(i+" ");
n=n/i;
}
else
i++;
}
break;
case 2:
System.out.print("Enter a Number : ");
n=sc.nextInt();
int l=n%10,r=0;
for(int temp=n;temp>0;temp/=10)
r=temp%10;
int s=l+r;
System.out.println("Sum of first and last digits is
"+s);
break;
default:
System.out.println("Wrong Choice!!");
}
}
}
/*Asst 2 Prog 9*/
import java.util.Scanner;
public class ASST2PROG9
{
public static void main(String[] Strings)
18 | A T A N U B H A N D A R Y
{
Scanner input = new Scanner(System.in);
System.out.print("Enter coefficient a: ");
double a = input.nextDouble();
System.out.print("Enter coefficient b: ");
double b = input.nextDouble();
System.out.print("Enter coefficient c: ");
double c = input.nextDouble();
double d = b * b - 4.0 * a * c;
double r1=0.0d,r2=0.0d;
if (d > 0.0)
{
r1 = (-b + Math.sqrt(d)) / (2.0 * a);
r2 = (-b - Math.sqrt(d)) / (2.0 * a);
System.out.println("The real roots are " + r1 + " and " + r2);
}
else if (d == 0.0)
{
r1 = -b / (2.0 * a);
System.out.println("The unique real root is " + r1);
}
else
{
double P =-b/(2.0*a);
double Q = Math.sqrt(Math.abs(d)) / (2.0 * a);
System.out.println("The imaginary roots are (" + P+"+i"+Q+ ")
and (" +P+"-i"+Q+")");
}
}
}
/*ASST 2 PROG 10*/
import java.util.*;
public class ASST2PROG10
{
//function for finding sum of digits
public static int sumDig(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
//function for generating prime factors and finding their sum
public static int sumPrimeFact(int n)
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
19 | A T A N U B H A N D A R Y
sum=sum+sumDig(i);
n=n/i;
}
else
i++;
}
return sum;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("\nPRESS 1 FOR KAPREKAR NUMBER TEST ");
System.out.println("PRESS 2 FOR SMITH NUMBER TEST ");
System.out.print("ENTER YOUR CHOICE: ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
int c=0, n2, sq, pow;
System.out.print("Enter the number: ");
n = sc.nextInt();
n2 = n;
sq = n*n;
while(n>0)
{
n = n/10;
c++;
}
pow = (int)Math.pow(10, c);
if(sq%pow + sq/pow == n2)
System.out.println("The number is a Kaprekar
number");
else
System.out.println("The number is not a Kaprekar
number");
break;
case 2:
System.out.print("Enter a Number : ");
n=sc.nextInt();
int a=sumDig(n);
int b=sumPrimeFact(n);
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
break;
default :
System.out.println("WRONG CHOICE!");
}
}
}
/**ASST 3 PROG 1*/
20 | A T A N U B H A N D A R Y
import java.util.*;
public class Asst3prog1
{
public static void main(String args[])
{
Scanner inn = new Scanner (System.in);
String Str, result; // stores the string
int len ; // stores length of the string
int ch; // stores choice of the user
System.out.print("\n ENTER A sentence : ");
Str = inn.nextLine();
Str=Str.trim();
int V=0;
String word = "";// For new word
System.out.println("Enter your choice--- 1 for first letter of
word ");
System.out.println("Enter your choice--- 2 for last letter of
word ");
System.out.print("Enter your choice--- 1/2 : ");
ch = inn.nextInt();
switch(ch)
{
case 1:
Str = " "+Str;
len = Str.length();// strore sentence length
for( int i=0;i<len;i++)
{
if(Str.charAt(i)==' ')
{
word = word + Str.charAt(i+1);
}
}
for(int j=0;j<word.length();j++)
{

if(word.charAt(j)=='a'||word.charAt(j)=='e'||word.charAt(j)=='i'||word.c
harAt(j)=='o'||word.charAt(j)=='u'

||word.charAt(j)=='A'||word.charAt(j)=='E'||word.charAt(j)=='I'||word.ch
arAt(j)=='O'||word.charAt(j)=='U')
{
V++;
}
}
System.out.println("THE RESULT = "+word);
System.out.println(" TOTAL VOWELS = "+V);
break;
case 2:
Str = Str+" ";
len = Str.length();// strore sentence length
for( int i=0;i<len;i++)
{
if(Str.charAt(i)==' ')

21 | A T A N U B H A N D A R Y
{
word = word + Str.charAt(i-1);
}
}
for(int j=0;j<word.length();j++)
{

if(word.charAt(j)=='a'||word.charAt(j)=='e'||word.charAt(j)=='i'||word.c
harAt(j)=='o'||word.charAt(j)=='u'

||word.charAt(j)=='A'||word.charAt(j)=='E'||word.charAt(j)=='I'||word.ch
arAt(j)=='O'||word.charAt(j)=='U')
{
V++;
}
}
System.out.println("THE RESULT = "+word);
System.out.println(" TOTAL VOWELS = "+V);
break;
default :
System.out.println("WRONG CHOICE HAVE BEEN
ENTERED");
}
}
}
/**ASST 3 PROG 1*/
import java.util.*;
public class Asst3Prog2
{
public static void main(String args[])
{
Scanner inn = new Scanner (System.in);
String Str, result; // stores the string
int len ; // stores length of the string
int ch; // stores choice of the user
System.out.print("\n ENTER A WORD : ");
Str = inn.nextLine();
Str=Str.trim();
len = Str.length();
System.out.println("Enter your choice--- 1 for reverse order
");
System.out.println("Enter your choice--- 2 for alphabetical
order");
System.out.print("Enter your choice--- 1/2 : ");
ch = inn.nextInt();
switch(ch)
{
case 1:
result ="";
for(int i=len-1;i>=0;i--)
result = result + Str.charAt(i);
System.out.println("THE RESULT = "+result);
break;
22 | A T A N U B H A N D A R Y
case 2:
result="";
for(char i='A';i<='Z';i++)
{
for(int j=0;j<len;j++)
{
if((Str.charAt(j)==i ) ||Str.charAt(j)==(i+32))
result=result+Str.charAt(j);
}
}
System.out.println("THE RESULT = "+result);
break;
default :
System.out.println("WRONG CHOICE HAVE BEEN
ENTERED");
}
}
}
/*Asst 3 Prog 3*/
import java.util.*;
public class Asst3Prog3 {

public static boolean isPalindrome(String s) {


String reverse = "";
for (int i = s.length() - 1; i >= 0; i--) {
reverse = reverse + s.charAt(i);
}
if (reverse.equals(s)) {
return true;
} else {
return false;
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter sentence: ");
String sentence = sc.nextLine()+" ";
String longestPalindrome = "", currentWord="";
System.out.println(" Palindrome word are :");
for (int i = 0; i < sentence.length(); i++)
{
char c= sentence.charAt(i);
if(c!=' ')
currentWord+=c;
else
{
if (isPalindrome(currentWord)==true)
{
System.out.println(" "+currentWord);
if (longestPalindrome.equals("")==true) {
longestPalindrome = currentWord;
}
else
23 | A T A N U B H A N D A R Y
{
if (currentWord.length() >
longestPalindrome.length())
{
longestPalindrome = currentWord;
}
}
}
currentWord="";
}
}

if (longestPalindrome.equals("")==false)
System.out.println("Longest palindrome is " +
longestPalindrome);
else
System.out.println("No palindrome words");
}
}
/**ASST 3 PROG 4*/
import java.util.*;
public class Asst3Prog4
{
public static boolean isunique(String w)
{
int i,j;
boolean f=true;
for(i=0;i<w.length();i++)
{
for(j=i+1;j<w.length();j++)
{
if (w.charAt(i)==w.charAt(j))
f=false;
}
}
return f;
}
public static void main(String args[])
{
Scanner buf = new Scanner(System.in);
System.out.print("\n ENTER THE STRING : ");
String s = buf.nextLine();
s=s+" ";
String wd = "";
int uc=0,nuc=0;
System.out.println("\nUnique words are ");
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
// extracting each word
if (c != ' ')
wd = wd + c;
else {
24 | A T A N U B H A N D A R Y
if (isunique(wd)==true) {
System.out.println(""+wd);
uc++;
}
wd = "";
}}
System.out.println("Frequency = "+uc);
System.out.println("\nNon-Unique words are ");
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
// extracting each word
if (c != ' ')
wd = wd + c;
else {
if (!isunique(wd)) {
System.out.println(""+wd);
nuc++;
}
wd = "";
}}
System.out.println("Frequency = "+nuc);
}
}
/** ASST 3 PROG 5*/
import java.util.*;
public class Asst3Prog5
{
public static String swap(String s)
{
int len =s.length();
char c[]=s.toCharArray();
for(int i=0;i<len-1;i+=2)
{
char temp = c[i];
c[i]=c[i+1];
c[i+1]=temp;
}
return (new String(c));
}
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
System.out.print("\nEnter a string :");
String Str=sc.nextLine();
Str=Str+" ";
String wd="";
for(int i=0;i<Str.length();i++)
{
char c= Str.charAt(i);
if(c!=' ')
wd=wd+c;
else
25 | A T A N U B H A N D A R Y
{
System.out.print(swap(wd)+" ");
wd="";
}
}
}
}
/**Asst 3 Prog 6*/
import java.util.*;
public class Asst3Prog6
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.print("ENTER A NAME : ");
String STR = sc.nextLine();
STR = STR.trim();
STR=" "+STR;
int lp=STR.lastIndexOf(' ');
String NW="";
for(int i=0;i<lp;i++)
{
char c=STR.charAt(i);
if(c==' ')
NW = NW+STR.charAt(i+1)+".";
}
NW = NW+STR.substring(lp+1,STR.length());
NW=NW.trim();
int l=0,ll=0;
for(int i=0;i<STR.length();i++)
{
char c=STR.charAt(i);
if(c>='a'&&c<='z' || c>='A'&&c<='Z')
l++;
}
for(int i=0;i<NW.length();i++)
{
char c=NW.charAt(i);
if(c>='a'&&c<='z' || c>='A'&&c<='Z')
ll++;
}
System.out.println(" OUTPUT : "+NW);
System.out.println(" Number of characters in original name =
"+l);
System.out.println(" Number of characters in initial name =
"+ll);
}
}
/**ASST 3 PROG 7*/
import java.util.*;
public class Asst3Prog7
{
public static void main(String args[])
26 | A T A N U B H A N D A R Y
{
Scanner inn = new Scanner (System.in);
String Str, TEMP ; // stores the string
int l ; // stores length of the string
int cch=0; // stores character in Ascii form
char c; // stores character
int i; // used in for loop
System.out.print("\n ENTER A STRING : ");
Str = inn.nextLine();
Str = Str.trim();
l = Str.length();
TEMP="";
System.out.println("INPUT STRING : "+Str);
for(i=0;i<l;i++)
{
c=Str.charAt(i);
if((c >='a'&& c<='x')||(c >='A'&& c<='X'))
cch=(int)(c+2);
else if((c >='y'&& c<='z')||(c >='Y'&& c<='Z'))
cch = (int)(c-24);
else
cch =(int)c;
c=(char)cch;
TEMP = TEMP + c;
}
System.out.println("ENCODED STRING : " +TEMP);
}
}
/**ASST 3 PROG 8 */
import java.util.*;
public class Asst3Prog8
{
public static void main(String args[])
{
Scanner OB = new Scanner (System.in);
System.out.print("\nENTER A SENTENCE : ");
String STR = OB.nextLine();
STR = STR.trim();
STR = STR +" ";
int LEN = STR.length();
String WD ="";
int pos = 0;
System.out.print("OUTPUT :");
for(int i=0; i<LEN; i++)
{
char ch1 = STR.charAt(i);
if(ch1 == ' ')
{
WD = STR.substring(pos,i); // extracting words one by
one
WD =WD.trim();
char ch2 = WD.charAt(0);
char ch3 = WD.charAt(WD.length()-1);
27 | A T A N U B H A N D A R Y
if((ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' ||
ch2=='U'
||ch2=='a' || ch2=='e' || ch2=='i' || ch2=='o'
|| ch2=='u') &&
((ch3>='a' && ch3<='z') || (ch3>='A' &&
ch3<='Z')) &&
(ch3!='A' || ch3!='E' || ch3!='I' || ch3!='O'
|| ch3!='U'
|| ch3!='a' || ch3!='e' || ch3!='i' ||
ch3!='o' || ch3!='u'))
{
System.out.println(" " +WD);

}
pos = i+1;
}
WD ="";
}
}
}

/**ASST 3 PROG 9 */
import java.util.*;
public class Asst3Prog9
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.print("\n ENTER A SENTENCE : ");
String STR = sc.nextLine();
STR = STR.trim();
STR = STR+" ";
int LEN = STR.length();
String WD =" ";
System.out.println("Input String is "+STR);
for( int i=0;i<LEN;i++)
{
char c=STR.charAt(i);
if( (c>='a' && c<='z') ||(c>='A'&&c<='Z'))
WD = WD+c;
else
continue;
}
System.out.println("Output String is "+WD);
}
}
/**Asst 3 Prog 10*/
import java.util.*;
public class Asst3Prog10
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
28 | A T A N U B H A N D A R Y
String Str; // stores the string
int ch; // stores choice of the user
System.out.print("\n Enter a String : ");
Str = sc.next();
Str=Str.trim();
Str=Str.toUpperCase();
System.out.println("Press 1 for choice 1 ");
System.out.println("Press 2 for choice 2 ");
System.out.print("Enter your choice--- 1/2 : ");
ch = sc.nextInt();
switch(ch)
{
case 1:
for(int i=0;i<Str.length();i++)
System.out.println(Str.substring(0,i+1));
break;
case 2:
for(int i=0;i<Str.length();i++)
{

System.out.print(Str.substring(i,Str.length()));
System.out.println(Str.substring(0,i));
}
break;
default:
System.out.println(" Wrong Choice!");
}
}
}

29 | A T A N U B H A N D A R Y

Вам также может понравиться