JavaAssigments 0 DIVISION H 13 389 VIK
JavaAssigments 0 DIVISION H 13 389 VIK
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation: 19/02/2025
Time of Creation : 07:00 PM
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial of " + num + " is: " + fact);
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
3
LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
sc.close();
}
}
Test Case 1:
Input: 5
Expected Output: 120
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
sc.close();
}
}
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int originalNum = num;
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
if (originalNum == reversed) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
sc.close();
}
import java.util.Scanner;
public class LargestDigit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int largest = 0;
while (num != 0) {
int digit = num % 10;
if (digit > largest) {
largest = digit;
}
num /= 10;
}
System.out.println("Largest digit: " + largest);
sc.close();
}
}
Test Case 1:
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
6
LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
Input: 9876
Expected Output: 9
3. Assignments for do-while Loop
Practical No : 3.1 Menu-Driven Calculator
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation: 19/02/2025
Time of Creation : 07:40 PM
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\nMenu:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if (choice >= 1 && choice <= 4) {
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
double result = 0;
switch (choice) {
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
7
LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
case 1: result = num1 + num2; break;
case 2: result = num1 - num2; break;
case 3: result = num1 * num2; break;
case 4: result = num2 != 0 ? num1 / num2 : Double.NaN; break;
}
System.out.println("Result: " + result);
} else if (choice != 5) {
System.out.println("Invalid choice. Try again.");
}
} while (choice != 5);
System.out.println("Exiting Calculator...");
sc.close();
}
}
Test Case 1:
Input: 1, 5, 3
Expected Output: 8
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
8
LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
int sum = 0;
do {
sum += num % 10;
num /= 10;
} while (num > 0);
System.out.println("Sum of digits: " + sum);
sc.close();
}
}
Test Case 1:
Input: 123
Expected Output: 6
import java.util.Scanner;
import java.util.Random;
int guess;
do {
guess = sc.nextInt();
} else {
System.out.println("Correct Guess!");
sc.close();
int vowelCount = 0;
if ("AEIOUaeiou".indexOf(ch) != -1) {
vowelCount++;
}
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
1
2 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
}
}
Test Case 1:
Input: ["apple", "orange", "grape"]
Expected Output: 2, 3, 2
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Practical No : 6.10 Zigzag Pattern
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
import java.util.Scanner;
public class ZigzagPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
System.out.print("Enter columns: ");
int cols = sc.nextInt();
int num = 1;
for (int i = 0; i < rows; i++) {
if (i % 2 == 0) {
for (int j = 0; j < cols; j++) {
System.out.print(num++ + " ");
}
} else {
num += cols - 1;
for (int j = 0; j < cols; j++) {
System.out.print(num-- + " ");
}
num += cols + 1;
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
2
5 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
}
System.out.println();
}
sc.close();
}
}
Test Case:
Input: 5 5
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
Roll No : 34
Enrollment No :24004401110338
Author : Manohar Jangid
Date of Creation: 11/02/2025
Time of Creation :12:30 PM
2. If-Else Statement
Q4. Check if a number is even or odd
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
import java.util.Scanner;
public class EvenOddCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println("The number is Even.");
} else {
System.out.println("The number is Odd.");
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
2
9 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
}
scanner.close();
}
}
Test Cases
Test Case Input Expected Output
1 10 The number is Even.
2 7 The number is Odd.
3 0 The number is Even.
2. Nested If Statement
Q7. Find the largest of three numbers
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
import java.util.Scanner;
public class LargestOfThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
if (num1 >= num2 && num1 >= num3) {
System.out.println(num1 + " is the largest.");
}
else if (num2 >= num1 && num2 >= num3) {
System.out.println(num2 + " is the largest.");
}
else {
System.out.println(num3 + " is the largest.");
}
scanner.close();
}
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
3
2 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
}
Test Cases
Test Case Input Expected Output
1 10, 20, 30 30 is the largest.
2 50, 25, 40 50 is the largest.
3 30, 30, 30 30 is the largest.
4. If-Else-If Ladder
Q10. Assign grades based on marks
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
import java.util.Scanner;
public class GradeAssignment {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter marks: ");
int marks = scanner.nextInt();
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
scanner.close();
}
}
Test Cases
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
3
5 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
Test Case Input Expected Output
1 92 Grade: A+
2 85 Grade: A
3 45 Grade: F
5. Switch Statement
Q13. Create a calculator program using a switch statement
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter operation (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
System.out.println("Result: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + result);
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
3
8 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
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("Cannot divide by zero.");
}
break;
default:
System.out.println("Invalid operation.");
}
scanner.close();
}
}
Test Cases
Test Case Input Expected Output
1 5, 3, + Result: 8.0
2 10, 5, / Result: 2.0
3 4, 0, / Cannot divide by zero.
Q14. Display the name of the day of the week based on a number
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
6. Ternary Operator
Q16. Find the smallest of two numbers using the ternary operator
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Q28. Determine if a given year is a leap year using nested if and switch
statements
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
import java.util.Scanner;
public class LeapYearCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
if (year % 4 == 0) {
if (year % 100 == 0) {
switch (year % 400) {
case 0:
System.out.println(year + " is a Leap Year");
break;
default:
System.out.println(year + " is not a Leap Year");
}
} else {
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
5
4 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
System.out.println(year + " is a Leap Year");
}
} else {
System.out.println(year + " is not a Leap Year");
}
scanner.close();
}
}
Test Cases
Test Case Input Expected Output
1 2020 2020 is a Leap Year
2 1900 1900 is not a Leap Year
3 2000 2000 is a Leap Year
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
2. Reverse an Array
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
import java.util.Arrays;
public class ReverseArray {
public static void main(String[] args) {
MCA- Noon Batch : DIVISION :-- H
Roll No :-- 07
Enrollment No :-- 24004401110338
Name of the students :--Shruti Surve
6
0 LOK JAGRUTI KENDRA UNIVERSITY
School of Computer Application
Master of computer Application
Academic Year 2024-2025
Evening Batch – Division H
Assignments 0 : Basic Datatypes Control Statements Array and String
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
System.out.println("Reversed Array: " + Arrays.toString(arr));
}
}
Testcase:
Input: {1, 2, 3, 4, 5}
Output: Reversed Array: [5, 4, 3, 2, 1]
Output :
Difference Matrix:
44
44
18. Transpose of a Matrix
Implementation :
This program demonstrates input validation, code modularity,
and testing by dividing the logic into multiple methods.
Problem: A program to print even number.
Roll No : 5
Enrolment Nos
:24004401110338
Author : Shruti Surve
Date of Creation:19/02/2025
Time of Creation : 07:20 PM
public class TransposeMatrix {
public static void main(String[] args) {
int[][] mat = {{1, 2, 3}, {4, 5, 6}};
int rows = mat.length, cols = mat[0].length;
int[][] transposed = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = mat[i][j];
}
}
System.out.println("Transpose:");