0% found this document useful (0 votes)
89 views

CA Programs Java

This document contains 12 code snippets showing the use of various Java programming concepts like arrays, loops, methods, conditionals, and mathematical functions. The code snippets include programs to find minimum and maximum values, square roots, factorials, sorting arrays, binary search, calculating area of shapes, checking if a number is happy or not, and simulating coin tosses with loops.

Uploaded by

Aarnav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

CA Programs Java

This document contains 12 code snippets showing the use of various Java programming concepts like arrays, loops, methods, conditionals, and mathematical functions. The code snippets include programs to find minimum and maximum values, square roots, factorials, sorting arrays, binary search, calculating area of shapes, checking if a number is happy or not, and simulating coin tosses with loops.

Uploaded by

Aarnav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

/*1 Mathematical Functions

Name: Aarnav
Unique ID: -NA-
Import java.io.*;
public class Math_functions_1
{
public static void main(String args[])
{
double m =6.25, n=8.75, p=15.625;
System.out.println("The output of different mathematical functions are:");
System.out.println("Minimum value between 6.25 and 8.75 ="
+Math.min(m,n));
System.out.println("Maximum value between 6.25 and 8.75 ="
+Math.max(m,n));
System.out.println("Square root of 6.25 =" +Math.sqrt(m));
System.out.println("Cube root of 15.625 =" +Math.cbrt(p));
System.out.println("6.25 raised to the power 3 =" +Math.pow(m,3));
System.out.println("Absolute value of (6.25-8.75) =" +Math.abs(m-n));
System.out.println("The floor value of 6.25 =" +Math.floor(m));
System.out.println("The ceil value of 6.25 =" +Math.ceil(m));
System.out.println("Integral value of 6.25 =" +Math.rint(m));
System.out.println("Rounded value of 6.25 =" +Math.round(m));
System.out.println("Exponent value of 6.25 =" +Math.exp(m));
System.out.println("Natural log of 6.25 =" +Math.log(m));
}
}
/* 2. Mathematical Function
Name: Aarnav A
Unique ID: -NA-

import java.util.*;
public class Math_functions_2
{
public static void main(String args[])
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();
System.out.print("Enter c: ");
int c = in.nextInt();
double d = Math.pow(b, 2) - (4 * a * c);
long roundedD = Math.round(d);
System.out.println("Discriminant to the nearest whole number = " +
roundedD);
}
}

OUTPUT:
/* 3 Conditional statements
Name: Aarnav
Unique id: -NA-
import java.util.Scanner;
public class ElectricBill
{
public String n;// instance variables
public int unit;
public double bill;
public void accept()
{
Scanner fg = new Scanner(System.in);
System.out.println("Please enter your name");
n = fg.next();
System.out.println("Please enter the amount of units you have consumed");
System.out.println("in numbers only.");
unit = fg.nextInt();
}
public void calculate()
{

{
if(unit>=100&&unit<200)
{
bill = unit*2.0;
}
else if(unit>=200&&unit<300)
{
bill = unit*2.0+(unit-100)*3.0;
}
else
{
bill = 100*2.0+200*3.0+(unit-300)*5.0;
double surcharge = bill*2.5/100;
bill = bill+surcharge;
}
}
}
public void print()
{
System.out.println("Name: "+n);
System.out.println("Units consumed: "+unit);
System.out.println("Bill amount: "+bill);
}
public static void main(String args[])
{
ElectricBill e = new ElectricBill();
e.accept();
e.calculate();
e.print();
}
}
OUTPUT:
/*4 Conditional statements
Name: Aarnav
Unique id: -NA-
import java.util.Scanner;
public class BankDeposit
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Term Deposit");
System.out.println("Type 2 for Recurring Deposit");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
double p = 0.0, r = 0.0, a = 0.0;
int n = 0;

switch (ch) {
case 1:
System.out.print("Enter Principal: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in years: ");
n = in.nextInt();
a = p * Math.pow(1 + r / 100, n);
System.out.println("Maturity amount = " + a);
break;
case 2:
System.out.print("Enter Monthly Installment: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in months: ");
n = in.nextInt();
a = p * n + p * ((n * (n + 1)) / 2) * (r / 100) * (1 / 12.0);
System.out.println("Maturity amount = " + a);
break;
default:
System.out.println("Invalid choice");
}
}
}

OUTPUT:
/*5 Conditional statements
Name: Aarnav
Unique id: -NA-
import java.util.Scanner;
public class Conditonal_Statements_3
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter c to calculate area of circle");
System.out.println("Enter s to calculate area of square");
System.out.println("Enter r to calculate area of rectangle");
System.out.print("Enter your choice: ");
char choice = in.next().charAt(0);
switch(choice) {
case 'c':
System.out.print("Enter radius of circle: ");
double r = in.nextDouble();
double ca = (22 / 7.0) * r * r;
System.out.println("Area of circle = " + ca);
break;
case 's':
System.out.print("Enter side of square: ");
double side = in.nextDouble();
double sa = side * side;
System.out.println("Area of square = " + sa);
break;

case 'r':
System.out.print("Enter length of rectangle: ");
double l = in.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = in.nextDouble();
double ra = l * b;
System.out.println("Area of rectangle = " + ra);
break;
default:
System.out.println("Wrong choice!");
}
}
}

OUTPUT:
/*6 Conditional statements
Name: Aarnav
Unique id: -NA-
import java.util.Scanner;
public class Conditional_Statements_4
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();
System.out.print("Enter c: ");
int c = in.nextInt();
double d = Math.pow(b, 2) - (4 * a * c);
if (d >= 0) {
System.out.println("Roots are real.");
double r1 = (-b + Math.sqrt(d)) / (2 * a);
double r2 = (-b - Math.sqrt(d)) / (2 * a);
System.out.println("Roots of the equation are:");
System.out.println("r1=" + r1 + ", r2=" + r2);
}
else {
System.out.println("Roots are imaginary.");
}
}
}
OUTPUT:
/*7 Iterative statements
Name: Aarnav
Unique id: -NA-
import java.util.Scanner;
public class Iterative_Statements_1
{
public void spyNumCheck() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = in.nextInt();
int digit, sum = 0;
int orgNum = num;
int prod = 1;
while (num > 0) {
digit = num % 10;
sum += digit;
prod *= digit;
num /= 10;
}
if (sum == prod)
System.out.println(orgNum + " is Spy Number");
else
System.out.println(orgNum + " is not Spy Number");
}
}
OUTPUT:
/*8 Iterative statements
Name: Aarnav
Unique ID: -NA-
import java.util.*;
public class Toes
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,c,h=0,t=0;
double d;
for(i=1;i<=20;i++)
{
d=Math.random()*2;
c=(int) d;
if(c==1)
h=h+1;
else
t=t+1;
}
System.out.println("Number of times Head obtained =" +h);
System.out.println("Number of times Tail obtained =" +t);
}
}
OUTPUT:
/*9 Iterative statements
Name: Aarnav
Unique ID: -NA-
import java.util.*;
class Iterative_statements_3
{
public static void main(int num)
{
int p,f=0;
p=num*num;
do
{
if(num%10!=p%10)
{
f=1;
break;
}
else
{
num=num/10;
p=p/10;
}
}
while(num>0);
if(f==0)
System.out.println("Automorphic number");
else
System.out.println("Not an Automorphic number");
}
}
OUTPUT:
/*10 Nested loops
Name: Aarnav
Unique ID: -NA-
import java.util.*;
class Happy_Number
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int num,sum=0,d;
System.out.println("Enter a number");
num = in.nextInt();
sum=num;
do
{
num=sum;sum=0;
do
{
d=num%10;
sum=sum+d*d;
num=num/10;
}
while(num>0);
}
while(sum>9);
if(sum==1)
System.out.println("Happy number");
else
System.out.println("Not a happy number");
}
}
OUTPUT:
/*11 Arrays
Name: Aarnav
Unique ID: -NA-
import java.util.*;
public class Ascending
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,t;
int m[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter numbers in the cell :");
m[i]=in.nextInt();
}
for(i=0;i<9;i++)
{
for(j=0;j<(9-i);j++)
{
if(m[j]>m[j+1])
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}
}
}
}
}

OUTPUT:
/*12 Arrays
Name: Aarnav
Unique ID: -NA-
import java.util.*;
public class B_search
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,k=0,p=0,ns,ib=0,ub=9;
int m[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("enter the numbers in ascending order:");
m[i]=in.nextInt();
}
System.out.println("Enter the number to be searched:");
ns=in.nextInt();
while (ib<=ub)
{
p=(ib+ub)/2;
if(m[p]>ns)
ub=p-1;
if(m[p]==ns)
{
k=1;
break;
}
}
}
}
OUTPUT:
/*13 Arrays
Name: Aarnav
Unique ID: -NA-
import java.util.Scanner;
public class MatrixElementsSum
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("---Initiating Input of the Elements of the 4x4 Matrix---");
int mat[][] = new int[4][4];
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
System.out.print("Enter element of Row ["+(i+1)+"], Column ["+(j+1)+"]:
");
mat[i][j] = sc.nextInt();
}
System.out.println();
}
System.out.println("\nHere is the Matrix: ");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
int rowSum[] = new int[4];
int columnSum[] = new int[4];
//Initiate Loops to calculate Sum
for(int i = 0; i < 4; i++)
{
rowSum[i] = 0;
columnSum[i] = 0;

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


{
rowSum[i] += mat[i][j];
columnSum[i] += mat[j][i];
}
}
System.out.println("\n---The Sum of Rows---");
for(int i = 0; i < 4; i++)
{
System.out.println("Sum of elements in Row ["+(i+1)+"] = "+rowSum[i]);
}
System.out.println("\n---The Sum of Columns---");
for(int i = 0; i < 4; i++)
{
System.out.println("Sum of elements in Column ["+(i+1)+"] =
"+columnSum[i]);
}
}
}
OUTPUT:
/*14 Strings
Name: Aarnav
Unique ID: -NA-4
import java.util.*;
public class Vowel
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,v=0;
String st;
char chr;
System.out.println("Enter your string :");
st=in.nextLine();
b=st.length();
for(a=0;a<b;a++)
{
chr=st.charAt(a);
if(chr=='a'||chr=='e'||chr=='i'||chr=='o'||chr=='u'||chr=='A'||chr=='E'||
chr=='I'||chr=='O'||chr=='U')
v=v+1;
System.out.println("The number of vowels in a string :"+v);
}
}
}
OUTPUT:
/*15 Strings
Name: Aarnav
Unique ID: -NA-
import java.util.*;

class Name{
public static void main(String[] args){
System.out.println("Please enter a Firstname , MiddleName & Lastname
separated by spaces");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println(name);
String[] arr = name.split(" ",3);
System.out.println(arr[0].charAt(0)+" "+arr[1].charAt(0)+" "+arr[2]);
}
}
OUTPUT:

You might also like