CTA Project
CTA Project
II) ***
**
*
import java.io.*;
public class Program37 {
public void main() throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Press 1 - To print pattern 1");
System.out.println("Press 2 - To print pattern 2");
int s = Integer.parseInt(br.readLine());
switch (s)
{
case 1:
// Printing pattern 1
System.out.print("Enter the number of rows for the pattern:
");
int rows = 3;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
break;
case 2:
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(" ");
}
for (int j = 0; j < 3 - i; j++)
{
System.out.print("*");
}
System.out.println();
}
break;
default:
System.out.println("Invalid input! Please enter 1 or 2.");
}
br.close();
}}
Question-8:
Write a program to compute using switch case:
I) square root
II) cube root
import java.util.*;
public class Program_8
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("INPUT NUMBER");
System.out.println("1 FOR SQUARE ROOT ");
System.out.println("2 FOR CUBE ROOT");
int s = in.nextInt();
switch(s)
{
case 1:
System.out.println("number equals");
double r=in.nextDouble();
double sr=Math.sqrt(r);
System.out.println("square root is"+" "+sr);
break;
case 2:
System.out.println("number equals");
double c=in.nextDouble();
double cr=Math.cbrt(c);
System.out.println("cube root is"+" "+cr);
break;
default:
System.out.println("WRONG CHOICE");
}}}
Question-10:
Write a program to compute using switch case:
Displays days of a week
import java.util.Scanner;
public class days
{
public void main(String[] days)
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter a number (1,7) to get the corresponding
day of the week");
int day obj.nextInt();
switch(day)
{
case 1:
System.out.println("monday");
break;
case 2:
System.out.println("tuesday");
break;
case 3:
System.out.println("wednesday");
break;
case 4:
System.out.println("thursday");
break;
case 5:
System.out.println("friday");
break;
case 6:
System.out.println("saturday");
break;
case 7:
System.out.println("sunday");
break;
default:
System.out.println("invalid input");
}}}
Question 11:
Write a program to design a class of overloading using user’s
choice:
I) void Polygon(int n,char ch) –takes 1 integer and 1 character
input to print a filled square with sides n using ch.
II) void Polygon(int x, int y) – takes 2 integer data types to print
a filled rectangle with l=x, b=y using ‘@’ symbol.
III)void Polygon() -
*
**
***
public class Program_11
{
public void Polygon(int n, char ch)
{
if (n <= 0)
{
System.out.println("Size must be a positive integer.");
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(ch+" ");
}
System.out.println();
}
}
public void Polygon(int x,int y)
{
if (x<= 0&& y<=0)
{
System.out.println("Size must be a positive integer.");
}
for (int i= 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
System.out.print("@ ");
}
System.out.println();
}
}
public void Polygon()
{
for(int i= 1;i<=3;i=i+1)
{
for(int j=1;j<=i;j=j+1)
{
System.out.print("*");
}
System.out.println();
}
}
public void main()
{
Program_11 obj = new Program_11();
obj.Polygon(5,'*');
obj.Polygon(4,7);
obj.Polygon();
}}
Question-12:
Write a program in java to read four function calculator. The
program requires the user to enter two numbers and two
operators as arguments, which then carries out specific arithmetic
operators of two numbers and finally displays the result.
import java.util.*;
public class program12
{
public void main()
{
Scanner in= new Scanner(System.in) ;
// Input First Number
System.out.println("Enter the First number :");
double num1=in.nextDouble();
// Input Second number
System.out.println("Enter the Second number :");
double num2=in.nextDouble();
// Input operator
System.out.println("Enter the Operator +,-,*,/");
char operator=in.next().charAt(0);
double result;
// Perform the Specified operation
switch(operator)
{
case '+':
result=num1+num2;
System.out.println("Result : " +result);
break;
case '-':
result=num1-num2;
System.out.println("Result :"+result);
break;
case '*':
result=num1*num2;
System.out.println("Result :"+result);
break;
case '/':
if (num2!=0)
{
result=num1/num2;
System.out.println("Result :"+result);
}
else
{
System.out.println("Error: Cannot be divided by zero");
}
break;
default:
System.out.println("Error: Invalid Operator , please choose
+,-,*,/");
}}}
Question-13:
Write a program in java to display the character of the year.
public class MonthDays
{
public void main(int year, int month)
{
if (month < 1 || month > 12)
{
System.out.println("Invalid month. Please enter a value lbetween
1 and 12.");
}
else
{
int days = getDaysInMonth(year, month);
String monthName = getMonthName(month);
System.out.println(monthName + " " + year + " has " + days + "
days.");
}}
public int getDaysInMonth(int year, int month)
{
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days = daysInMonth[month - 1];
if (month == 2 && isLeapYear(year))
{
days = 29;
}
return days;
}
public boolean isLeapYear(int year)
{
boolean leap = false;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { leap =
true; } ;
return leap;
}
public String getMonthName(int month)
{
String[] months = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December"};
String monthName = "";
for (int i = 0; i < months.length; i++)
{
if (i == month - 1)
{
monthName = months[i];
break;
}}
return monthName;
}}