Solution 2.0

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

1)

Program code:
import java.util.*;

class FutureDateCalculator2

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.print("Enter day Number:");

int d=sc.nextInt();

System.out.print("Enter year:");

int y=sc.nextInt();

System.out.print("Enter date after:");

int n=sc.nextInt();

if((d>366)||(y<1000 && y>9999)||(n<1 && n>100))

System.out.println("Invalid Input");

else

calculate(d,y);

d=d+n;

if(d>365 && y%4!=0)

d=d-365;

y++;

}
else if(d>366 && y%4==0)

d=d-366;

y++;

calculate(d,y);

public static void calculate(int x,int yr)

String
m[]={"","January","February","March","April","May","June","July","August","September","October","No
vember","December"};

int day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

if(yr%4==0)

day[2]=29;

int i=0;

while(x>day[i])

x=x-day[i];

i++;

String s="";

if(x%10 ==1)

s="st";

else if(x%10 ==2)

s="nd";

else if(x%10==3)
s="rd";

else

s="th";

System.out.println(x+s+" "+m[i]+" "+yr);

Algorithm:
1. Accept Input:
 Use a Scanner to accept input for the day number, year, and the number of days
to add (n).
 Check for invalid input conditions (day number greater than 366, year not in the
range 1000 to 9999, and n not in the range 1 to 100).
2. Calculate Future Date:
 Call the calculate function with the initial day number ( d) and year (y).
 Add the specified number of days ( n) to the day number (d).
 Adjust the day number and year if it exceeds the total number of days in a year
(considering leap years).
 Call the calculate function again with the updated day number and year.
3. Calculate Function:
 Takes the day number ( x) and year (yr) as input.
 Initializes arrays for month names ( m) and the number of days in each month
(day).
 Adjusts the number of days in February for leap years.
 Finds the month and day corresponding to the input day number.
 Prints the calculated date in the format "day month year."
Variable description:

2)Program code:
import java.util.*;

class WordFrequencyAnalyzer10

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter the String");

String str=sc.nextLine();

str=str.toUpperCase();

StringTokenizer st=new StringTokenizer(str,"!?,.' '");

int l=st.countTokens();

for(int i=0;i<l;i++)

String x=st.nextToken();

int v=0,c=0;
for(int j=0;j<x.length();j++)

char ch=x.charAt(j);

if(Character.isLetter(ch))

if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

v++;

else

c++;

else

System.out.println("Invalid Input");

return;

System.out.println("WORD\t\tCOUNT");

System.out.print(x+" ");

for(int j=1;j<=v;j++)

System.out.print("V");

System.out.println();

System.out.print(" ");

for(int j=1;j<=c;j++)

System.out.print("C");

System.out.println();

}
Algorithm:
1. Accept Input:
 Create a Scanner to read input from the user.
 Prompt the user to enter a string.
 Convert the string to uppercase to handle both uppercase and lowercase letters.
 Use StringTokenizer to tokenize the string using delimiters ('!', '?', ',', '.', ' ', and
single quotes).
2. Tokenization and Processing:
 Count the number of tokens obtained from the StringTokenizer .
 Iterate through each token:
 Initialize variables v and c to count vowels and consonants.
 Iterate through each character in the token.
 Check if the character is a letter. If not, display an "Invalid Input" message
and terminate.
 Count vowels ('A', 'E', 'I', 'O', 'U') and consonants.
 Display the word, vowel count, and consonant count in the specified
format.
Variable description:

3)
Program code:
import java.util.*;

class MatrixOperations2

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter rows and columns");

int m=sc.nextInt();

int n=sc.nextInt();

if((m<=2 || m>=10)||(n<=2 || n>=10))

{ System.out.println("Invalid Input");

System.exit(0);

int a[][]=new int[m][n];

System.out.println("Enter array elements");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

a[i][j]=sc.nextInt();

System.out.println("Original Matrix");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

System.out.print(a[i][j]+" ");
}

System.out.println();

System.out.println("Rotated Matrix");

int s=0;

for(int j=0;j<n;j++)

for(int i=m-1;i>=0;i--)

System.out.print(a[i][j]+" ");

if(a[i][j]%2!=0)

s=s+a[i][j];

System.out.println();

System.out.println("The sum of odd elements is="+s);

Algorithm:
1. Accept Input:
 Create a Scanner to read input from the user.
 Prompt the user to enter the number of rows ( m) and columns (n).
 Check for invalid input conditions (rows or columns less than or equal to 2, or
greater than or equal to 10). If invalid, display an "Invalid Input" message and
terminate the program.
 Declare a 2D array (a) of size (m x n) to store array elements.
 Prompt the user to enter array elements and populate the array.
2. Display Original Matrix:
 Display the header "Original Matrix."
 Use nested loops to display the elements of the original matrix.
3. Rotate Matrix and Sum Odd Elements:
 Display the header "Rotated Matrix."
 Use nested loops to traverse and print the rotated matrix (by columns in reverse
order).
 If an element in the rotated matrix is odd, accumulate its value in the variable s.
 Display the sum of odd elements (s).
Variable description:

You might also like