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"); }
}