PracticalAssignmentPrograms PDF
PracticalAssignmentPrograms PDF
import java.util.Scanner;
public class NeonNumber
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int sqr = n * n;
while (sqr > 0)
{
int r = sqr % 10;
sum = sum + r;
sqr = sqr / 10;
}
if (n == sum)
{
System.out.println("Neon Number");
}
else
{
System.out.println("Not Neon Number");
}
}
}
8 WAP to accept a number and check if it is a Palindrome number or not.
(A palindromic number is a number that remains the same when its digits are reversed.
Eg:- 16461, 121, 252)
import java.util.Scanner;
public class PalindromeNumber
{
public static void main(String[] args)
{
int rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (n == rev)
{
System.out.println("Palindrome Number");
}
else
{
System.out.println("Not Palindrome Number");
}
}
}
9 WAP to accept a number and check if it is a Perfect number or not.
(A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding
the number itself. Eg: - 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number.
import java.util.*;
public class PerfectNumber
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
sum = sum + i;
}
}
if (n== sum)
{
System.out.println("Perfect Number");
}
else
{
System.out.println("Not Perfect Number");
}
}
}
10 WAP to accept a number and check if it is a Special number (Krishnamurthy) or not.
(A number is said to be special number when the sum of factorial of its digits is equal to the
number itself. Example- 145 is a Special Number as 1!+4!+5!=145.)
import java.util.Scanner;
public class SpecialNumber
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
int fact=1;
for(int i=1;i<=r;i++)
{
fact=fact*i;
}
sum = sum+fact;
num = num / 10;
}
if(n==sum)
{
System.out.println("Special Number" );
}
else
{
System.out.println("Not Special Number" );
}
}
}
11 WAP to accept a number and check if it is a SPY number or not.
(A spy number is a number where the sum of its digits equals the product of its digits.
Eg: 1124 is a spy number, the sum of its digits is 1+1+2+4=8 and the product of its digits is
1*1*2*4=8.)
import java.util.*;
public class SpyNumber
{
public static void main(String[] args)
{
int prod = 1, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
sum = sum + r;
prod=prod * r;
num = num / 10;
}
if (prod == sum)
{
System.out.println("Spy Number");
}
else
{
System.out.println("Not Spy Number");
}
}
}
12 WAP to display all the Buzz numbers from M to N.
import java.util.*;
public class SpyNumber
{
public static void main(String[] args)
{
int prod = 1, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
int n = sc.nextInt();
int num = n;
while (num > 0)
{
int r = num % 10;
sum = sum + r;
prod=prod * r;
num = num / 10;
}
if (prod == sum)
{
System.out.println("Spy Number");
}
else
{
System.out.println("Not Spy Number");
}
}
}
13 WAP to accept two numbers and find the LCM and GCD/HCF of it.
import java.util.*;
public class LcmGcd
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter 2 numbers");
int a = sc.nextInt();
int b = sc.nextInt();
int lcm=0, hcf=0;
for(int i=1;i<=b; i++)
{
if(a%i==0 && b%i==0)
hcf=i;
}
lcm=(a*b)/hcf;
System.out.println("Lcm ="+lcm);
System.out.println("Hcf ="+hcf);
}
}
14 WAP to accept a number and check if it is a Pronic number or not.
(A number is said to be a pronic number if product of two consecutive integers is equal to the
number. Example- 42 is said to be a pronic number, 42=6×7, here 6 and 7 are consecutive
integers)
import java.util.Scanner;
public class PronicNumber
{
public static void main(String[] args)
{
int n;
boolean flag=false;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
for(int i=0; i < n; i++)
{
if(i*(i+1) == n)
{
flag =true;
break;
}
}
if(flag)
{
System.out.println("Pronic Number");
}
else
{
System.out.println("Not Pronic Number");
}
}
}
15 Write a menu driven program to accept the choice from the user and display the given pattens
If choice =1 display pattern1 , if choice =2 display pattern2 otherwise display “Invalid option”.
Pattern1 Pattern2
5
1
4 5
2 3
3 4 5
4 5 6
2 3 4 5
7 8 9 10
1 2 3 4 5
11 12 13 14 15
import java.util.*;
class Patterns
{
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
System.out.println(" 1 for Pattern1");
System.out.println(" 2 for Pattern2");
System.out.println(" Enter ur choice");
int choice=sc.nextInt();
switch(choice)
{
case 1:
int a=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(a);
a++;
}
System.out.println();
}
break;
case 2:
for(int i=5;i>=1;i--)
{
for(int j=i;j<=5;j++)
{
System.out.print(j);
}
System.out.println();
}
break;
default:
System.out.println("Invalid Option"); break;
}
}
}
16 Write a menu driven program to accept the choice from the user and display the given series
If choice =1 display series1 , if choice =2 display series2 otherwise display “Invalid option”.
series1=(1+2)+ (1+2+3)+(1+2+3+4)+ --------- +(1+2+3+4+5----+n)
series2= x/5 +x/10+x/15+-------- n terms
import java.util.*;
class Series
{
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
System.out.println(" 1 for Series1");
System.out.println(" 2 for Series2");
System.out.println(" Enter ur choice");
int choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the no of terms");
int n=sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++)
{ int s=0;
for(int j=1;j<=i+1;j++)
{
s=s+j;
}
sum=sum+s;
}
System.out.println(sum);
break;
case 2:
System.out.println("Enter the X value");
int x=sc.nextInt();
System.out.println("Enter the no of terms");
int n1=sc.nextInt();
double a=5; double sum1=0;
for(int i=1;i<=n1;i++)
{ a=a*i;
sum1=sum1+ x/a;
}
System.out.println(sum1);
break;
default:
System.out.println("Invalid Option"); break;
}
}
}
17 WAP to compute the amount that a customer pays for the taxi that he hires based on the
following conditions: Input the Customers name, the taxi number and the no of kilometers.
import java.util.*;
class Taxi
{
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter the customers name");
String name=sc.next();
System.out.println(" Enter the taxi number ");
int num=sc.nextInt();
System.out.println("distance travelled");
int km=sc.nextInt();
double amt=0;
if(km>=1 & km<=10)
amt=km*25;
else if(km>10 & km<=30)
amt=25*10+(km-10)*10;
else if(km>30 & km<=70)
amt=25*10+ 20*10+ (km-30)* 15;
else if(km>70)
amt= 25*10+ 20*10+ 30*15 +(km-70)* 12;
System.out.println("Customers Name: "+name);
System.out.println("Taxi number: "+num);
System.out.println("Distance travelled: "+km);
System.out.println("Total amount to be paid: "+amt);
}
}