0% found this document useful (0 votes)
3 views30 pages

CTA Project

The document contains multiple Java programs that utilize switch-case statements to perform various computations, including arithmetic operations, area calculations, series summation, factorial summation, pattern printing, and root calculations. Each program prompts the user for input and executes the corresponding case based on the user's choice. The programs demonstrate fundamental programming concepts such as loops, conditionals, and method overloading.

Uploaded by

bruhanth2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views30 pages

CTA Project

The document contains multiple Java programs that utilize switch-case statements to perform various computations, including arithmetic operations, area calculations, series summation, factorial summation, pattern printing, and root calculations. Each program prompts the user for input and executes the corresponding case based on the user's choice. The programs demonstrate fundamental programming concepts such as loops, conditionals, and method overloading.

Uploaded by

bruhanth2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Question-1:

Write a program to compute using switch case:


I) Multiplication
II) Modulus
III) Division
import java.util.*;
public class comp
{
public void v()
{
Scanner obj=new Scanner(System.in); System.out.println("input
first number");
double a obj.nextDouble();
System.out.println("input second number");
double b=obj.nextDouble();
System.out.println("press 1 for multiplication");
System.out.println("press 2 for modulus");
System.out.println("press 3 for division");
int c=obj.nextInt();
switch(c)
{
case (1):
double res=a*b;
System.out.println("multiplication of two numbers are:"+res);
break;
case (2):
double result-a%b;
System.out.println("modulus of two numbers are: "+result);
break;
case (3):
double r=a/b;
System.out.println("division of two numbers are: "+r);
break;
default:
System.out.println("wrong choice:input either 1,2 or 3");
}}}
Question-2:
Write a program to compute using switch case:
I) Area of triangle
II) Area of rectangle
III) Area of square
import java.util.*;
public class Prog2
{
public void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
System.out.println("1. Square");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
int c = sc.nextInt();
switch (c)
{
case (1):
System.out.println("Enter the side length of the square:");
double Length = sc.nextDouble();
double Area1 = Length * Length;
System.out.println("The area of the square is: " + Area1);
break;
case (2): System.out.println("Enter the length of the rectangle:");
double length = sc.nextDouble();
System.out.println("Enter the width of the rectangle:");
double width = sc.nextDouble();
double Area2 = length * width;
System.out.println("The area of the rectangle is: " + Area2);
break;
case 3:
System.out.println("Enter the base of the triangle:");
double base = sc.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = sc.nextDouble();
double Area3 = (base * height) / 2;
System.out.println("The area of the triangle is: "+Area3);
break;
default:
System.out.println("Invalid choice.");
break;
}}}
Question-3:
Write a program to compute using switch case:
I) Volume of sphere
II) Volume of cube
III) Volume of cuboid
import java.util.*;
public class Program_3
{
public void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter your choice-1,2,3");
int str= in.nextInt();
switch (str)
{
case 1: //Triangle
System.out.println("Enter the base of the triangle: ");
double b=in.nextDouble();
System.out.println("Enter the height of the triangle: ");
double h=in.nextDouble();
double areatri =0.5*h*b;
System.out.println("The area of the triangle"+areatri);
break;
case 2: //rectangle
System.out.println("Enter the length of the rectangle: ");
double l=in.nextDouble();
System.out.println("Enter the width of the rectangle: ");
double w=in.nextDouble();
double arearec=l*w;
System.out.println("The area of the rectangle is"+arearec);
break;
case 3: //square
System.out.println("Enter the side of the square");
double s=in.nextDouble();
double areasq=s*s;
System.out.println("The area of the square is "+areasq);
}}}
Question-4:
Write a program to compute using switch case:
I) Sum=1+2+3.... n terms
II) Sum=2+4+6.... n terms
import java.util.Scanner;
public class SeriesSum
{
public void main()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Choose the series to compute:");
System.out.println("1. Sum = 1 + 2 + 3 + ... + n");
System.out.println("2. Sum = 2 + 4 + 6 + ... + n");
System.out.print("Enter your choice (1 or 2): ");
int choice = scanner.nextInt();
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
int sum = 0;
switch (choice)
{
case 1:
for (int i = 1; i <= n; i++)
{
sum += i;
}
System.out.println("Sum of series 1 + 2 + 3 + ... + n = " + sum);
break;
case 2:
for (int i = 2; i <= n; i += 2)
{
sum += i;
}
System.out.println("Sum of series 2 + 4 + 6 + ... + n = " + sum);
break;
default:
System.out.println("Invalid choice!");
}
scanner.close();
}}
Question-5:
Write a program to compute using switch case:
I) Sum= 1/2+1/4+1/6.... n terms
II) Sum=1/4+1/8+1/12.... n terms
import java.util.Scanner;
public class Series1
{
public void main()
{
/* Sample Input – 5
*Sample Output - 0.75*/
Scanner scanner=new Scanner(System.in);
System.out.print("Enter an integer n: ");
int n=scanner.nextInt();
if (n<=0)
{
System.out.println("Please enter an integer greater than 0.");
return;
}
double sum=0;
for (int i=2;i<=n;i=i+1)
{
switch (i%2)
{
case 0:
sum=sum+(1.0 / i);
break;
case 1: break;
default:
break;
}
}
System.out.println("The sum of the series is: "+sum);
}
public void main()
{
Scanner scanner=new Scanner(System.in);
System.out.print("Enter an integer n: ");
int n=scanner.nextInt();
if (n<=0)
{
System.out.println("Please enter an integer greater than 0.");
return;
}
double sum=0;
for (int i=4;i<=n;i=i+1)
{
switch (i%4)
{
case 0:
sum=sum+(1.0 / i);
break;
default:
break;
}}
System.out.println("The sum of the series is: "+sum);
}}
Question-6:
Write a program to compute using switch case:
I) Sum= 1!+2!+3!.... n terms
II) Sum=2!+4!+6!.... n terms
import java.util.Scanner;
public class FactorialSum
{
public void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter the value of n:");
int n = obj.nextInt();
System.out.println("Choose an option:\n1. Sum of 1! + 2! + ... +n!\
n2. Sum of 2! + 4! + ... + n!");
int choice = obj.nextInt();
int sum = 0;
switch (choice)
{
case 1:
for (int i = 1; i <= n; i++)
{
sum += factorial(i);
}
System.out.println("Sum of 1! + 2! + ... + " + n + "! = " + sum);
break;
case 2:
for (int i = 2; i <= n; i += 2)
{
sum += factorial(i);
}
System.out.println("Sum of 2! + 4! + ... + " + n + "! = " + sum);
break;
default:
System.out.println("Invalid choice.");
}
obj.close();
}
public int factorial(int num)
{
int result = 1;
for (int i = 1; i <= num; i++)
{
result *= i;
}
return result;
}}
Question 7:
Write a program to compute using switch case:
I) *
**
***

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

You might also like