0% found this document useful (0 votes)
17 views7 pages

Java Programming Basics and Examples

The document contains a collection of Java programs that demonstrate various programming concepts, including arithmetic operations, control structures, and data handling. Each program addresses a specific problem, such as calculating sums, checking conditions, and manipulating arrays. The examples serve as practical applications for beginners to learn Java programming.

Uploaded by

abrarshaik00977
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)
17 views7 pages

Java Programming Basics and Examples

The document contains a collection of Java programs that demonstrate various programming concepts, including arithmetic operations, control structures, and data handling. Each program addresses a specific problem, such as calculating sums, checking conditions, and manipulating arrays. The examples serve as practical applications for beginners to learn Java programming.

Uploaded by

abrarshaik00977
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

Q1) Sum of first n even numbers

import [Link];
class SumEven {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n=[Link](), sum=0;
for(int i=1;i<=n;i++) sum+=2*i;
[Link]("Sum="+sum);
}
}

Q2) Swap two numbers without temp


import [Link];
class Swap {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a=[Link](), b=[Link]();
a=a+b; b=a-b; a=a-b;
[Link]("a="+a+" b="+b);
}
}

Q3) Positive, Negative or Zero


import [Link];
class NumberCheck {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n=[Link]();
if(n>0) [Link]("Positive");
else if(n<0) [Link]("Negative");
else [Link]("Zero");
}
}

Q4) Even or Odd + list even numbers


import [Link];
class EvenOdd {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n=[Link]();
if(n%2==0){
[Link]("Even");
for(int i=2;i<=n;i+=2) [Link](i+" ");
} else [Link]("Odd");
}
}

Q5) Sum of even numbers 1–50


class SumEven50 {
public static void main(String[] args) {
int sum=0;
for(int i=2;i<=50;i+=2) sum+=i;
[Link]("Sum="+sum);
}
}

Q6) Multiplication table


import [Link];
class Table {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n=[Link]();
for(int i=1;i<=10;i++) [Link](n+"x"+i+"="+(n*i));
}
}

Q7) Basic Calculator


import [Link];
class Calculator {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a=[Link](), b=[Link]();
char op=[Link]().charAt(0);
switch(op){
case '+': [Link](a+b); break;
case '-': [Link](a-b); break;
case '*': [Link](a*b); break;
case '/': [Link](a/b); break;
}
}
}

Q8) Student Grade


import [Link];
class StudentGrade {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int s=[Link]();
if(s>=90) [Link]("A");
else if(s>=80) [Link]("B");
else if(s>=70) [Link]("C");
else if(s>=60) [Link]("D");
else [Link]("F");
}
}

Q9) Max of 3 numbers


import [Link];
class MaxThree {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a=[Link](), b=[Link](), c=[Link]();
if(a>=b && a>=c) [Link](a);
else if(b>=c) [Link](b);
else [Link](c);
}
}

Q10) Leap Year Check


import [Link];
class LeapYear {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int y=[Link]();
if(y%4==0){
if(y%100==0){
if(y%400==0) [Link]("Leap");
else [Link]("Not Leap");
} else [Link]("Leap");
} else [Link]("Not Leap");
}
}

Q11) Vowel or Consonant


import [Link];
class VowelCheck {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
char ch=[Link]().charAt(0);
if("aeiouAEIOU".indexOf(ch)!=-1)
[Link]("Vowel");
else [Link]("Consonant");
}
}

Q12) Month by Number


import [Link];
class Month {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int m=[Link]();
switch(m){
case 1:[Link]("Jan");break;
case 2:[Link]("Feb");break;
case 3:[Link]("Mar");break;
case 4:[Link]("Apr");break;
case 5:[Link]("May");break;
case 6:[Link]("Jun");break;
case 7:[Link]("Jul");break;
case 8:[Link]("Aug");break;
case 9:[Link]("Sep");break;
case 10:[Link]("Oct");break;
case 11:[Link]("Nov");break;
case 12:[Link]("Dec");break;
default:[Link]("Invalid");
}
}
}

Q13) Day of Week


import [Link];
class WeekDay {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int d=[Link]();
switch(d){
case 1:[Link]("Mon");break;
case 2:[Link]("Tue");break;
case 3:[Link]("Wed");break;
case 4:[Link]("Thu");break;
case 5:[Link]("Fri");break;
case 6:[Link]("Sat");break;
case 7:[Link]("Sun");break;
default:[Link]("Invalid");
}
}
}

Q14) Factorial (for loop)


import [Link];
class Factorial {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n=[Link](), f=1;
for(int i=1;i<=n;i++) f*=i;
[Link]("Factorial="+f);
}
}

Q15) Fibonacci sequence


import [Link];
class Fibonacci {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n=[Link](), a=0,b=1;
while(a<=n){
[Link](a+" ");
int c=a+b; a=b; b=c;
}
}
}

Q16) Celsius to Fahrenheit


import [Link];
class CelsiusToF {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
double c=[Link]();
double f=(c*9/5)+32;
[Link]("F="+f);
}
}

Q17) Star Pattern


import [Link];
class StarPattern {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int r=[Link]();
for(int i=1;i<=r;i++){
for(int j=1;j<=i;j++) [Link]("* ");
[Link]();
}
}
}

Q18) Prime numbers 1–100


class PrimeNumbers {
public static void main(String[] args) {
int n=2;
while(n<=100){
boolean p=true;
for(int i=2;i<=n/2;i++)
if(n%i==0){p=false;break;}
if(p) [Link](n+" ");
n++;
}
}
}

Q19) Min & Max in Array


class ArrayMinMax {
public static void main(String[] args) {
int[] arr={5,9,2,15,7};
int i=1,min=arr[0],max=arr[0];
while(i<[Link]){
if(arr[i]<min) min=arr[i];
if(arr[i]>max) max=arr[i];
i++;
}
[Link]("Min="+min+" Max="+max);
}
}

Q20) Array Length


class ArrayLength {
public static void main(String[] args) {
int[] arr={10,20,30,40,50};
[Link]("Length="+[Link]);
}
}

Q21) Reverse array of 3


class ReverseArray {
public static void main(String[] args) {
int[] arr={10,20,30};
for(int i=[Link]-1;i>=0;i--)
[Link](arr[i]);
}
}

Q22) Average of array


class ArrayAverage {
public static void main(String[] args) {
int[] arr={5,10,15,20,25,30,35,40,45,50};
int sum=0;
for(int x:arr) sum+=x;
double avg=sum/(double)[Link];
[Link]("Avg="+avg);
}
}

Q23) Student class avg & pass


class Student {
String name; int roll; double[] g;
double avg(){double s=0;for(double x:g)s+=x;return s/[Link];}
boolean pass(){return avg()>=50;}
public static void main(String[] args){
Student s=new Student();
[Link]="Ali"; [Link]=1; s.g=new double[]{60,70,80};
[Link]("Avg="+[Link]());
[Link]("Pass="+[Link]());
}
}
Q24) Library class
class Library {
String[][] books=new String[5][2];int c=0;
void addBook(String t,String a){books[c][0]=t;books[c][1]=a;c++;}
void list(){for(int i=0;i<c;i++)[Link](books[i][0]+" by "+books[i][1]);}
public static void main(String[] args){
Library l=new Library();
[Link]("Java","James"); [Link]("C++","Bjarne");
[Link]();
}
}

Q25) Student scores & grade


class StudentMarks {
int[] s;
int max(){int m=s[0];for(int x:s)if(x>m)m=x;return m;}
int min(){int m=s[0];for(int x:s)if(x<m)m=x;return m;}
char grade(){int sum=0;for(int x:s)sum+=x;
double avg=sum/(double)[Link];
if(avg>=90)return 'A';else if(avg>=80)return 'B';
else if(avg>=70)return 'C';else if(avg>=60)return 'D';else return 'F';}
public static void main(String[] args){
StudentMarks st=new StudentMarks();
st.s=new int[]{85,92,78};
[Link]("Max="+[Link]()+" Min="+[Link]()+" Grade="+[Link]());
}
}

Common questions

Powered by AI

To calculate the sum of the first 'n' even numbers, you can use a loop to iterate through the first 'n' natural numbers and sum up their double values. The loop will run from 1 to 'n', and for each 'i', 2*i is added to the sum. The formula for the sum of the first 'n' even numbers is n*(n+1). This is because the sequence of the first 'n' even numbers is 2, 4, 6, ..., 2n, which can be expressed as 2*(1+2+3+...+n) = 2n(n+1)/2 = n(n+1).

The program swaps two numbers without a temporary variable using arithmetic operations by employing the concepts of sum and difference. It assigns 'a = a + b', then 'b = a - b', which effectively assigns 'b' the original value of 'a'. Finally, it assigns 'a = a - b', which gives 'a' the original value of 'b'. This method leverages the sum to store the intermediate value necessary for swap without external storage .

The leap year determination is based on three conditions: if a year is divisible by 4, it is a leap year, unless it is divisible by 100, in which case it must also be divisible by 400 to be a leap year. The nested conditional checks use: 'if(y % 4 == 0)', 'if(y % 100 == 0)', and 'if(y % 400 == 0)' to correctly identify whether a year is a leap year or not .

The method used involves checking if the character is present in a string containing all vowels, both uppercase and lowercase ('aeiouAEIOU'). The function 'indexOf(ch) != -1' checks if the input character 'ch' is in that string. If it returns a non-negative index, it confirms the character is a vowel, otherwise, it is a consonant. This method ensures accuracy because it covers both cases: sensitive to both lowercase and uppercase vowels through a single check .

The program iteratively checks each element of the array, comparing it with the current minimum and maximum values starting with the first element. If an element is smaller than the current minimum or larger than the current maximum, the respective value is updated. This method has a time complexity of O(n), as each element is checked once. Its main limitation is inefficiency on exceptionally large arrays or when parallel processing could be used, as it does not parallelize well .

The program uses conditional logic to assign a grade based on the student’s score. It checks if the score is greater than or equal to certain thresholds (90 for 'A', 80 for 'B', and so on). By using sequential 'if-else if' statements, it organizes the conditions hierarchically, ensuring only one grade is assigned based on the highest satisfied condition .

The multiplication table is generated by a loop iterating from 1 to 10. For each iteration, the loop prints the product of the input number 'n' and the loop iterator 'i'. To extend this to a dynamic range, the loop limits can be adjusted based on additional inputs, such as a start and end range, allowing for flexible table size generation. These inputs would replace the fixed '1' and '10' limits in the for-loop .

The logic involves initializing two variables, 'a' and 'b', with 0 and 1, respectively. A loop generates the next number in the sequence by summing 'a' and 'b', printing 'a', then updating 'a' and 'b' to 'b' and 'c' respectively, where 'c = a + b'. This continues while 'a' is less than or equal to 'n'. For large 'n', this can be optimized by storing previously computed values (memoization) or using an iterative approach to fit only the required states, or by using matrix exponentiation, which reduces the time complexity from O(n) to O(log n).

The process of calculating the factorial is implemented using a loop that iterates from 1 to 'n'. For each iteration, it multiplies the current value of the iterator to an accumulator variable 'f', which initially holds the value 1. This loop results in the product of all integers from 1 to 'n', which is the definition of factorial. The computational complexity of this algorithm is O(n) because it involves a single loop that iterates 'n' times .

The conversion from Celsius to Fahrenheit uses the formula 'F = (C * 9/5) + 32'. This is implemented by reading the Celsius temperature, applying the formula, and printing the result. For reverse conversion (Fahrenheit to Celsius), the formula can be adjusted to 'C = (F - 32) * 5/9', reversing the elements of the equation to solve for the Celsius temperature from Fahrenheit .

You might also like