0% found this document useful (0 votes)
52 views5 pages

Odule Ractice Xcercises: Name:Mohamed Ashraf MA Roll No:20F131

The document contains 7 Java programs: 1) A program to convert a number to binary. 2) A program to find the biggest of four numbers. 3) A program to print numbers divisible by 3 and 7 from 1 to 200. 4) A program to check if a number is Armstrong or not. 5) A simple calculator program. 6) A program to find the maximum and minimum elements in an array. 7) A program to calculate employee gross salary using encapsulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

Odule Ractice Xcercises: Name:Mohamed Ashraf MA Roll No:20F131

The document contains 7 Java programs: 1) A program to convert a number to binary. 2) A program to find the biggest of four numbers. 3) A program to print numbers divisible by 3 and 7 from 1 to 200. 4) A program to check if a number is Armstrong or not. 5) A simple calculator program. 6) A program to find the maximum and minimum elements in an array. 7) A program to calculate employee gross salary using encapsulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MODULE 1 PRACTICE EXCERCISES

Name:Mohamed Ashraf MA

Roll No:20F131

1. Write a java program to convert a given number into a binary number

import java.util.Scanner;
public class Binary {
public static void main(String[] args){
int a;
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
String b=Integer.toBinaryString(a);
System.out.println(b);
}
}

2. Write a java program to find the biggest of four numbers

import java.util.Scanner;
public class Biggestof4number {
public static void main(String[] args){
int a,b,c,d;
Scanner sc=new Scanner(System.in);
System.out.println("Enter A value");
a=sc.nextInt();
System.out.println("Enter B value");
b= sc.nextInt();
System.out.println("Enter C value");
c=sc.nextInt();
System.out.println("Enter D value");
d= sc.nextInt();
if(a>b&&a>c&&a>d)
System.out.println(a+" is biggest number");
else
if(b>c&&b>d)
System.out.println(b+" is biggest number");
else
if(c>d)
System.out.println(c+" is biggest number");
else
System.out.println(d+" is biggest number");
}
}

3. Write a java program to print the numbers which are divisible by 7 and 3 from 1 to 200

public class DivisiblebySevenandThree{


public static void main(String[] args) {
for (int i = 1; i < 200; i++) {
if (i%3== 0 && i % 7 == 0)
System.out.print(i+" ");
}
System.out.println("\n");
}
}

4. Write a java program to find given number is Armstrong or not

import java.util.Scanner;
public class Armstrong {
public static void main(String[] args) {
int num,number, temp, total = 0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
num=sc.nextInt();
number=num;
while (number != 0)
{
temp = number % 10;
total = total + temp*temp*temp;
number /= 10;
}
if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}

5. Write a java program for simple calculations

import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
int n,a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a vaule");
a=sc.nextInt();
System.out.println("Enter b vaule");
b=sc.nextInt();
System.out.println("Enter Your option 1.add 2.sub 3.mul 4.div
5.mod");
n=sc.nextInt();
switch(n)
{
case 1:
System.out.println(a+b);
break;
case 2:
System.out.println(a-b);
break;
case 3:
System.out.println(a*b);
break;
case 4:System.out.println(a/b);
break;
case 5:System.out.println(a%b);
break;
default:
System.out.println("Enter valid option");
break;
}
}

6. Write a java program to find maximum and minimum elements in an array

import java.util.Scanner;
public class MaxandMininArray{
public int max(int [] array) {
int max = 0;

for(int i=0; i<array.length; i++ ) {


if(array[i]>max) {
max = array[i];
}
}
return max;
}
public int min(int [] array) {
int min = array[0];

for(int i=0; i<array.length; i++ ) {


if(array[i]<min) {
min = array[i];
}
}
return min;
}
public static void main(String[] args){

Scanner sc=new Scanner(System.in);


int[] a = new int[5];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<5; i++)
{
a[i]=sc.nextInt();
}
MaxandMininArray m = new MaxandMininArray();
System.out.println("Maximum value in the array is::"+m.max(a));
System.out.println("Minimum value in the array is::"+m.min(a));
}
}

7. Write a java program for calculating the gross salary of an employee ( using
encapsulation concept)

public class GrossSalary {


private String name;
private int id;
private float Bsalary;
private double da;
private double hra;
private double ma;
private double GrossSalary;
public void Setname(String name)
{
this.name=name;
}
public void Setid(int id)
{
this.id=id;
}
public void SetBsalary(float Bsalary)
{
this.Bsalary=Bsalary;
}
public void Setda(double da)
{
this.da=da;
}
public void Sethra(double hra)
{
this.hra=hra;
}
public void Setma(double ma)
{
this.ma=ma;
}

public void Gross()


{
System.out.println(name);
System.out.println(id);
da=(da*Bsalary)/100;
hra=(hra*Bsalary)/100;
GrossSalary=Bsalary+da+hra;
System.out.println("Gross Salary of Employee:"+GrossSalary);

}
public static void main(String[] args)
{
GrossSalary k=new GrossSalary();
k.Setname("Laveen");
k.Setid(111);
k.SetBsalary(10000);
k.Setda(87.9);
k.Sethra(88.5);
k.Setma(54.3);
k.Gross();

8. Write a java program to find the transpose of a matrix

public class TransporseofMatix {


static int n=4;
static void transpose(int a[][],int b[][]) {
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
b[i][j]=a[i][j];
}
}
public static void main(String[] args)
{
int a[][]= {{6,8,9,9},
{9,5,6,7},
{6,4,3,2},
{7,9,2,1}};
int b[][]=new int[n][n],i,j;
transpose(a,b);
System.out.println("Transpose matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.println(b[i][j]+" " );

}
}
}

9. Write a java program to calculate compound interest using constructor

package module1unit;

public class CompundInterest {


public CompundInterest(int i, int j, double d, int k) {
double a=i*Math.pow(1+(d/k),k*j);
double interest=a-i;
System.out.println("Compound interest after " +'\t'+ j +'\t'+
"years:"+'\t'+interest);
System.out.println("Amount after" + j +"years" +'\t'+a);
}

public static void main(String[] args)


{
CompundInterest j=new CompundInterest(5000,5,0.09,12);
}

You might also like