0% found this document useful (0 votes)
2 views10 pages

Kedar Java

The document contains multiple Java programs that perform various tasks including arithmetic operations, calculating squares, printing welcome messages, finding the largest number, generating Fibonacci series and factorials, multiplying matrices, sorting names, checking for palindromes, reversing arrays, and counting characters, lines, and words in a text. Each program is encapsulated in a class and utilizes user input or command line arguments as necessary. The code snippets demonstrate fundamental programming concepts and Java syntax.
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)
2 views10 pages

Kedar Java

The document contains multiple Java programs that perform various tasks including arithmetic operations, calculating squares, printing welcome messages, finding the largest number, generating Fibonacci series and factorials, multiplying matrices, sorting names, checking for palindromes, reversing arrays, and counting characters, lines, and words in a text. Each program is encapsulated in a class and utilizes user input or command line arguments as necessary. The code snippets demonstrate fundamental programming concepts and Java syntax.
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

1.

Write a program to read two numbers from the user and print their addition,
subtraction, multiplication and division.

package kedar;

import [Link];

public class arithmetic {


public static void main(String args[]) {
Scanner scr = new Scanner([Link]);

[Link]("Enter the first number: ");


double num1 = [Link]();

[Link]("Enter the second number: ");


double num2 = [Link]();

[Link]("Addition: " + (num1 + num2));


[Link]("Subtraction: " + (num1 - num2));
[Link]("Multiplication: " + (num1 * num2));

if (num2 != 0) {
[Link]("Division: " + (num1 / num2));
} else {
[Link]("Division: Cannot divide by zero.");
}

[Link]();
}
}
2. Write a program to print the square of a number passed through command
line arguments.

package kedar;

public class square {


public static void main(String[] args) {
if ([Link] > 0) {
int number = [Link](args[0]);
int square = number * number;
[Link]("The square of " + number + " is: " + square);
} else {
[Link]("Please provide a number as a command line
argument.");
}
}
}
3. Write a program to send the name and surname of a student through
command line arguments and print a welcome message for the student.

package kedar;

public class studentname {


​ public static void main(String[] args) {
if ([Link] >= 2) {
String name = args[0];
String surname = args[1];
[Link]("Welcome, " + name + " " + surname );
} else {
[Link]("Please provide both name and surname as
command line arguments.");
}
}
}
4. Write a Java program to find the largest number out of n natural numbers.

package kedar;

import [Link];

class LargestOfTwoNumbers {
public static void main(String[] args) {
Scanner scr = new Scanner([Link]);

[Link]("Enter the first number: ");


int num1 = [Link]();

[Link]("Enter the second number: ");


int num2 = [Link]();

if (num1 >= num2) {


[Link]("The largest number is: " + num1);
} else {
[Link]("The largest number is: " + num2);
}

[Link]();
}
}
5. Write a Java program to find the Fibonacci series and factorial of a number
using: Recursive functions

package kedar;
import [Link];
public class factfebi {
​ static int factorial(int n) {
​ if (n <= 1) {
​ return 1;
​ }
​ return n * factorial(n - 1);
​ }
​ static int fibonacci(int n) {
​ if (n <= 1) {
​ return n;
​ }
​ return fibonacci(n - 1) + fibonacci(n - 2);
​ }
​ public static void main(String[] args) {
​ Scanner scr = new Scanner([Link]);
​ [Link]("Enter a number for Factorial: ");
​ int factNum = [Link]();
​ [Link]("Factorial of " + factNum + " is: " +
factorial(factNum));
​ [Link]("\nEnter the number of terms for Fibonacci series: ");
​ int fibTerms = [Link]();
​ [Link]("Fibonacci series: ");
​ for (int i = 0; i < fibTerms; i++) {
​ [Link](fibonacci(i) + " ");
​ }
​ [Link]();
​ [Link]();
​ }
​ }
6. Write a Java program to multiply two given matrices.

package kedar;
import [Link];
class matrix {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int[][] a = new int[3][3], b = new int[3][3], c = new int[3][3];

[Link]("Enter 9 numbers for Matrix A :");


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
a[i][j] = [Link]();

[Link]("Enter 9 numbers for Matrix B:");


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
b[i][j] = [Link]();

[Link]("Multiplication Result (3x3):");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++)
c[i][j] += a[i][k] * b[k][j];

[Link](c[i][j] + " ");


}
[Link]();
}

[Link]();
}
}
7. Write a Java program for sorting a given list of names in ascending order.

package kedar;
import [Link];
class sortname{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("How many names do you want to enter? ");


int n = [Link]();
[Link]();
String[] names = new String[n];

[Link]("Enter the names:");


for (int i = 0; i < n; i++)
names[i] = [Link]();

for (int i = 0; i < n - 1; i++) {


for (int j = i + 1; j < n; j++) {
if (names[i].compareTo(names[j]) > 0) {
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}

[Link]("\nSorted names:");
for (int i = 0; i < n; i++)
[Link](names[i]);

[Link]();
}
}
8. Write a Java program that checks whether a given string is a palindrome or
not. Example: MADAM is a palindrome.

package kedar;

import [Link];

class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");


String original = [Link]();
String reverse = "";

for (int i = [Link]() - 1; i >= 0; i--) {


reverse = reverse + [Link](i);
}

if ([Link](reverse)) {
[Link]("It is a palindrome.");
} else {
[Link]("It is not a palindrome.");
}

[Link]();
}
}
9. Write a Java program to read n number of values in an array and display
them in reverse order.

package kedar;

import [Link];

class reversearray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter number of elements: ");


int n = [Link]();
int[] arr = new int[n];

[Link]("Enter the elements:");


for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

[Link]("Array in reverse order:");


for (int i = n - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
[Link]();
}
}
10. Write a Java program that displays the number of characters, lines, and
words in a given text.

package kedar;
import [Link];
public class CharLineWords {

​ public static void main(String[] args) {


​ Scanner sc = new Scanner([Link]);

​ [Link]("Enter your text: ");
​ String text = [Link]();
​ int chars = [Link]();
​ int words = 1;
​ int lines = 1;

​ for (int i = 0; i < [Link](); i++) {


​ char ch = [Link](i);

​ if (ch == ' ' || ch == '\n') {
​ words++;
​ }
​ if (ch == '\n') {
​ lines++;
​ }
​ }

​ [Link]("Characters: " + chars);


​ [Link]("Words: " + words);
​ [Link]("Lines: " + lines);

​ [Link]();
​ }
​ }

You might also like