0% found this document useful (0 votes)
12 views9 pages

Java Fundamentals

The document contains examples of Java code demonstrating basic programming concepts like variables, data types, operators, conditional statements, loops, functions, arrays, and string manipulation. The code snippets show how to use concepts like if-else, for loops, while loops, switch statements, methods, arrays, finding sum/average/max/min of arrays, sorting arrays, and string operations in Java. Examples include programs to print strings, check if a number is even/odd/prime, find the factorial of a number, reverse an integer, and manipulate 2D arrays.

Uploaded by

group13glproject
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views9 pages

Java Fundamentals

The document contains examples of Java code demonstrating basic programming concepts like variables, data types, operators, conditional statements, loops, functions, arrays, and string manipulation. The code snippets show how to use concepts like if-else, for loops, while loops, switch statements, methods, arrays, finding sum/average/max/min of arrays, sorting arrays, and string operations in Java. Examples include programs to print strings, check if a number is even/odd/prime, find the factorial of a number, reverse an integer, and manipulate 2D arrays.

Uploaded by

group13glproject
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Language Basics

Q1 )------

public class Main{


public static void(String[] args) {
System.out.println(args[0] + " Technologies " + args[1]);
}
}

Q2)-------

public class Main{


public static void(String[] args) {
System.out.println("Welcome " + args[0]);
}
}

Q3)-------

public class Main{


public static void(String[] args) {
System.out.println("The sum of " + args[0] + " and " + args[1] + "is "
+ (Integer.parseInt(args[0]) + (Integer.parseInt(args[1])));;
}
}

Flow Control Statements

Q1)--------

a.-----

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int number = obj.nextInt();
if (number == 0) System.out.println("Zero");
else if (number < 0) System.out.println("Negative");
else System.out.println("Positive");
}
}

b.------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner (System.in);
String num1 = obj.nextLine();
String num2 = obj.nextLine();
System.out.println(num2.endsWith(num1));

}
}

Q2)-------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner (System.in);
int numb = obj.nextInt();
if (numb % 2 == 1) System.out.println("Odd");
else System.out.println("Even");
}
}

Q5)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner (System.in);
char word = obj.next().charAt(0);
if (word >= 48 && word < 58) System.out.println("Digit");
else if ((word >= 65 && word < 91) || (word >= 97 && word <123))
System.out.println("Alphabet");
else System.out.println("Special Character");
}
}

Q8)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner (System.in);
char word = obj.next().charAt(0);
String comedy = "Nev evaru";
switch(word){
case 'R':
comedy = "Red";
break;
case 'B':
comedy = "Blue";
break;
case 'G':
comedy = "Green";
break;
case 'O':
comedy = "Orange";
break;
case 'Y':
comedy = "Yellow";
break;
case 'W':
comedy = "White";
break;
default:
comedy = "Invalid Code";
}
System.out.println(comedy);
}
}

Q9)-------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
// To receive a number and print the corresponding month name
Scanner obj = new Scanner(System.in);
int numb = obj.nextInt();
String name = "";
switch(numb) {
case 1:
name = "January";
break;
case 2:
name = "Febuary";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
default:
name = "Invalid month";
break;
}
System.out.println(name);
}
}

Q10)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
// 1 to 10 numbers in a single row with one tab space
for (int x = 1; x < 11; x++) {
System.out.print(x + " ");
}
}
}

Q11)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner (System.in);
for(int x = 23; x < 57; x++){
if (x % 2 == 0) System.out.println(x);
}
}
}

Q12)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner (System.in);
int numb = obj.nextInt();
boolean flag = true;
for(int x = 2; x < numb; x++){
if (numb % x == 0){
flag = false;
break;
}
}
if (flag) {System.out.println("Prime");}
else System.out.println("Not a Prime");
}
}

Q13)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
// prime numbers between 10 and 99
boolean flag = true;
for(int x = 10; x < 99; x++){
flag = true;
for(int y = 2; y < x; y++){
if (x % y == 0) {
flag = false;
break;
}
}
if (flag) System.out.println(x);
}
}
}

Q14)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner(System.in);
int num = obj.nextInt();
int total = 0;
int rem = 0;
while(num > 0){
rem = num % 10;
total += rem;
num = num / 10;
}
System.out.println(total);
}
}

Q15)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner(System.in);
// 15 Floyds Format
int numb = obj.nextInt();
for(int x = 0; x < numb; x++){
for(int y = 0; y <= x; y++){
System.out.print("* ");
}
System.out.println("\n");
}
}
}

Q16)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner obj = new Scanner(System.in);
int num = obj.nextInt();
int reverse = 0, rem;
while (num != 0){
rem = num % 10;
reverse = reverse * 10 + rem;
num /= 10;
}
System.out.println(reverse);
}
}

Arrays

Q1)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
// sum and average of the array
int[] arr = {1, 2, 3, 4, 5, 6};
int sum = 0;
for(int i: arr){
sum += i;
}
double avg = sum / arr.length;
System.out.println(sum);
System.out.println(avg);
}
}

Q2)------

import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
// minimum and maximum of the array
int[] arr = {1, 16, 0, -4, 5, 6};
int min = arr[0];
int max = arr[0];
for(int i: arr){
if (min > i) min = i;
if (max < i) max = i;
}
System.out.println("Minimum " + min);
System.out.println("Maximum " + max);
}
}

Q3)------

import java.util.Scanner;
public class Main
{
public static void main (String[] kukaBiriyani)
{
// given number is present in the array or not
Scanner obj = new Scanner(System.in);
int[] arr = {1, 16, 0, -4, 5, 6};
int searchItem = obj.nextInt();
int present = -1;
for(int x = 0; x < arr.length; x++) {
if (arr[x] == searchItem){
present = x;
break;
}
}
System.out.println(present);
}
}

Q4)------

import java.util.Scanner;
public class Main
{
public static void main (String[] entiMawa)
{
// given number is present in the array or not
Scanner obj = new Scanner(System.in);
int[] arr = {91, 116, 90, 64, 75, 126};
for(int x = 0; x < arr.length; x++) {
System.out.print((char)arr[x] + " ");
}
}
}

Q7)-------

import java.util.Arrays;
public class Array7 {
public static void main(String[] args) {
int a[] = {12,15,22,5,1,6,1};
int m = a.length;
Arrays.sort(a);
int temp[] = new int[m];
int j = 0;
for(int i=0;i<m-1;i++) {
if(a[i]!=a[i+1]) {
temp[j++]=a[i];
}
}
temp[j++] = a[m - 1];
for (int i=0;i<temp.length-1; i++) { System.out.print(temp[i]+" ");
}
}
}

Q10)------

import java.util.Scanner;
public class Main
{
public static void main (String[] privyet)
{
// even odd
int[] evOd = {3, 3, 2};
int[] evenOdd = new int[evOd.length];
int countE = 0;
int countO = evOd.length - 1;
for(int x = 0; x < evOd.length; x++){
if (evOd[x] % 2 == 0) {
evenOdd[countE] = evOd[x];
countE++;
} else {
evenOdd[countO] = evOd[x];
countO--;
}
}
for(int i: evenOdd){
System.out.print(i+ " ");
}
}

Q14)------

import java.util.Scanner;
public class Main
{
public static void main (String[] thinavaRa)
{
// 3 * 3 array
if (thinavaRa.length < 9) System.out.println("Please enter 9 integer numbers");
else{
System.out.println("The given array is:");
int count = 0;
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
System.out.print(thinavaRa[count] + " ");
count++;
}
System.out.println("\n");
}
int max = Integer.parseInt(thinavaRa[0]);
for(int z = 0; z < thinavaRa.length; z++){
int kd = Integer.parseInt(thinavaRa[z]);
if(max < kd) max = kd;
}
System.out.println("The biggest number in the given array is " + max);
}
}
}

You might also like