A blog dedicated for programming in different languages, it gives you in depth understanding of the programming world.
It can teach you programming in just few days.
(Follow Youtube for more : http://bit.ly/2xjHHgg )
import java.util.*;
public class qstn_3
{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Taking user input System.out.println("Enter the sentence"); String s = sc.nextLine(); //For lowercase characters String low = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"; //For Uppercase characters String cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM"; //Taking the length of input string int length = s.length(); //To check for the boundary condition if(length<4||length>99) { System.out.println("INVALID LENGTH"); System.exit(0); } //For the new word String new_word = ""; for(int i=0;i<length;i++) { //Extracting char c = s.charAt(i); //Checking for the uppercase character if(Character.isUpperCase(c)) { //To get the position of the character //so that you can encode it. for(int j = 0;j<cap.length();j++) { char c1 = cap.charAt(j); //To compare both the characters //to check if they are equal if(c==c1) { //Increasing the character by 13 position char lower_case = cap.charAt(j+13); //Storing the new character new_word+=lower_case;//new_word = new_word+lower_case; break; } } } //Checking for the lowercase character else if(Character.isLowerCase(c)) { //To get the position of the character //so that you can encode it. for(int j = 0;j<low.length();j++) { char c1 = low.charAt(j);
//To compare both the characters //to check if they are equal if(c==c1) {
//Increasing the character by 13 position char upper_case = low.charAt(j+13); //Storing the new character new_word+=upper_case; break; } } } //For exclaimation mark character else if(c=='!') { new_word+="?"; } //For any other characters else { new_word+=c; } } //Result output System.out.println("\nThe cipher text is:"); System.out.println(new_word); }
}
import java.util.*;
public class qstn_1
{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the input "); int n = sc.nextInt(); int n1 = n; int count = 0; if(n>1000) { System.out.println("INVALID INPUT"); System.exit(0); } if(n>=48) { int div = n/48; n = n-div*48; count = count + div; } if(n>=24) { int div = n/24; n = n-div*24; count = count + div; }
if(n>=12) { int div = n/12; n = n-div*12; count = count + div; } if(n>=6) { int div = n/6; n = n-div*6; count = count + div; } System.out.println("Remaining boxes = "+n); if(n!=0) { count++; } System.out.println("Total number of boxes = "+n1); System.out.println("Total number of cartons = "+count);
import java.util.*;
public class qstn_3
{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of words"); int n = sc.nextInt(); //To shift the command to next line as after an integer input when we take a string
// input the command does not shift to next line and is rather skipped.
//It does not read the newline character in your input created by hitting "Enter," sc.nextLine(); //Boundary cases if(n<2 && n>9) { System.out.println("Invalid Input"); System.exit(0); } // word array String s[] = new String[n]; // length array int length[] = new int[n]; for(int i = 0;i<n;i++) { // taking the string input s[i] = sc.nextLine(); // length of each word stored in the array length[i] = s[i].length(); } int j = 0; String k = ""; int flag = 0; // infinite loop as we don't know the length of // maximum word. while(true) { // Running the loop for each word for(int i = 0;i<n;i++) { // This will check if the length is not zero if(length[i]!=0) { // We extract the character char c = s[i].charAt(j); //We store the character in the variable with a space k = k+c+" "; // We decrease the length length[i]--; // A check variable for exit condition flag++; } // If there are no character, we just add two spaces. else { k = k+" "; } } // We increase the character position j++; // Display of the words System.out.println(k); // Erasing the word for next loop k=""; //Exit condition when no more character in any word if(flag==0) { System.exit(0); } //Initializing the flag to zero for next loop flag = 0; } }
}
import java.util.*;
public class qstn_1
{ public static int check(int n,int pos,int input) { int result=0; int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; for(int i = pos;i<prime.length;i++) { int sum = n+prime[i]; if(sum==input) { result= prime[i]; break; } sum = 0; } return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number"); int n = sc.nextInt(); // boundary conditions if(n<9 || n>50) { System.out.println("INVALID INPUT. NUMBER OUT OF RANGE."); System.exit(0); } if(n>1 && n%2!=0) { System.out.println("INVALID INPUT. NUMBER IS ODD."); System.exit(0); } int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; int n1[] = new int[10]; int n2[] = new int[10]; int ins1 = 0,ins2 = 0; int length = prime.length; int pos = 0; int result=0; int pos2 = 0; while(length!=0) { pos2 = pos; result = check(prime[pos],pos++,n); if(result!=0) { n1[ins1++] = prime[pos2];// number sending n2[ins2++] = result; // result from function } length--; } for(int i=0;i<ins1;i++) { System.out.println(n1[i]+", "+n2[i]); } }
}
Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user
to generate and display the corresponding date. Also, accept ‘N’ (1 <= N <= 100) from the
user to compute and display the future date corresponding to ‘N’ days after the generated date.
Display an error message if the value of the day number, year and N are not within the limit or
not according to the condition specified.
Test your program with the following data and some random data:
Example 1
INPUT: DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT: DATE: 12 TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4 TH OCTOBER, 2018
Example 2
INPUT: DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT: DATE: 26 TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9 TH FEBRUARY, 2019
Example 3
INPUT: DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT: DAY NUMBER OUT OF RANGE.
Example 4
INPUT: DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330
OUTPUT: DATE AFTER (N DAYS) OUT OF RANGE.
import java.util.*;
public class qstn_1
{ static int[] daycheck(int day_number, int year) {
int dd[] = {31,28,31,30,31,30,31,31,30,31,30,31}; boolean leap = false; if(year % 4 == 0) { if( year % 100 == 0) { // year is divisible by 400, hence the year is a leap year if ( year % 400 == 0) leap = true; else leap = false; } else leap = true; } else leap = false; if(leap!=false) dd[1] = 29; int res = day_number; int flag = 0; int result [] = {0,0}; while(res>31) { res = res-dd[flag++]; } result[0] = res;//date result[1] = flag++;//month return result; } static int[] after_check(int day,int day_after,int year,int month) { int dd[] = {31,28,31,30,31,30,31,31,30,31,30,31}; boolean leap = false; if(year % 4 == 0) { if( year % 100 == 0) { // year is divisible by 400, hence the year is a leap year if ( year % 400 == 0) leap = true; else leap = false; } else leap = true; } else leap = false; if(leap!=false) dd[1] = 29; int res = day; int flag = month--;//as as array index starts from 0 int left = dd[flag]-day; if(left==day_after) day+=dd[flag]; else if(left<day_after) { day_after= day_after -left; day = dd[flag++];// Giving the maximum day of the month } else if(left>day_after) { day +=day_after;// adding the after day to current day day_after = 0; } int yc = 0; //for change in year if(flag==12) { flag=0; yc = 1; } while(day_after!=0) { // if there is a month change so year has to change if(flag==11&&day_after!=0) { flag=0; yc = 1; } // when finally day after is less than the days in that particular month if(day_after<dd[flag]) { day = day_after; day_after = 0; } //when day afer is exactly as number of days in month else if(day_after==dd[flag]) { day = day_after; day_after = 0; } //when day after is greater than the number of days in month we add the days //we subtract the day after from number of days in the month and also //add the days for calculation in the current period. else if(day_after>dd[flag]) { day = day_after-dd[flag]; day_after-=dd[flag++]; } } if(yc==1) year +=1; else year = year; int result[] = new int[3]; result[0] = day; result[1] = flag;// month result[2] = year; return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the day number"); int n = sc.nextInt(); System.out.println("Enter the year"); int y = sc.nextInt(); System.out.println("Enter the date after (N days) "); int a = sc.nextInt(); if(n<1 || n>366) { System.out.println("DAY NUMBER OUT OF RANGE"); System.exit(0); } if(a<1 || a>100) { System.out.println("DATE AFTER (N DAYS) OUT OF RANGE"); System.exit(0); } String year = Integer.toString(y); int length = year.length(); if(length!=4) { System.out.println("INVALID YEAR"); System.exit(0); } int day[] = daycheck(n,y); //System.out.println(day[0]); int after[] =after_check(day[0],a,y,day[1]); String mon1 = "",mon2 =""; String month[] = {"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"}; if(day[0]>3) { mon1 = "TH"; } if( after[0]>3) { mon2="TH"; } if(day[0]==1) { mon1 = "ST"; } if(after[0]==1) { mon2 = "ST"; } if(day[0]==2) { mon1 = "ND"; } if(after[0]==2) { mon2 = "ND"; } if(day[0]==3) { mon1 = "RD"; } if (after[0]==3) { mon2 = "RD"; }
//else
//{
//mon1 = "TH";
//mon2 = "TH";
//} System.out.println(day[0]+" "+mon1+" "+month[day[1]]+","+y ); System.out.println(after[0]+" "+mon2+" "+month[after[1]]+","+after[2]); }
3. Write a program to accept a sentence which may be terminated by either ‘.’ , ‘?’ or
‘!’ only. The words are to be separated by a single blank space and are in UPPER
CASE.
Perform the following tasks:
(a) Check for the validity of the accepted sentence.
(b) Convert the non-palindrome words of the sentence into palindrome words by
concatenating the word by its reverse (excluding the last character).
Example: The reverse of the word HELP would be LEH (omitting the last
alphabet) and by concatenating both, the new palindrome word is
HELPLEH. Thus, the word HELP becomes HELPLEH.
Note: The words which end with repeated alphabets, for example ABB
would become ABBA and not ABBBA and XAZZZ becomes
XAZZZAX.
[Palindrome word: Spells same from either side. Example: DAD, MADAM etc.]
(c) Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:
Example 1
INPUT: THE BIRD IS FLYING.
OUTPUT: THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF
Example 2
INPUT: IS THE WATER LEVEL RISING?
OUTPUT: IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR
Example 3
INPUT: THIS MOBILE APP LOOKS FINE.
OUTPUT: THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF
Example 4 INPUT: YOU MUST BE CRAZY#
OUTPUT: INVALID INPUT
import java.util.*;
public class qstn_2
{ static String palincheck(String word) { int flag = 0; String s1 = "",s2 = "",s3 = ""; String result = ""; // Reversing the word. for(int i=word.length()-1;i>=0;i--) { char c = word.charAt(i); s1 = s1+c; } //Compare the reverse word with original word. if(s1.equalsIgnoreCase(word)) { flag=1; } //When the word is not palindrome. if(flag!=1) { int l = word.length(); char last_char = word.charAt(l-1); int dupli_length = 0; int count = 0; //To get the length when the word has repeated //character ending for(int i = l-1;i>=0;i--) { char c1 = word.charAt(i); if(last_char==c1) { count++; dupli_length = i; } //So that only last repeated character else break; } //Change the length when the characters //have repeated ending. if(count<=1) { dupli_length = l; } //We increase it by 1 for array indexing purposes. if(count>1) { dupli_length++; } for(int i = 0;i<dupli_length-1;i++) // hello hell lleh { char c = word.charAt(i); s2=c+s2; } s3 = word+s2; result = s3; } return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the sentence"); String s = sc.nextLine(); int l = s.length(); String words[] = new String[100]; String k = ""; int ins = 0; if(s.charAt(l-1)=='?'||s.charAt(l-1)=='.'||s.charAt(l-1)=='!') { for(int i = 0;i<l;i++) { char c = s.charAt(i); if(c==' '||c=='?'||c=='.'||c=='!') { words[ins++] = k; k = ""; } else { k = k+c; } } for(int i = 0;i<ins;i++) { String w = palincheck(words[i]); words[i] = w; } System.out.println(s); for(int i =0;i<ins;i++) System.out.print(words[i]+" "); } else System.out.println("INVALID INPUT"); }
}
import java.util.Scanner;
public class smith_number
{ public static int sum(int number) { int sum = 0; while(number!=0) { int mod = number%10; sum = sum+mod; number = number/10; } return sum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number"); int n = sc.nextInt(); int n1 = n; int check = 2; int result1 = sum(n); int result2 = 0; while(n!=0) { if(n%check==0) { n = n/check; result2 = result2+sum(check); } else if(check>n1) break; else check++; } if(result1==result2) System.out.println(n1+" is a Smith Number"); else System.out.println(n1+" is not a Smith Number"); sc.close(); }
}
/*
Input : n = 4
Output : Yes
Prime factorization = 2, 2 and 2 + 2 = 4
Therefore, 4 is a smith number
Input : n = 6
Output : No
Prime factorization = 2, 3 and 2 + 3 is
not 6. Therefore, 6 is not a smith number
Input : n = 666
Output : Yes
Prime factorization = 2, 3, 3, 37 and
2 + 3 + 3 + (3 + 7) = 6 + 6 + 6 = 18
Therefore, 666 is a smith number
Input : n = 13
Output : No
Prime factorization = 13 and 13 = 13,
But 13 is not a smith number as it is not
a composite number
*/
import java.util.*;
public class keith_number
{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number"); String s = sc.next();//taking it as String input int length = s.length();//taking the string length int n1 = Integer.parseInt(s);//converting the string into integer int n2 = n1;//making copy of the number for digit separation operation int a[] = new int[100]; int insert = length; while(n2!=0) { int mod = n2%10; a[--insert] = mod;//each digits are stored in array separately n2 = n2/10; } int flag = length; int sum = 0; int count= 0; while(true)//Going for infinite loop { for(int i=count;i<flag;i++)//adding last n numbers, where n stands for number of digits { sum += a[i]; } if(sum>n1)//if the sum exceeds the original number, we exit the program { System.out.println(n1+" is not a Keith Number"); System.exit(0); } if(sum==n1)//if sum is equal to number, than it is a Keith number { System.out.println(n1+" is a Keith Number"); System.exit(0); } a[flag++] = sum;//adding the sum back to the array, and length of array iteration also exceeds count++;//increasing the array starting value sum=0;//initializing the sum back to zero } }
}
/*
A n digit number x is called Keith number if it appears in a special sequence
(defined below) generated using its digits. The special sequence has first n terms
as digits of x and other terms are recursively evaluated as sum of previous n terms.
The task is to find if a given number is Keith Number or not.
Examples:
Input : x = 197
Output : Yes
197 has 3 digits, so n = 3
The number is Keith because it appears in the special
sequence that has first three terms as 1, 9, 7 and
remaining terms evaluated using sum of previous 3 terms.
1, 9, 7, 17, 33, 57, 107, 197, .....
Input : x = 12
Output : No
The number is not Keith because it doesn't appear in
the special sequence generated using its digits.
1, 2, 3, 5, 8, 13, 21, .....
Input : x = 14
Output : Yes
14 is a Keith number since it appears in the sequence,
1, 4, 5, 9, 14, ...
*/
import java.util.Scanner;
public class EMIRP_number
{ public static boolean check(int n) { int count=0; for(int i=1;i<n;i++) { if(n%i==0) count++; } if(count==1) return true; return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int n = sc.nextInt(); int n1 = n; boolean b1 = check(n); int rev = 0; while(n!=0) { int mod = n%10; rev = rev*10+mod; n/=10; } boolean b2 = check(rev); if(b1==true && b2==true) System.out.println(n1+" is an EMIRP number"); else System.out.println(n1+" is not an EMIRP number"); }
}
/*
Input : n = 13
Output : 13 is Emirp!
Explanation :
13 and 31 are both prime numbers.
Thus, 13 is an Emirp number.
import java.util.*;
public class mersenne_number
{ static boolean prime_check(double n) { int count = 0; for(int i=1;i<n;i++) { if(n%i==0) count++; } if(count==1) return true; else return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter till where mersenne number is to be generated"); int n = sc.nextInt(); double a[] = new double[100];// storing the answers int count = 0; for(int i=2;i<n;i++) { double number = Math.pow(2, i)-1; // 2^k-1 if(number<=n) { boolean result = prime_check(number); if(result==true) { a[count++] = number; } } else { break; } } if(count!=0) { System.out.println("The Mersenne number are as follows :"); for(int i=0;i<count;i++) { System.out.println(a[i]); } } else System.out.println("There are no mersenne number"); }
}
/*
Mersenne Prime is a prime number that is one less than a power of two. In other words,
any prime is Mersenne Prime if it is of the form 2^k-1 where k is an integer greater than
or equal to 2. First few Mersenne Primes are 3, 7, 31 and 127.
The task is print all Mersenne Primes smaller than an input positive integer n.
Examples:
Input: 10
Output: 3 7
3 and 7 are prime numbers smaller than or
equal to 10 and are of the form 2^k-1
import java.util.*;
// Harshad number is also called niven number
public class harshad_number
{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number to be checked"); int n = sc.nextInt(); int n1 = n; int sum = 0; while(n1!=0) { int rem = n1%10; sum = sum + rem; n1 = n1/10; } if(n%sum==0) { System.out.println(n+" is a harshad number"); } else { System.out.println(n+" is not a harshad number"); } }
}
/*
Input: 3
Output: 3 is a Harshad Number
Code:
import java.util.Scanner;
public class duck_number
{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number to be checked"); String s = sc.next(); char c = s.charAt(0); if(c=='0') System.out.println(s+" is not a duck number"); else { int count = 0; for(int i=1;i<s.length();i++) { c = s.charAt(i); if(c=='0') count++; } if(count>0) System.out.println(s+" is a duck number"); else System.out.println(s+" is not a duck number"); } }
}
/*
Input : 707069
Output : It is a duck number.
Explanation: 707069 does not contains zeros at the beginning.
Input : 02364
Output : It is not a duck number.
Explanation: in 02364 there is a zero at the beginning of the number.
*/
import java.util.*;
public class disarium_number
{ static boolean check(int n1) { //Reversing the number String s = Integer.toString(n1),s2 = ""; //Reverse loop for(int i=0;i<s.length();i++) { char c = s.charAt(i); s2 = c+s2;// reverse number in string format } int n = Integer.parseInt(s2); double sum = 0; int power = 1; //checking the number while(n!=0)//531 { int rem = n%10; sum = sum+Math.pow(rem,power); n = n/10; power++; } System.out.println("The sum is "+sum+" and Original Number is "+n1); if(sum==n1) return true; else return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); //taking user input System.out.println("Enter the number to be checked"); int n = sc.nextInt(); //sending the number to be checked boolean b = check(n); if(b==true) System.out.println(n+" is a disarium number"); else System.out.println(n+" is not a disarium number"); sc.close(); }
}
/*
Input : n = 135
Output : Yes
1^1 + 3^2 + 5^3 = 135
Therefore, 135 is a Disarium number
Input : n = 89
Output : Yes
8^1+9^2 = 89
Therefore, 89 is a Disarium number
import java.util.Scanner;
public class automorphic_number
{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number to be checked "); int n = sc.nextInt(); int square = n*n; System.out.println(square); String s = Integer.toString(n); int l = s.length(); double power = Math.pow(10, l); double rem = square%power; if(rem==n) System.out.println(n+" is an automorphic number"); else System.out.println(n+" is not an automorphic number"); sc.close(); }
}
/*
Input : N = 76
Output : Automorphic
Explanation: As 76*76 = 5776
Input : N = 25
Output : Automorphic
As 25*25 = 625
Input : N = 7
Output : Not Automorphic
As 7*7 = 49
5
25
Code:
import java.util.*;
public class kaprekar_number
{ static int check(int square,int n) { String s = Integer.toString(square); int l = s.length(); if(l%2==0) // when square has even digits { String s1 = s.substring(0,l/2);//20 int n1 = Integer.parseInt(s1); String s2 =s.substring(l/2,l); //25 int n2 = Integer.parseInt(s2); System.out.println(s1+" "+s2); if((n1+n2)==n&& n1>0 &&n2>0) return 1; } else // When the square has odd digits { String s1 = s.substring(0,l/2+1); int n1 = Integer.parseInt(s1); String s2 =s.substring(l/2+1,l); int n2 = Integer.parseInt(s2); System.out.println(s1+" "+s2); if((n1+n2)==n && n1>0 &&n2>0) return 1; String s3 = s.substring(0,l/2); int n3 = Integer.parseInt(s3); String s4 =s.substring(l/2,l); int n4 = Integer.parseInt(s4); System.out.println(s3+" "+s4); if(((n3+n4)==n)&& n3>0 &&n4>0) return 1; } return 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number to be checked"); int n = sc.nextInt(); int square = n*n; System.out.println(square); int result = check(square,n); if(result ==1) System.out.println(n+" is a kaprekar number"); else System.out.println(n+" is not a kaprekar number"); sc.close(); }
}
/*
Input : n = 45
Output : Yes
Explanation : 45^2 = 2025 and 20 + 25 is 45
Input : n = 13
Output : No
Explanation : 13^2 = 169. Neither 16 + 9 nor 1 + 69 is equal to 13
Input : n = 297
Output : Yes
Explanation: 297^2 = 88209 and 88 + 209 is 297 and 882+09 = 891
Input : n = 10
Output : No
Explanation: 10^2 = 100. It is not a Kaprekar number even if
sum of 10 + 0 is 10. This is because of the condition that
none of the parts should have value 0.
*/