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

Java Practical Mcasem-II.docx

Uploaded by

yohoje3948
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)
26 views

Java Practical Mcasem-II.docx

Uploaded by

yohoje3948
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/ 13

MCA SEM-II FundamentalS of JAVA Programming (Practical)

//P1 Write a java Program to Print Hello World

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

/*P2 Write a java program to add, sub,multiply and divide two integers
numbers.*/

public class JavaProgram

public static void main(String args[])

int first=10, second=20, add, subtract, multiply;

float devide;

add = first + second;

subtract = first - second;

multiply = first * second;

devide = (float) first / second;

System.out.println("Sum = " + add);

System.out.println("Difference = " + subtract);

System.out.println("Multiplication = " + multiply);

1 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

System.out.println("Division = " + devide);

//P3 write a java Program to swap two numbers.

class Swap

public static void main(String[] args)

int x, y, t;// x and y are to swap

x=10;

y=20;

System.out.println("before swapping numbers: "+x +" "+ y);

t = x;

x = y;

y = t;

System.out.println("After swapping: "+ x + " " + y);

System.out.println( );

//P4 Write a java Program to Check the Number is Positive, Negative or Zero

2 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

public class Check

public static void main(String[] args)

int num=912;

if(num>0)

System.out.println("The number is positive.");

else if(num<0)

System.out.println("The number is negative.");

else

System.out.println("The number is zero.");

// P5 Write a java Program to check whether a given number is even or odd

public class EvenOdd {

3 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

public static void main(String[] args) {

int num=20;

if(num % 2 == 0)

System.out.println(num + " is even");

else

System.out.println(num + " is odd");

//P6 Write a java Program to find the maximum and minimum of three numbers

public class Example {

public static void main(String args[]) {

int num1 = 15;

int num2 = -5;

int num3 = 7;

if (num1 >= num2 && num1 >= num3)

System.out.println( num1 + " is the maximum number.");

else if (num2 >= num1 && num2 >= num3)

System.out.println( num2 + " is the maximum number.");

else

System.out.println( num3 + " is the maximum number.");

4 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

/*PL 7 (1.2) Write a simple java application to print a pyramid with 5 lines.

The first line has one character; the second line has two characters and so on.

The character to be used in the pyramid is taken as a command line argument.

Java code to demonstrate star patterns*/

public class Pyramid

{ public static void main(String args[])

int i, j, n=5;

for(i=0; i<n; i++)

for(j=0; j<=i; j++)

System.out.print("* ");

System.out.println();

} }

//PL8 (1.1) Write a java Program to find the sum of all integers than 100 & less
than 200 and are divisible by 5

5 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

public class SumCountDivisibleBy5

public static void main(String args[])

int i,sum=0, count=0;

for(i=101; i<200; i++)

if(i%5==0)

sum=sum+i;

count++;

System.out.println("sum of number between 100 and 200 which are


divisible by 5"+sum);

System.out.println("Total number between 100 and 200 which are


divisible by 5"+count);

// P9 Write a java Program to make Simple Calculator. (input)

import java.util.Scanner;

6 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

public class cals

public static void main(String[] args)

float a, b, res;

int choice;

Scanner scan = new Scanner(System.in);

System.out.println("1. Addition");

System.out.println("2. Subtraction");

System.out.println("3. Multiplication");

System.out.println("4. Division");

System.out.print("Enter Your Choice (1-4): ");

choice = scan.nextInt();

if(choice>=1 && choice<=4)

System.out.print("\nEnter any Two Number: ");

a = scan.nextFloat();

b = scan.nextFloat();

if(choice==1)

res = a+b;

else if(choice==2)

7 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

res = a-b;

else if(choice==3)

res = a*b;

else

res = a/b;

System.out.println("\nResult = " +res);

else

System.out.println("\nInvalid Choice!");

/*PL10 (1.3) Write a Java application which takes several command line
arguments, which are supposed to be the names of students and prints output
as given below: Number of arguments = 3 1: Tom 2: Dick 3: Harry */

class CommLine

public static void main(String a[])

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

System.out.println(str[i]+"Student Name is="+str[i]);

8 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

// P11 Write a java Program to find a Factorial of given number

class FactorialExample{

public static void main(String args[]){

int i,factorial=1;

int n = 8;

for(i=1;i<=n;i++){

factorial = factorial*i;

System.out.println(factorial+" is the factorial of: "+n);

//P12 Write a java Program to print fibonacci series

class FibonacciWithoutRecursion{

public static void main(String args[])

int number1=0, number2=1, number3, i, count=5;

System.out.print(number1+" "+number2);

9 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

for(i=2; i<count; ++i)

number3 = number1+number2;

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

number1 = number2;

number2 = number3;

//P13 Write a java Program to checked given number is a Prime number or Not

public class PrimeNumber{

public static void main(String args[])

int i,m=0,flag=0;

int n=5;//it is the number to be checked

m=n/2;

if(n==0||n==1)

System.out.println(n+" is not prime number");

else

10 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

for(i=2;i<=m;i++)

if(n%i==0)

System.out.println(n+" is not prime number");

flag=1;

break;

if(flag==0)

{ System.out.println(n+" is prime number"); }

// P14 Write a java Program to Display Array

class Testarray

public static void main(String args[])

int a[]={10,20,70,40,50};

11 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

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

System.out.println(a[i]);

// P15 write a java Program Sort the array in ascending order

public class SortAsc {

public static void main(String args[]) {

int arr[] = {5, 2, 8, 7, 1};

int temp = 0;

System.out.println("Elements of original array: ");

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

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

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

for (int j = i+1; j < arr.length; j++) {

if(arr[i] > arr[j]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

12 BY:-Prof Hina K. Patel


MCA SEM-II FundamentalS of JAVA Programming (Practical)

System.out.println();

System.out.println("Elements of array sorted in ascending order: ");

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

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

13 BY:-Prof Hina K. Patel

You might also like