Program 1
Program 1
SolUTIoN:
import [Link].*;
class pangram{
public static void pangramm(String s){
s=[Link]();
s=[Link]();// to remove trailing spaces from terminal ends
for(int i=65;i<=90;i++)//if this loop gets completed this means
that the string is pangram
{
String d=""+(char)i;
if()//if one of the alphabet is not present in
the string then the program terminates making string nonpangram
{[Link]("It is not a Pangram String");
[Link](0);}
}
[Link]("It is a Pangram String");
Example 2-
Enter string to be tested:
I am the owner of this car.
It is not a Pangram String
V.d.S.:
SolUTIoN:
import [Link].*;
class largesmall{
public static void smaarge(String s){
s=[Link]();// to remove the spaces at the terminal ends
s=s+" "; // to add a space so that the word can be extracted
from the string
int l=[Link]();
String large="";
String small=[Link](0,[Link](' ')); //to extract first
word from string
String rep="";
for(int i=0;i<l;i++)
{rep=rep+[Link](i);
if([Link](i)==' ')
{if([Link]()<[Link]()) // condition to find the
longest word
large =rep;
if([Link]()>[Link]()) // condition to find the
smallest word
small =rep;
rep="";// to emptify "rep" after each iteration
}}
[Link]("LONGEST WORD: "+large);
[Link]("SMALLEST WORD: "+small);
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("INPUT :");
String s=[Link]();
smaarge(s);
}}
oUTPUT:
INPUT : Hi ! my name is prakhar pratap singh
LONGEST WORD: prakhar
SMALLEST WORD: Hi
V.d.S.:
SolUTIoN:
import [Link].*;
class unique{
public static boolean Unique(int p) // function to check if number
is unique or not
{
String s=[Link](p); // converting number to string
int f=0;
int l=[Link]();
for(int i=0;i<l;i++){
for(int j=i+1;j<l;j++)
{if([Link](i)==[Link](j))
f=1;}
}if(f==0)
return true;
else
return false; }
public static void main(String args[]){
Scanner sc =new Scanner([Link]);
[Link]("INPUT : ");
[Link]("Value of m<n");
[Link]("m = ");
int m=[Link]();
[Link]("n = ");
int n=[Link]();
if(m<30000&&n<30000)// m & n should be less then 30000
{
[Link]("OUTPUT: THE UNIQUE DIGIT INTEGERS ARE :"); // a
unique digit no. is the one in which all numbers are different
for(int i=m;i<=n;i++)
if(Unique(i))
[Link](i+" , ");
}
else
[Link]("The value of m and n should be less than 30000");
}}
oUTPUT:
Example 1-
INPUT :
Value of m<n
m = 120
n = 130
OUTPUT: THE UNIQUE DIGIT INTEGERS ARE :
120 , 123 , 124 , 125 , 126 , 127 , 128 , 129 , 130 ,
Example 2-
INPUT :
Value of m<n
m = 30009
n = 45678
The value of m and n should be less than 30000
V.d.S.:
SolUTIoN:
import [Link].*;
class sortstring{
public static void sort(String s){
s=[Link](); // to remove the spaces from terminal points
s=[Link]();// to convert trimmed string to upper case
int c=0;
String st="";
for(int i=65;i<=90;i++)
{
for(int j=0;j<[Link]();j++)//loop to check the number of
occurences of a character
{
if([Link](j)==(char)i)
++c;// to count the no. of times a character is present
in string
}
for(int k=1;k<=c;k++)
{
st =st+(char)i;// to add the character c no. of times in
the blank string
}
c=0;
}
[Link]("Original word : "+s);
[Link]("Alphabetical word : "+st);
}
public static void main(String[] args) {
Scanner sc=new Scanner ([Link]);
[Link]("Input : ");
String s=[Link]();
sort(s);
}
}
oUTPUT:
Example 1:
Input : computer
Original word : COMPUTER
Alphabetical word : CEMOPRTU
Example 2:
Input : zenith
Original word : ZENITH
Alphabetical word : EHINTZ
V.d.S.:
SolUTIoN:
import [Link].*;
class hammin{
public static void hamming(int n){
int dup=n;
if(n>0){
while(n%2==0) //to divide the number by 2 as long as
possible
{n/=2;}
while(n%3==0) //to divide the number by 3 as long as
possible
{n/=3;}
while(n%5==0) //to divide the number by 5 as long as
possible
{n/=5;}
if(n==1)
[Link](dup+" is a Hamming number");
else
[Link](dup+" is not a hamming number");
}
else
[Link]("Enter a number that is positive not
negative");
}
public static void main(String[] args) {
Scanner sc=new Scanner ([Link]);
[Link]("Enter a positive number to be tested :");
int n=[Link]();
hamming(n);
}
}
oUTPUT:
Example 1-
Enter a positive number to be tested :
90
90 is a Hamming number
Example 2-
Enter a positive number to be tested :
102
102 is not a hamming number
Example 3-
Enter a positive number to be tested :
-100
Enter a number that is positive not negative
V.d.S.:
SolUTIoN:
import [Link].*;
public class twisted {
public static boolean prime(int n)// function to check if a number
is prime
{
int c=0;
for(int i=1;i<=n;i++) {
if(n%i==0)
c++;}
if (c==2)
return true;
else return false;
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
[Link]("Enter a number to be checked :");
int n=[Link]();
if(prime(n))
{
int rev=0;
for(int i=n;i>0;i/=10)//to reverse the entered number
{
rev=rev*10+i%10;
}
[Link]("Reversed Number = "+rev);
if(prime(rev)) // to check if the reversed number is prime
[Link](n+" is 'Twisted Prime'");
else
[Link](n+" is not a twisted prime");
}
else{
[Link]("It is not prime number \nEnter a prime
number");//in case the entered number is not prime
}
}
oUTPUT:
Example 1-
Enter a number to be checked :
191
Reversed Number = 191
191 is 'Twisted Prime'
Example 2-
Enter a number to be checked :
214
It is not prime number
Enter a prime number
Example 3-
Enter a number to be checked :
97
Reversed Number = 79
97 is 'Twisted Prime'
V.d.S.:
SolUTIoN:
class pattern2{
public static void main(String args[]){
for(int i=7;i>=1;i-=2)
{
[Link](" ");//to print the spaces in
left side
for(int j=1;j<=i;j++)
{
[Link]("* ");//to print the pattern in upper right
side
}
[Link]();
}
for(int i=3;i<=7;i+=2)
{
for(int j=1;j<=(13-(i*2-1));j++)
{[Link](" ");} //to print the spaces in left side in
downward side
for(int k=1;k<=i;k++)
[Link](" *");// to print pattern in downward side
[Link]();
}
}}
oUTPUT:
*******
*****
***
*
***
*****
*******
V.d.S.:
SolUTIoN:
class pattern3
{
public static void main(String args[]){
for(int i=1;i<=5;i++)
{
for(int j=i;j<5;j++)//loop to print the left side spaces
{[Link](" ");}
for(int k=1;k<=i;k++)//loop to print the rows of numbers in
patterns after space
{[Link](k+" ");}
for(int d=i-1;d>0;d--)//loop to print the numbers after the
first set of numbers
{[Link](d+" ");}
[Link]();//to change line
}
}
}
oUTPUT:
1
121
12321
1234321
123454321
V.d.S.:
SolUTIoN:
class pattern{
public static void main(String args[])
{
for(int i=6;i>0;i--)
{
for(int j=1;j<=i;j++)//to print the upper half of pattern
{ [Link]("* ");}
[Link]();
}
for(int i=2;i<=6;i++)
{
for(int j=1;j<=i;j++)// to print of lower half of pattern
{ [Link]("* ");}
[Link]();// to change to next line
}
}
}
oUTPUT :
******
*****
****
***
**
*
**
***
****
*****
******
V.d.S.:
SolUTIoN:
import [Link].*;
class triangularnumber{
static void triangular(int n){
int sum=0;
for(int i=1; ; i++)//used this loop to find the sum of natural
numbers to compare with n
{
sum=sum+i;
if(sum==n)//to compare the sum with n for traingular no.
{ [Link](n+" is a TRIANGULAR number");
break;}
if(sum>n)//in case the value of sum exceeds n
{[Link](n+" is not a TRIANGULAR number");
break;}
}}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter a number to be tested :");
int n=[Link]();
triangular(n);
}
}
oUTPUT:
Example 1-
Enter a number to be tested :
28
28 is a TRIANGULAR number
Example 2-
Enter a number to be tested :
35
35 is not a TRIANGULAR number
V.d.S.:
SolUTIoN:
import [Link].*;
class twinprime{
public static boolean prime(int n)//method to check if a number is
prime
{
int c=0;
for(int i=1;i<=n;i++) {
if(n%i==0)
c++;}
if (c==2)
return true;
else return false;
}
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter a prime number ");
int n=[Link]();
if(prime(n)){
if(prime(n+2))//to check if the number +2 n is prime or not
[Link]("Twin primes : "+n+" "+(n+2));
if(prime(n-2))//to check if the number -2 n is prime or not
[Link]("Twin primes : "+n+" "+(n-2));
if(!prime(n-2)&&!prime(n+2))//if both numbers are not prime
[Link](n +" has no Twin primes");
} else
[Link]("It is not prime number \nEnter a prime
number");
}
}
oUTPUT:
Example 1-
Enter a prime number
5
Twin primes : 5 7
Twin primes : 5 3
Example 2-
Enter a prime number
27
It is not prime number
Enter a prime number
Example 3-
Enter a prime number
31
Twin primes : 31 29
V.d.S.:
SolUTIoN:
import [Link].*;
class lcm{
public static void main(String[] args) {
Scanner sc =new Scanner([Link]);
[Link]("Enter first number :");
int n1=[Link]();
[Link]("Enter second number :");
int n2=[Link]();
int LCM =0;
for(int i=1; ;i++)
{
if((n1*i)%n2==0)// to find the first number that is
divisible by both numbers
{ LCM =n1*i;
break ; //to terminate the loop as soon as the number is found
}}
[Link]("LCM of "+n1+" and "+n2 +" is "+LCM) ;} }
oUTPUT:
Enter first number :
345
Enter second number :
24
LCM of 345 and 24 is 2760
V.d.S.:
Create an object of the class in the main method and invoke the
member methods.
SolUTIoN:
import [Link].*;
class CabService{
String car_type ;
double km ;
double bill;
CabService(){ //it is the constructor to initialise members
car_type = "";
km=0.0;
bill = 0.0;
}
void accept(){
Scanner sc=new Scanner([Link]);
[Link]("Enter the type of car \"AC\" or \"NON_AC\"
:");//to specify the type of cars
car_type= [Link]();
[Link]("Enter the number of kilometers travelled
:");
km =[Link](); //to store the distance travelled
}
void calculate()//to calculate the bill
{
if(car_type.compareToIgnoreCase("AC")==0)//used string to check
if car is ac or not
{
if(km<=5.00)
bill = 150;
else
bill =150+(km-5.0)*10;
}
else if(car_type.compareToIgnoreCase("NON_AC")==0)
{
if(km<=5.0)
bill=120;
else
bill = 120+(km-5.0)*8;
}
else //in case the input is neither ac nor nonac
[Link]("Invalid car type ") ; }
void display(){
[Link]("CAR TYPE: "+car_type);
[Link]("KILOMETER TRAVELLED: "+km);
[Link]("TOTAL BILL: Rs."+bill);
}
public static void main(String[] args) {
CabService ob =new CabService();
[Link]();
[Link]();
[Link]();
}
}
oUTPUT:
Example 1-
Enter the type of car "AC" or "NON_AC" :
ac
Enter the number of kilometers travelled :
45
CAR TYPE: ac
KILOMETER TRAVELLED: 45.0
TOTAL BILL: Rs.550.0
Example 2-
Enter the type of car "AC" or "NON_AC" :
non_ac
Enter the number of kilometers travelled :
45
CAR TYPE: non_ac
KILOMETER TRAVELLED: 45.0
TOTAL BILL: Rs.440.0
V.d.S.:
V.d.S.:
Write a main method to create object of a class and call the above
member method.
SolUTIoN:
import [Link].*;
class PrimePalindrome{
boolean isPrime(int n)//function to check if the number is prime or
not
{
int c=0;
for(int i=1;i<=n;i++)
if(n%i==0)
++c;
if(c==2)
return true;
else
return false;}
boolean isPalin(int n)// function to check if the number is palindrome
or not
{
int rev=0;
for(int i=n;i>0;i/=10)//loop to reverse n
rev=rev*10+i%10;
if(rev==n)// to check if n is palindrome or not
return true;
else
return false;
}
void show(int n1,int n2)
{
[Link]("PrimePalindrome numbers in the given range are :
");
int c=0;
for(int i=n1;i<=n2;i++)
{
if(isPrime(i)&&isPalin(i))
{ [Link](i+" , ");
c++;}
}
if(c==0)
[Link]("There are no PrimePalindrome numbers in range");
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the lower limit : ");
int l=[Link]();
[Link]("Enter the upper limit : ");
int u=[Link]();
PrimePalindrome ob= new PrimePalindrome();
[Link](l,u);
}
}
oUTPUT:
Enter the lower limit : 100
Enter the upper limit : 200
PrimePalindrome numbers in the given range are :
101 , 131 , 151 , 181 , 191 ,
V.d.S.:
SolUTIoN:
import [Link].*;
class superspy{
public static void check(int n){
int l=0,sum=0;
for(int i=n;i>0;i/=10)// to find the sum of digits
{
sum=sum+i%10;
++l;
}
if(l==sum)
{
[Link]("Output : SUPERSPY number[SUM OF THE DIGITS = ");
oUTPUT:
Example 1-
Input : 201
Output : SUPERSPY number[SUM OF THE DIGITS = 2+0+1=3]
NUMBER OF DIGITS = 3
Example 2-
Input : 345
Output : Not an SUPERSPY number [3+4+5=12 is not equal to 3 ]
V.d.S.:
oUTPUT:
Example 1-
Enter the index number in following format: "xxxxxxx/xxx"
1257084/896
Class: 10
Year: 25
Center Number: 7084
Index Number: 896
Example 2-
Enter the index number in following format: "xxxxxxx/xxx"
2256789/908
Class: 12
Year: 25
Center Number: 6789
Index Number: 908
Example 3-
Enter the index number in following format: "xxxxxxx/xxx"
5467784/890
Invalid index number
V.d.S.:
SolUTIoN:
import [Link].*;
class oddeven{
public static void main(String args[]){
Scanner sc= new Scanner([Link]);
[Link]("Enter 20 numbers :");
int evsum=0,odsum=0;
for(int i=1;i<=20;i++)//loop to input 20 numbers by using same
variable n
{
int n=[Link]();//using same variable 20 times
if(n%2==0)//condition to check even numbers
evsum=evsum+n;
else
odsum=odsum+n;//sum if number is odd
}
[Link]("Sum of odd numbers = "+odsum );
[Link]("Sum of even numbers = "+evsum);}
}
oUTPUT:
Enter 20 numbers :
34
57
69
64
24
90
45
60
69
67
31
34
25
94
60
71
75
30
47
20
Sum of odd numbers = 556
Sum of even numbers = 510
V.d.S.:
SolUTIoN:
import [Link].*;
class tech{
public static void techno(int n)
{
int l=[Link](n).length();// to convert no. to string to
find its length
int sum=0;
if(l%2==0)// condition to check if the number contains even number
of digits
{
int h=n%(int)[Link](10,l/2);//to divide number in 2 halves
int r=n/(int)[Link](10,l/2);
sum=(int)[Link](h+r,2);// to find square of sum of 2 halves
}
else{
[Link](n+" is not an Even digit number");
[Link](0);
}
if(sum==n)
[Link](n+" is a Tech number");
else
[Link](n+" is not a Tech number");
}
public static void main(String args[]){
Scanner sc =new Scanner([Link]);
[Link]("Enter a number containing even digits : ");
int n=[Link]();
techno(n);
}
}
oUTPUT:
Example 1-
Enter a number containing even digits :
2025
2025 is a Tech number
Example 2-
Enter a number containing even digits :
5879
5879 is not a Tech number
Example 3-
Enter a number containing even digits :
45679
45679 is not an Even digit number
V.d.S.:
Write a main method to create an object of the class and call the
above member methods.
SolUTIoN:
import [Link].*;
class ElectricBill{
String n;
int units;
double bill;
void accept(){
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of customer :");
n=[Link]();
[Link]("Enter the number of units consumed :");
units = [Link]();
}
void calculate()// to calculate the bill
{
if(units<=100) //conditions to calculate the
bill as per consumption
bill = units *2;
else if(units>100&&units<=300)
bill =(units-100)*3+200;
else
{bill=(units-300)*5+800;
bill=bill+(2.5/100*bill);}
}
void print()// to print the final electricity bill of consumer
{
[Link]("Name of the customer: "+n);
[Link]("Number of units consumed: "+units);
[Link]("Bill amount: Rs."+bill);
}
public static void main(String[] args) {
ElectricBill ob = new ElectricBill();
[Link]();
[Link]();
[Link]();
}
}
oUTPUT:
Enter the number of customer :
Mayank Narayan Saini
Enter the number of units consumed :
567
Name of the customer: Mayank Narayan Saini
Number of units consumed: 567
Bill amount: Rs.2188.375
V.d.S.: