- WAP to enter the current date in dd/mm/yyyy format and the day name on the 1st of January of that year. Display the name of the day on the given date.
/* Write a program to accept a date in the string format dd/mm/yyyy * and accept the name of the day * on 1st of January of the corresponding year. Find the day for the given date. Example: INPUT Date: 5/7/2001 Day on 1st January : MONDAY OUTPUT Day on 5/7/2001 : THURSDAY Test run the program on the following inputs: INPUT DATE DAY ON 1ST JANUARY OUTPUT DAY FOR INPUT DATE 4/9/1998 THURSDAY FRIDAY 31/8/1999 FRIDAY TUESDAY 6/12/2000 SATURDAY WEDNESDAY The program should include the part for validating the inputs namely the date and the day on 1st January of that year. */ import java.util.*; class CurrentDayName { public static void main (String args[]) throws InputMismatchException { Scanner scan = new Scanner(System.in); int i,dd,mm,yy,tdays, index,r; String date,dayName; System.out.println("Enter a date in the string format dd/mm/yyyy: "); System.out.println("If the date is 9th Februaray, 2013 enter it as 09/02/2013. "); date = scan.next(); dd=mm=yy=0; // Convert the date in string format to day, month and year try{ dd = Integer.parseInt(date.substring(0,2)); mm = Integer.parseInt(date.substring(3,5)); yy = Integer.parseInt(date.substring(6)); System.out.println("Enter the name of the day on 1st of January "); dayName = scan.next(); //Store the days of the months int dom[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //If it is a leap year, februrary should have 29 days. if(yy%4 == 0) dom[1] = 29; if(dd < 0 || dd > dom[mm-1]){ System.out.println("Invalid date."); }else{ //Store the day names of the week String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; //Find the corresponding day of the year tdays = 0; //Add the days of the month for(i = 0; i < mm-1; i++) tdays+= dom[i]; //Add the days of the current month tdays+= dd; index = 0; //Find the index of the entered day name in the day names array for(i = 0; i < 7; i++) { if(dayName.equalsIgnoreCase(days[i])){ index = i; break; } } //find the index of the day in the day names array for the current day r = tdays%7 + index - 1; if( r >= 7) r -= 7; System.out.println("Day on " + date + " : " + days[r]); } // else }catch(Exception e){ System.out.println("Please enter the date in the specified format."); System.out.println("Error : "+e); } }//end of main }//end of class
- WAP to enter two dates of the same year and find the number of days elapsed between them.
/* Enter two dates of the same year and find the * number of days elapsed between them */ import java.util.*; class TwoDateDiff { public static void main (String args[])throws InputMismatchException { Scanner scan = new Scanner(System.in); int d1,m1,d2,m2,y,i,total1,total2,diff; System.out.println("Enter the day and month of first date: "); d1 = scan.nextInt(); m1 = scan.nextInt(); System.out.println("Enter the day and month of second date: "); d2 = scan.nextInt(); m2 = scan.nextInt(); System.out.println("Enter the year: "); y = scan.nextInt(); int dom[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //If it is a leap year, februrary should have 29 days. if(y%4 == 0) dom[1] = 29; // Check for validity of date if(d1 < 0 || m1 > 12 || d1 > dom[m1-1] || d2 < 0 || m2 >12 || d2 > dom[m2-1]){ System.out.println("Please enter a valid date."); }else{ total1 = 0; //Add the days of the month for(i=0; i< m1-1; i++) total1 += dom[i]; //Add the days of the current month total1+=d1; total2 = 0; //Add the days of the month for(i=0; i< m2-1; i++) total2 += dom[i]; //Add the days of the current month total2+=d2; diff = (int) Math.abs(total1 - total2); System.out.println("Days elapsed : "+diff); }//else }//end of main }//end of class
- WAP to enter a paragraph which may be terminated by a '?', '.' or an '!'. Print the number of sentences that have 2 or more palindrome words in it.
/* * WAP to enter a paragraph which may be terminated by a '?', '.' or an '!'. * Print the number of sentences that have 2 or more palindrome words in it. */ import java.io.*; class NoOfPalindromes { public static void main (String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); int i,p,j,f,l,count, nos=0; String str, word, revWord; char ch, ch1; System.out.print("Enter a paragraph(terminated by '?', '.' or '!') : "); str = br.readLine(); l= str.length(); p=0; for(i=0;i< l;i++) { ch=str.charAt(i); // check for the end of a sentence if(ch=='!' ||ch=='?'|| ch=='.') { f=0; count=0; //Loop to traverse through each sentence for(j=p;j< i+1;j++) { ch1=str.charAt(j); //Extract a word if(ch1==' '||ch1=='!' ||ch1=='?'|| ch1=='.') { word=str.substring(f,j); //Reverse the word revWord = (new StringBuffer(word).reverse()).toString(); //Count palindrome words if(word.equals(revWord)) count++; f=j+1; } } // If the sentence has two or more palindrome words increment // the counter if(count>=2) nos++; p=i+1; } } System.out.println("Number of sentences with two or more palindrome words in it : "+nos); }//end of main }//end of class
- WAP to enter two sentences and print the common words in them.
/* * WAP to enter two senteces from the user. * Print the common words in them. */ import java.io.*; class CommonWords { public static void main (String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); int i,j,l1,l2,p,x,y,count=0; String str1, str2; char ch; System.out.print("Enter two sentences terminated by either a '?', '.' or '!' : "); str1 = br.readLine(); str2 = br.readLine(); l1= str1.length(); l2= str2.length(); String s1[] = new String[l1]; x=0; p=0; //Store all the words of the first sentence in a string array for(i=0;i< l1;i++) { ch = str1.charAt(i); if(ch == ' ' || ch == '?' || ch == '.' || ch == '!'){ s1[x++]=str1.substring(p,i); p = i+1; } } String s2[] = new String[l1]; y=0; p=0; //Store all the words of the second sentence in a string array for(i=0;i< l2;i++) { ch = str2.charAt(i); if(ch == ' ' || ch == '?' || ch == '.' || ch == '!'){ s2[y++]=str2.substring(p,i); p = i+1; } } //Now compare the words stored in two arrays for(i=0;i< x;i++){ for(j=0;j< y;j++){ //If match is found, print the word and store blank space to avoid repetition if(s1[i].equalsIgnoreCase(s2[j]) && !s2[j].equals(" ")){ System.out.println(s1[i]); s2[j]=" "; } } } }//end of main }//end of class
- Enter n sentences and merge them to form a single sentence, where n <= 10. The merged sentence should not have duplicate words and must only consist of letters and blank spaces. Any other character should be removed.
/* * Enter n sentences and merge them to form a single sentence, * where n <= 10. The merged sentence should not have duplicate words and must * only consist of letters and blank spaces. Any other character * should be removed. */ import java.io.*; class MergeSentence { public static void main (String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); int n,i,j,k,p,x,l,now; String ms; char ch,t; System.out.println("Enter the no of sentences (upto 10): "); n = Integer.parseInt(br.readLine()); if(n<=10){ //String array to store the sentences String str[] = new String[n]; //counter to store number of words now=0; System.out.println("Enter the "+n+" sentences: "); for(i=0;i< n;i++) { str[i]=br.readLine(); //This loop is used to count the number of words in all the sentences. //The counter is then used to initialize the array that stores all the words for(j=0;j< str[i].length();j++){ ch = str[i].charAt(j); if(ch == ' ' || ch == '?' || ch == '.' || ch == '!'){ now++; } } } //String array to store number of words String words[] = new String[now]; x=0; for(i=0;i< n;i++){ l=str[i].length(); p=0; for(j=0;j< l;j++){ ch = str[i].charAt(j); ms=""; if(ch == ' ' || ch == '?' || ch == '.' || ch == '!'){ //Loop to access the word for(k=p;k< j;k++){ t = str[i].charAt(k); //Only add letters to the merged string if((t >= 'A' && t<='Z') || (t>='a' && t<='z')) ms = ms + t; } //Store the words in string array words[x++] = ms; p = j+1; } } } //Now remove duplicate words from the string array for(i=0;i< x;i++){ for(j=i+1;j< x;j++){ if(words[i].equalsIgnoreCase(words[j])){ for(k=j;k< x-1;k++) words[k] = words[k+1]; j--; // Decrement loop counter to cater to successive duplicates x--; } } } for(i=0;i< x;i++){ System.out.print(words[i]+" "); } }else{ System.out.println("Invalid value!"); } }//end of main }//end of class
- Enter date of announcement, number of days given to complete the project and find the date of submission of project.
/* Enter date of announcement, number of days given to complete the project and * find the date of submission of project. */ import java.util.*; class DateOfSubmission { public static void main (String args[])throws InputMismatchException { Scanner scan = new Scanner(System.in); int dd, mm, yy, nod,i,tdays=0,m; System.out.println("Enter the day, month and year of the date of announcement of the project: "); dd = scan.nextInt(); mm = scan.nextInt(); yy = scan.nextInt(); System.out.println("Enter the no of days given to complete the project: "); nod = scan.nextInt(); int dom[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //If it is a leap year, februrary should have 29 days. if(yy%4 == 0) dom[1] = 29; //Add the days of the month for(i=0; i< mm-1; i++) tdays+=dom[i]; //Add the days of the current month tdays+=dd; //Add the number of days given to complete the project tdays+=nod; //Now convert the total calculated days to date // If the days exceed the total number of days in a year // Increment the year by one if(yy%4 == 0){ if(tdays > 366){ tdays-=366; yy++; } }else{ if(tdays > 365){ tdays-=365; yy++; } } if(yy%4 == 0) dom[1] = 29; else dom[1] = 28; m=0; while(tdays>dom[m]){ tdays-=dom[m]; m++; } System.out.println("Date of submission: "+tdays+"/"+(m+1)+"/"+yy); }//end of main }//end of class
- A number with n digits, which, when multiplied by 1, 2, 3, ..., n produces the same digits in a different order is known as a Cyclic Number. For example, 142857 is a cyclic number: 142857 x 2 = 285714; 142857 x 3 = 428571; 142857 x 4 = 571428; 142857 x 5 = 714285; 142857 x 6 = 857142, and so on. Write a program to enter a number and check if it is a cyclic number or not.
/* * Write a program to enter a number and check if it is a cyclic number or not. */ import java.io.*; class CyclicNo { //Function to arrange the digits stored in the string s in ascending order static String sort(String s) { String t=""; int i,j; char a[] = s.toCharArray(); for(i = 0; i < a.length; i++) { for(j = 0; j < a.length-i-1; j++) { if(a[j] > a[j+1]) { char r = a[j]; a[j] = a[j+1]; a[j+1] = r; } } } for(i = 0; i < a.length; i++) t = t + a[i]; return t; } public static void main(String arg[])throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String w,r,p; int i,n; boolean flag = true; System.out.println("Enter a number"); n = Integer.parseInt(br.readLine()); // Convert the integer to string String str = Integer.toString(n); // Sort the number w = sort(str); for(i = 2; i <= str.length(); i++) { // Get the multiples of the number r = Integer.toString(n*i); // Sort the multiple p = sort(r); if(p.equals(w) == false) { flag = false; break; } } if(flag) System.out.println(n + " is a cyclic number"); else System.out.println(n + " is not a cyclic number"); }//end of main }//end of class
- A number that is smaller than the sum of its aliquot parts (proper divisors) is known as an Abundant Number. Twelve is the smallest abundant number - the sum of its aliquot parts is 1 + 2 + 3 + 4 + 6 = 16 - followed by 18, 20, 24, and 30.
/* * WAP to print all the abudant numbers between m and n. * A weird number is one which is smaller than the sum of its aliquot parts. */ import java.util.*; class AbundantNo { public static void main (String args[])throws InputMismatchException { Scanner scan = new Scanner(System.in); int m,n,i,j,s; System.out.println("Enter the limits m and n: "); m = scan.nextInt(); n = scan.nextInt(); for(j=m;j<=n;j++){ s=0; for(i=1;i< j;i++){ if(j%i == 0) s=s+i; } if(s>j) System.out.print(j+" "); } }//end of main }//end of class
- A number such that both the sum of its divisors and the number of its divisors are perfect numbers is known as a Sublime Number. The smallest sublime number is 12. There are 6 divisors of 12 - 1, 2, 3, 4, 6, and 12 - the sum of which is 28. Both 6 and 28 are perfect. Write a program to check if a number is a sublime number or not.
/* * WAP to check if a number is sublime number or not. */ import java.io.*; class SublimeNo { public static void main (String args[])throws IOException { int s=0,f=0,s1=0,s2=0,n,i,j; BufferedReader obj=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter a no: "); n=Integer.parseInt(obj.readLine()); for(i=1;i<=n;i++) { if(n%i==0) { s+=i; f++; } } for(j=1;j< s;j++) { if(s%j==0) s1+=j; } for(j=1;j< f;j++) { if(f%j==0) s2+=j; } if(s1==s && s2==f) System.out.println(n+ " is a sublime number"); else System.out.println(n+ " is not a sublime number"); } }
- Find 1's complement of a number
/* * WAP to enter a negative number in base 10 and find * its 1's complement. */ import java.util.*; class OnesComplement { public static void main (String args[])throws InputMismatchException { Scanner scan = new Scanner(System.in); int n,i,p,b,x; System.out.println("Enter a number (in base 10): "); n = scan.nextInt(); if(n< 0){ int a[] = new int[100]; p = -1*n; x=0; //Convert the number to binary while(p>0){ b = p%2; a[x++] = b; p=p/2; } for(i=0; i< x; i++){ if(a[i] == 0){ a[i] = 1; }else{ a[i] = 0; } } for(i=x-1; i>=0; i--){ System.out.print(a[i]); } }else{ System.out.println("Invalid number. Please enter a negative number."); } }//end of main }//end of class
- Find 2's complement of a numbeR
/* * WAP to enter a negative number in base 10 and * find its 2's complement. */ import java.util.*; class TwosComplement { public static void main (String args[])throws InputMismatchException { Scanner scan = new Scanner(System.in); int n,i,p,b,x,carry; System.out.println("Enter a number (in base 10): "); n = scan.nextInt(); if(n<0){ int a[] = new int[100]; p = -1*n; x=0; //Convert the number to binary while(p>0){ b = p%2; a[x++] = b; p=p/2; } //Convert the array to store the 1s complement for(i=0; i< x; i++){ if(a[i] == 0){ a[i] = 1; }else{ a[i] = 0; } } System.out.print("1s complement: "); for(i=x-1; i>=0; i--){ System.out.print(a[i]); } //Add 1 to the last digit of 1s complement to get 2s complement a[0] += 1; carry = 0; for(i=0; i < x; i++){ a[x] = a[x] + carry; if(a[x] == 2){ a[x] = 0; carry = 1; } } System.out.print("\n2s complement: "); for(i=x-1; i>=0; i--){ System.out.print(a[i]); } }else{ System.out.println("Invalid number. Please enter a negative number."); } }//end of main }//end of class
- Add two binary numbers
/* * Enter two numbers in base 10, convert them in binary * and find their sum */ import java.io.*; class BinarySum { public static void main(String args[])throws IOException { int m,n,u,v,x,y,z,p,r,i,d,e; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the first number (base 10): "); m= Integer.parseInt(br.readLine()); System.out.print("Enter the second number (base 10): "); n= Integer.parseInt(br.readLine()); //Arrays to store numbers in binary form int a[]= new int [20]; int b[]=new int [20]; //Array to store the sum of binary numbers int c[]= new int[50]; p=m; x=0; //Convert the number to binary and store in an array while(p>0) { a[x++]=p%2; p=p/2; } System.out.print(m+"="); for(i=x-1;i>=0;i--) { System.out.print(a[i]); } System.out.println(); p=n; y=0; //Convert the number to binary and store in an array while(p>0) { b[y++]=p%2; p=p/2; } System.out.print(n+"="); for(i=y-1;i>=0;i--) { System.out.print(b[i]); } e=z=u=v=0; /* * To understand the following logic, you need to know binary addition. * In binary addition: * 0 + 0 = 0 * 0 + 1 = 1 * 1 + 0 = 1 * 1 + 1 = 10 (read as 0 CARRY 1) * 1 + 1 + 1 = 11 ( read as 1 CARRY 1) */ do{ d=a[u]+b[v]+e; //Add the digits if(d==2){ c[z++]=0; e=1; } else if(d==3){ c[z++]=1; e=1; } else{ c[z++]=d; e=0; } u++; v++; }while(u < x && v < y); //If any digit is left in the first array copy them // into the sum array after adding the carry if(u < x){ while(u < x){ d=a[u++]+e; if(d==2){ c[z++]=0; e=1; } else if(d==3){ c[z++]=1; e=1; } else{ c[z++]=d; e=0; } } } //If any digit is left in the second array copy them // into the sum array after adding the carry else if(v < y){ while(v < y){ d=b[v++]+e; if(d==2){ c[z++]=0; e=1; } else if(d==3){ c[z++]=1; e=1; } else{ c[z++]=d; e=0; } } } //If a carry is still left copy it at the last in the sum array if(e==1){ c[z++]=e; } System.out.print("\nSum of the two binary numbers : "); for(i=z-1;i>=0;i--) System.out.print(c[i]); }//end of main }//end of class
- Enter a sentence and print it in rotation of words for all possible combinations. Ex:
Input: java is robust Output: java is robust is robust java robust java is
/* * Enter a sentence and print it in rotation of words for all possible combinations. */ import java.io.*; class Rotation { public static void main (String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); int i,j,p,x,l; String s; char ch; System.out.println("Enter a sentence terminated by a period(.) : "); s = br.readLine(); l = s.length(); String a[] = new String[l]; x=0; p=0; //Store the words in a string array for(i=0; i< l; i++){ ch = s.charAt(i); if(ch == ' ' || ch == '.'){ a[x++] = s.substring(p,i); p = i+1; } } System.out.println("\nOUTPUT:"); //Print the sentence in rotation of words for(i=0; i< x; i++){ for(j=i; j< x; j++){ System.out.print(a[j]+ " "); } for(j=0; j< i; j++){ System.out.print(a[j]+ " "); } System.out.println(); } }//end of main }//end of class
this site is to help students to learn coding in java platform and qbasic platform. all programs are ICSE and ISC level. Java programs java python Python programs
ISC Programs_1
Subscribe to:
Posts (Atom)
Second largest number in java using ternary operator
//Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...
-
import java.util.*; class riwi { public static void main(String[]args) { Scanner sc=new Scanner(System.in); System...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
-
import java.io.*; class snowball { public static void main()throws IOException { BufferedReader br = new BufferedReader(new InputStreamRead...