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

logic based java with star pattern_NIFSD03

The document contains a series of Java programming exercises ranging from basic to advanced levels. Each program addresses a specific problem, such as checking for even or odd numbers, calculating sums of digits, and validating email formats. The exercises are designed to enhance programming skills and understanding of Java concepts.
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)
3 views

logic based java with star pattern_NIFSD03

The document contains a series of Java programming exercises ranging from basic to advanced levels. Each program addresses a specific problem, such as checking for even or odd numbers, calculating sums of digits, and validating email formats. The exercises are designed to enhance programming skills and understanding of Java concepts.
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/ 34

JAVA Logic based Programs

Starts from very basic to high level

Pro1:-write program to read three int values and perform addition operation in java?
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {

Scanner obj = new Scanner(System.in);


System.out.println("enter first no");
int a = obj.nextInt();
System.out.println("enter second no");
int b = obj.nextInt();
System.out.println("enter third no");
int c = obj.nextInt();
int sum = a + b + c;
System.out.println(sum);

Pro2:-write program to check whether the given number is even or


odd number?
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {

Scanner obj = new Scanner(System.in);


System.out.println("enter number");
int n = obj.nextInt();
if (n >= 0) {
if (n % 2 == 0)
System.out.println("even");
else
System.out.println("odd");
} else
System.out.println("invalid");

obj.close();
}

Pro3:-Given an integer n , perform the following conditional actions ,if number is odd,print
bad number ,if number is even and in the range of 2 to 5 ,print good number,if number is
even and in the range of 6 to 20 ,print bad number .if number is even and greater then
20,print bad number?

contraint-> 1<=n<=100
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {

Scanner obj = new Scanner(System.in);


System.out.println("enter number");
int n=obj.nextInt();
if(n>=1 && n<=100){
if(n%2!=0)
System.out.println("bad number");
else{
if(n>=2 && n<=5)
System.out.println("good number");
else if(n>=6 && n<=20)
System.out.println("bad number");
else
System.out.println("good number");
}
}

}
}
Pro4:- write program To check whether the given number is leap year or not ?

if we take any year it is said to be leap year if it follows the following conditions
1. if it is not a century year and divisible by 4
2. if it is a century year and divisible by 400
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {

Scanner obj = new Scanner(System.in);


System.out.println("enter year");
int year = obj.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println("leap year");
} else {
System.out.println("not leap year");
}
}

Pro5:- The e-commerce company Bookshelf wishes to analyse its monthly sales data
between minimum range 30 to max range 100. The company has categorized these book
sales into four groups depending on the number of sales with the help of these groups the
company will know which stock they should increase or decrease in their inventory for the
next month.
sales range groups

30-50 ------------------> D
51-60 ------------------> C
61-80 ------------------> B
81-100 -----------------> A

constraint---> 30<=saleCount<=100
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {

Scanner obj = new Scanner(System.in);


System.out.println("enter here");
int n=obj.nextInt();
if(n>=30 && n<=100){
if(n>=30 && n<=50)
System.out.println("D");
else if(n>=51 && n<=60)
System.out.println("C");
else if(n>=61 && n<=80)
System.out.println("B");
else
System.out.println("A");
}
else
System.out.println("invalid");
}

Pro6:-write a program that takes a number as an argument ,increments the number by +1


and return the result.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {

Scanner obj = new Scanner(System.in);


System.out.println("enter number here");
int n=obj.nextInt();
System.out.println(++n);
}
}

Pro7:-write a program to extract digits from the number.


import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt();
while(n!=0){
System.out.println((n%10)+" ");
n=n/10;
}
}

}
Pro8:- write a program to calculate sum of digits present in the given
number.

import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt();
int s=0,d;
while(n!=0){
d=n%10;
s=s+d;
n=n/10;
}
System.out.println(s);
}
}

Pro9:-write a program to calculate sum of even digits present in


the given number.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt();
int d,s=0;
while(n!=0){
d=n%10;
if(d%2==0)
s=s+d;
n=n/10;
}
System.out.println(s);

}
}

Pro10:-write a program to calculate sum of odd digits present in


the given number.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt();
int d,s=0;
while(n!=0){
d=n%10;
if(d%2!=0)
s=s+d;
n=n/10;
}
System.out.println(s);
}
}
Pro11:-write a program to calculate sum of prime digits present in
the given number.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt();
int d,s=0;
while(n!=0){
d=n%10;
if(d==2||d==3||d==5||d==7)
s=s+d;
n=n/10;
}
System.out.println(s);
}
}

Pro12:- write a program to calculate sum of digits that are


divisible by 3 in the given number.

import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt();
int d,s=0;
while(n!=0){
d=n%10;
if(d%3==0)
s=s+d;
n=n/10;
}
System.out.println(s);
}
}

Pro13:-write a program to check number of digits in the given


number.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n = obj.nextInt();
int c = 0;
while (n != 0) {
c++;
n = n / 10;
}
System.out.println(c);
}
}

Pro14:-write a program to reverse the given number


import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt(),d,r=0;
while(n!=0){
d=n%10;
r=r*10+d;
n=n/10;
}
System.out.println(r);
}
}

Pro15:-write a program to find number of occurrences of the given


digit in the number .
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n = obj.nextInt();
System.out.println("enter digit to check ");

int key = obj.nextInt();


int c = 0, d;
while (n != 0) {
d = n % 10;
if (d == key) {
c++;
}
n = n / 10;
}
System.out.println(c);
}
}

Pro16:-write a program to check whether the given number is


paliandrome or not
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt(),r,temp,d;
r=0;
temp=n;
while(n!=0){
d=n%10;
r=r*10+d;
n=n/10;
}
System.out.println((temp==r)?"Yes":"No");
}
}

Pro17:- Given a decimal number, write a program to convert the number into a binary
form.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter no");
System.out.println(Integer.toBinaryString(obj.nextInt()));
}
}
Pro18:- Write a program to accept a number and check and display whether it is a Niven
Number or not.
Niven Number is that a number which is divisible by its sum of digits.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter no");
int n=obj.nextInt();
int temp,s,d;
temp=n;
s=0;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
System.out.println((temp%s==0)?"Yes":"No");
}
}
Pro19:- A special two digit number is a number such that when the sum of its digits is
added to the product of its digits, the result should be equal to the original two-digit
number.

Implement a program to accept a two digit number and check whether it is a special two
digit number or not.
input -----> a two digit number
constraint-> 10<=n<=99
output ----> special two digit number or not
ex:- 59 ====> (5+9)+(5*9) =14+45=59
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n=obj.nextInt(),a,b,c;
a=n%10;
b=(n/10)%10;
c=(a+b)+(a*b);
System.out.println((c==n)?"Yes":"No");
}
}
Pro20:-write a program to find sum of even number between x and
y both are inclusive
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter start number");
int x = obj.nextInt();
System.out.println("enter end number");
int y = obj.nextInt();
int i, s;
s = 0;
for (i = x; i <= y; i++) {
if (i % 2 == 0)
s = s + i;
}
System.out.println(s);
}
}
Pro21:-write a program to convert Celsius to Fahrenheit.
formula: F=(C*9/5)+32
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter temp ");
System.out.println((obj.nextInt()*9/5)+32);
}
}
Pro22:-write a program to check whether the given number is prime number
or not.
A number is said to prime if it is having only two factors. i.e. 1 and number itself.

import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter no");
int n=obj.nextInt();
int i,factor=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
factor++;
}
System.out.println(factor==2);
}
}

Pro23:-given a string determine if it is palindrome string or


not.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine();
StringBuffer sb = new StringBuffer(s);
sb.reverse();
System.out.println((s.equals(sb.toString()))?"yes it is palindrome":"not
palindrome");
}
}
Pro24:- Program to count number of special characters and white spaces in a given string.
import java.util.Scanner;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine();
int i,c=0;
for(i=0;i<s.length();i++)
{
char ch = s.charAt(i);
if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')||(ch>='0' && ch<='9'))
continue;
else
c++;
}
System.out.println("total no of spaces and special char "+c);
}
}
Pro25:-write a program to check valid email .email name should be
starts with alphabet and should follow by number or underscore. It
should contains either number or underscore finally ends with
@gmail.com only.
Ex:[email protected]
Ex:[email protected]
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter email");
String s = obj.nextLine();
Pattern p = Pattern.compile("[a-z]+[0-9|_]@gmail[.]com");
Matcher m = p.matcher(s);
if(m.find()&& (m.group()).equals(s))
System.out.println("true");
else
System.out.println("false");
}
}
Pro26:-write a program to return first capital letter in a string.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
String s = obj.nextLine();
for(int i=0;i<s.length();i++)
{
if(Character.isUpperCase(s.charAt(i)))
{
System.out.println(s.charAt(i));
break;
}
}
}
}
Pro27:-write a program to test whether or not an integer number is a perfect
number or not?
A perfect number is a number that can be written as sum of its factors.
Excluding the number itself.
Ex:- 6 ===> 1,2,3,6 => 1,2,3 => 1+2+3=6
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter no");
int n=obj.nextInt(),s=0,i;
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
System.out.println(s==n);
}
}
Pro28:-write a program to determines whether a number is Oddish or Evenish.
Oddish:-a number is said to be oddish if the sum of all its digits is odd.
Evenish:-a number is said to be evenish if the sum of its digits is even
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter number");
int n = obj.nextInt(), s = 0;
while (n != 0) {
s = s + (n % 10);
n = n / 10;
}
System.out.println((s % 2 == 0) ? "Evenish" : "Oddish");
}
}
Pro29:- write a program that takes a string and return the word count ,try to give big
sentence as a input.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter text here");
String[] s = obj.nextLine().split(" ");
System.out.println("Total word count: "+s.length);
}
}
Pro30:-a word has been split into a left part and right part then re –generate
the word by adding both halves together changing the first to an uppercase
letter.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s1 = obj.nextLine();
String s2 = obj.nextLine();
System.out.println(s1.substring(0,1).toUpperCase()+s1.substring(1)+s2);
}
}
Pro31:write a program to check strings are anagrams or not.
Two strings a and b are called anagrams ,if they contains all the same character
in the same frequencies.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter first string");
char ch1[] = obj.nextLine().toCharArray();
System.out.println("enter second string");
char ch2[] = obj.nextLine().toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
System.out.println(Arrays.equals(ch1,ch2));
}
}
Pro32:-write a program that finds the word “bomb” in the given chat msg .
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine().toLowerCase();
System.out.println((s.contains("bomb"))?"attention!!!":"Relax, everything is
ok.");
}
}

Pro33:-write a program that takes a string and return the number of


vowels contained within it.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter word here");
String s = obj.nextLine();
int i=0,c=0;
for(i=0;i<s.length();i++)
{

if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)==
'u')
c++;
}
System.out.println("this word contains "+c+" vowels");
}
}
Pro34:-write a program that takes a word and return true if the
word has two consecutive identical letters.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine();
int i;
boolean found=false;
for(i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
{
found=true;
break;
}
}
System.out.println(found);
}
}
Pro35:-write a program which takes string as a input and remove all
vowels present in it and return new string.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
System.out.println(obj.nextLine().replaceAll("[aeiou]",""));
}
}

Pro36:-write a program that takes a string and return the middle


character . if word length is odd return middle character , and
if word length is even return middle two character.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter word");
String s = obj.nextLine();
int n = s.length();
if(n%2==0)
System.out.print(s.charAt(n/2-1)+""+s.charAt(n/2));
else
System.out.println(s.charAt(n/2));
}
}
Pro37:-write a program that return index of first vowel in a
string.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter value");
String s = obj.nextLine();
for(int i=0;i<s.length();i++)
{

if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)==
'u')
{
System.out.println(i);
break;
}
}
}
}
Pro38:-write a program that find longest word in a sentence .if two
or more words are found ,return the first longest word.
import java.util.StringTokenizer;

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine();
StringTokenizer st = new StringTokenizer(s);
int m=0;
String res="";
while(st.hasMoreTokens())
{
String token = st.nextToken();
if(token.length()>m)
{
m=token.length();
res=token;
}
}
System.out.println(res);
}
}
Pro39:-write a program which remove all the duplicates in the given
string.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine();
String rs="";
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
if(rs.indexOf(ch)<0)
rs=rs+ch;
}
System.out.println(rs);
}
}
Pro40:-write a program that return number of consonants present in
the given string.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine();
int i,c=0;
for(i=0;i<s.length();i++){

if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)==
'u')
continue;
else
c++;
}
System.out.println(c);
}
}

Pro41:-write a program to check if a string contains only digits


public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s = obj.nextLine();
int i,c=0;
for(i=0;i<s.length();i++){
if(s.charAt(i)>='0' && s.charAt(i)<='9')
c++;
}
System.out.println((c==s.length())?"Yes":"No");
}
}

Pro42:-write a program to capitalize first letter of each word in a


string.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter here");
String s[] = obj.nextLine().split(" ");
for(String ss:s){
System.out.print(ss.substring(0,1).toUpperCase()+ss.substring(1)+" ");
}
}
}

Pro43:-write a basic program to read an array elements and write


on the screen.take array elements from user.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n = obj.nextInt();
int i;
int a[] = new int[n];
for (i = 0; i < n; i++)
{
System.out.println("enter array value at index "+i);
a[i] = obj.nextInt();
}
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
}

Pro44:-write a program to read an array elements and prints sum of all its
elements.

public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n = obj.nextInt();
int i,s=0;
int a[] = new int[n];
for (i = 0; i < n; i++)
{
System.out.println("enter array value at index "+i);
a[i] = obj.nextInt();
}
for(i=0;i<n;i++)
{
s=s+a[i];
}
System.out.print(s);
}
}
Pro45:-write a program to read an array elements and print sum of
all even elements.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n = obj.nextInt();
int i,s=0;
int a[] = new int[n];
for (i = 0; i < n; i++)
{
System.out.println("enter array value at index "+i);
a[i] = obj.nextInt();
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
s=s+a[i];
}
System.out.print(s);
}
}
Pro46:-write a program to read an array elements and print sum of
all odd elements.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n = obj.nextInt();
int i,s=0;
int a[] = new int[n];
for (i = 0; i < n; i++)
{
System.out.println("enter array value at index "+i);
a[i] = obj.nextInt();
}
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
s=s+a[i];
}
System.out.print(s);
}
}
Pro47:-write a program to read an array elements and print sum of
all prime elements.
public class Test {

static boolean isprime(int n){


int i,f=0;
for(i=1;i<=n;i++){
if(n%i==0)
f++;
}
return f==2;
}

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n = obj.nextInt();
int i,s=0;
int a[] = new int[n];
for (i = 0; i < n; i++)
{
System.out.println("enter array value at index "+i);
a[i] = obj.nextInt();
}
for(i=0;i<n;i++)
{
if(isprime(a[i]))
s=s+a[i];
}
System.out.print(s);
}
}

Pro48:-write a program to read an array elements and print sum of all


palindrome number in array.

public class Test {

static boolean ispali(int n) {


int r = 0, t = n, d;
while (n != 0) {
d = n % 10;
r = r * 10 + d;
n = n / 10;
}
return t == r;
}

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n = obj.nextInt();
int i, s = 0;
int a[] = new int[n];
for (i = 0; i < n; i++) {
System.out.println("enter array value at index " + i);
a[i] = obj.nextInt();
}
for (i = 0; i < n; i++) {
if(ispali(a[i]))
s=s+a[i];
}
System.out.print(s);
}
}
Pro49:-write a program to read an array elements and print sum of
elements ending with 3 in array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n = obj.nextInt();
int i, s = 0;
int a[] = new int[n];
for (i = 0; i < n; i++) {
System.out.println("enter array value at index " + i);
a[i] = obj.nextInt();
}
for (i = 0; i < n; i++) {
if(a[i]%10==3)
s=s+a[i];
}
System.out.print(s);
}
}
Pro50:-write a program to search for an elements in an array
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),index=-1,i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter array element at index "+i);
a[i]=obj.nextInt();
}
System.out.println("enter element need to find");
int key=obj.nextInt();
for(i=0;i<n;i++)
{
if(key==a[i]){
index=i;
break;
}
}
System.out.println("present at index "+index);
}
}
Pro51:-write a program to sort the given array elements in ASC
order.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter array element at index "+i);
a[i]=obj.nextInt();
}
Arrays.sort(a);
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
->you can also perform this using sorting technique.

Pro52:-write a program to read an array elements and find the max


elements in array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter array element at index "+i);
a[i]=obj.nextInt();
}
Arrays.sort(a);
System.out.print(a[n-1]);
}
}
Pro53:-write a program to find difference between largest and
smallest element in array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter array element at index "+i);
a[i]=obj.nextInt();
}
Arrays.sort(a);
System.out.print(a[n-1]-a[1-1]);
}
}
Pro54:-write a program to read array elements and find second max
element in array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter element at index "+i);
a[i]=obj.nextInt();
}
Arrays.sort(a);
System.out.print(a[n-2]);
}
}
Pro55:-write a program to find the number of occurrences of the
given element.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int i,key,n=obj.nextInt(),count;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter element at index "+i);
a[i]=obj.nextInt();
}
System.out.println("show occurance of element ");
key=obj.nextInt();
count=0;
for(i=0;i<n;i++){
if(key==a[i])
count++;
}
System.out.println(count);
}

}
Pro56:-write a program to update an element in the given array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i,oldelement,newelement;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter element at index "+i);
a[i]=obj.nextInt();
}
System.out.println("enter old element");
oldelement=obj.nextInt();
System.out.println("enter new element");
newelement=obj.nextInt();
for(i=0;i<n;i++)
{
if(a[i]==oldelement)
a[i]=newelement;
}
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}

Pro57:-write a program to reverse the elements present in an array


public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter element at index "+i);
a[i]=obj.nextInt();
}
for(i=n-1;i>=0;i--)
System.out.print(a[i]+" ");
}

Pro58:-write a program to increment every element by one unit in


array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter element at index "+i);
a[i]=obj.nextInt();
}
for(i=0;i<n;i++)
System.out.print((a[i]+1)+" ");
}

}
Pro59:-write a program to find the number of duplicate elements present in the
given array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i,c=0,a[]=new int[n];
int b[]=new int[999];
for(i=0;i<n;i++)
{
System.out.println("enter element at index "+i);
a[i]=obj.nextInt();
}
for(i=0;i<n;i++)
b[a[i]]++;
for(i=0;i<999;i++)
{
if(b[i]>=2)
c++;
}
System.out.println(c);
}

}
Pro60:-write a program to find the sum of two arrays and display the result array
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array sizes");
int i,n=obj.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter array elements");
a[i]=obj.nextInt();
}
for(i=0;i<n;i++)
{
System.out.println("enter array elements");
b[i]=obj.nextInt();
}
for(i=0;i<n;i++)
System.out.print((a[i]+b[i])+" ");
}

}
Pro61:-write a program to print reverse of each element in an
array.
public class Test {
static int rev(int n)
{
int r=0,d;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i,a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter element at index "+i);
a[i]=obj.nextInt();
}
for(i=0;i<n;i++)
System.out.print(rev(a[i])+" ");

}
Pro62:-write a program to sort only first half of the arrays
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter the array size");
int n = obj.nextInt(), i;
int a[] = new int[n];
for (i = 0; i < n; i++) {
System.out.println("enter the element at index " + i);
a[i] = obj.nextInt();
}
Arrays.sort(a, 0, n / 2);
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}

}
Pro63:-write a program to find the difference between two arrays
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array sizes");
int i,n=obj.nextInt();

int a[]=new int[n];


int b[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter 1st array elements");
a[i]=obj.nextInt();
}
for(i=0;i<n;i++)
{
System.out.println("enter 2nd array elements");
b[i]=obj.nextInt();
}

for(i=0;i<n;i++)
System.out.print((a[i]-b[i])+" ");

}
Pro64:-write a program to eliminates odd numbers within array.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt();
int i;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter element in array ");
a[i]=obj.nextInt();
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
System.out.print(a[i]+" ");
}

}
Pro65:-write a program to check whether an array is paliandrome or
not.
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),i,low,high,flag=1;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter array elements");
a[i]=obj.nextInt();
}
low=0;
high=n-1;
while(low<=high){
if(a[low]!=a[high]){
flag=0;
break;
}
low++;
high--;
}
System.out.println(flag==1);
}

Pro66:-write a program to convert an array into matrix


public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);
System.out.println("enter array size");
int n=obj.nextInt(),m=(int)Math.sqrt(n),i,j,k=0;
int a[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("enter array element");
a[i]=obj.nextInt();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[k++]+" ");
}
System.out.println();
}

}
}
Pro67:-write a program to find sum of diagonal elements in matrix
public class Test {

public static void main(String args[]) throws Exception {


Scanner obj = new Scanner(System.in);

int a[][] = new int[3][3];


int i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=obj.nextInt();
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
s=s+a[i][j];
}
}
System.out.println(s);

}
}

Some pattern programs

Pro68:-write a java program to display the following


patterns?

public class Test {

public static void main(String args[]) throws Exception {


for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print("*" + " ");
}
System.out.println();
}

}
}

Pro67:-

public class Test {

public static void main(String args[]) throws Exception {


for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(j + " ");
}
System.out.println();
}

}
}

Pro68:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
System.out.print(9-j+" ");
}
System.out.println();
}
}
}

Pro69:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}

Pro70:

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
System.out.print(9-i+" ");
}
System.out.println();
}
}
}

Pro71:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
System.out.print((char)(65+j)+" ");
}
System.out.println();
}
}
}

Pro72:-
public class Test {

public static void main(String args[]) throws Exception {


{for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
System.out.print((char)(74-j)+" ");
}
System.out.println();
}
}
}
}

Pro73:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
System.out.print((char)(65+i)+" ");
}
System.out.println();
}
}
}

Pro74:-
public class Test {

public static void main(String args[]) throws Exception {


for (int i = 0; i < 10; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*" + " ");
}
System.out.println();
}
}
}

Pro78:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < (9-i); j++)
{
System.out.print(" ");
}
for(int k = 0; k <= i; k++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}

Pro79:-
public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < i; j++)
{
System.out.print(" "+" ");
}
for(int k = 0; k < (10-i); k++)
{System.out.print("*"+" ");
}
System.out.println();
}
}
}
Pro80:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < (10-i); j++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}

Pro 81:-
public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < (9-i); j++)
{
System.out.print(" ");
}
for(int k = 0; k <= i; k++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}

Pro82:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 10; i++)
{
for(int j = 0; j < i; j++)
{
System.out.print(" ");
}
for(int k = 0; k < (10-i); k++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}
Pro83:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 5; i++)
{
for(int j = 0; j < i; j++)
{
System.out.print(" ");
}
for(int k = 0; k < 10-2*i-1; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Pro:84

public class Test {


public static void main(String args[]) throws Exception {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < (9 - i); j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("*" + " ");
}
System.out.println();
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int k = 0; k < (9 - i); k++) {
System.out.print("*" + " ");
}
System.out.println();
}
}
}

Pro85:-

public class Test {

public static void main(String args[]) throws Exception {


for(int i = 0; i < 8; i++)
{
for(int j = 0; j < (8 - i); j++)
{
System.out.print(" ");
}
for(int k = 0; k <= i; k++)
{
System.out.print("*"+" ");
}
System.out.println();
}
for(int i = 0; i < 7; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.print(" "+" ");
}
for(int k = 0; k < 3; k++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}

You might also like