Skip to main content

Basic program of array in java

import java.util.Scanner;
class TestArray{
public static void main(String...args){
Scanner sc=new Scanner(System.in);
System.out.print("Enter size: ");
int size =sc.nextInt();
int[] array=new int[size];
int[] frq= new int[size];
System.out.println("Enter "+size+" Elements: ");
for(int i=0;i<size;i++){
array[i]=sc.nextInt();
frq[i]=-1;
}
System.out.println("Press 1 to show left to right: ");
System.out.println("Press 2 to show right to left: ");
System.out.println("Press 3 to show in ascending Order: ");
System.out.println("Press 4 to show in decending Order: ");
System.out.println("Press 5 to show sum : ");
System.out.println("Press 6 to show repeatation of element: ");
while(true){
System.out.print("\nEnter your choice:");
int choice=sc.nextInt();
switch(choice){
case 1:{
for(int vr:array)
System.out.print(vr+" ");
}
break;
case 2:{
for(int i=size-1;i>=0;i--){
System.out.print(array[i]+" ");
}
}
break;
case 3:{
System.out.print("No. in ascending order: ");
int temp;
for(int i=0;i<size;i++){
for(int j=0;j>size;j++){
if(array[i]<=array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
for(int vr:array)
System.out.print(vr+" ");
}
break;
case 4:{
System.out.print("No. in decending order: ");
int temp;
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
if(array[i]>=array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
for(int vr:array)
System.out.print(vr+" ");
}
break;
case 5:{
int sum=0;
for(int i=0;i<size;i++){
sum+=array[i];
}
System.out.print("sum is: "+sum);
}
break;
case 6:{
int count=0;
for(int i=0;i<size;i++){
count= 1;
for(int j=i+1;j<size;j++){
if(array[i]==array[j]){
count++;
frq[j]=0;
}
}
if(frq[i]!=0){
frq[i]=count;
}
}
for(int i=0;i<size;i++){
if(frq[i]!=0){
if(frq[i]==1)
System.out.println(array[i]+" comes "+frq[i]+" Time");
else 
System.out.println(array[i]+" comes "+frq[i]+" Times");
}  
}
}
break;
default:
System.out.println("Illegal Entry");
}
}
}
}

Comments

Popular posts from this blog

JAVA program of showing the student marksheet

package june13; import java.util.Scanner; public class student { int roll; String name; String email; int hindi,english,physics,chemistry,maths; void  input_details(){ System.out.println("please enter the following details "); Scanner sc= new Scanner(System.in); System.out.println("enter the roll number "); roll=sc.nextInt(); System.out.println("enter the name of student "); name=sc.next(); System.out.println(" enter the email"); email=sc.next(); System.out.println(" enter the marks of hindi "); hindi=sc.nextInt(); System.out.println("enter the marks of english "); english=sc.nextInt(); System.out.println(" enter the marks of physics ");; physics=sc.nextInt(); System.out.println(" enter the marks of chemistry "); chemistry=sc.nextInt(); System.out.println("enter the marks of maths "); maths=sc.nextInt(); ...

Program of Making a simple calculator using java swing

import javax.swing.*; import java.awt.event.*; class Calc implements ActionListener {     JFrame f;     JTextField t;     JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;     static double a=0,b=0,result=0;     static int operator=0;     Calc()     {         f=new JFrame("Calculator");         t=new JTextField();         b1=new JButton("1");         b2=new JButton("2");         b3=new JButton("3");         b4=new JButton("4");         b5=new JButton("5");         b6=new JButton("6");         b7=new JButton("7");         b8=new JButton("8");         b9=new JButton("9");         b0=new JButton("0");         b...

C Program to find next larger number from a given array from target number

  Here is Program:   #include<stdio.h> #include<stdlib.h>   void sort(int arr[] ,int n)  {   int i,j,temp;   for (i = 0 ; i < ( n - 1 ); i++)   {     for (j = i+1 ; j <( n - 1); j++)     {       if (arr[i] > arr[j])       {         temp  = arr[i];         arr[i]   = arr[j];         arr[j] = temp;       }      }    }   }   int main()   {     int a[] = {2,11,11,6,80};     int i, num1 = 8;     sort(a,5);         for(i=0;i<sizeof(a)/sizeof(a[i]);i++)     {         if(a[4]<num1)          {   ...