public class Javaprogram38 { public static void main(String[] args) { Scanner kb
ID: 3865157 • Letter: P
Question
public class Javaprogram38
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int n;
System.out.println("Enter number of elements in a list: ");
n = kb.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter element of list: ");
a[i] = kb.nextInt();
}
//displaying the list
System.out.println("List elements are: ");
for(int i=0;i<n;i++)
System.out.print(a[i]+ " ");
System.out.println(" ");
//1. displaying the other element
System.out.println("List alternative elements are: ");
for(int i=0;i<n;i = i+2)
System.out.print(a[i]+ " ");
System.out.println(" ");
//2. First and last element
System.out.println("First element of list: " + a[0]);
System.out.println("Last element of list: " + a[n-1]);
//3. checking no. 7 exists in the list or not
for(int i=0;i<n;i++)
{
if(a[i]==7)
{
System.out.println("7 exists in the list");
break;
}
}
//4. count no. of 7 in the list
int count = 0;
for(int i=0;i<n;i++)
{
if(a[i]==7)
count++;
}
System.out.println("7 occurs "+ count+" times in the list");
}
}
program this code using while loop and do while and for loop
each section(1,2,3,4) should be a seperate code(not one code) and the loops should be sperate code too
Explanation / Answer
Using While Loop
public class Javaprogram38
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int n;
System.out.println("Enter number of elements in a list: ");
n = kb.nextInt();
int[] a = new int[n];
int i=0;
while(i<n)
{
System.out.println("Enter element of list: ");
a[i] = kb.nextInt();
++i;
}
//displaying the list
System.out.println("List elements are: ");
i=0;
while(i<n)
{
System.out.print(a[i]+ " ");
++i;
}
System.out.println(" ");
//1. displaying the other element
System.out.println("List alternative elements are: ");
i=0;
while(i<n)
{
System.out.print(a[i]+ " ");
i = i+2;
}
System.out.println(" ");
//2. First and last element
System.out.println("First element of list: " + a[0]);
System.out.println("Last element of list: " + a[n-1]);
//3. checking no. 7 exists in the list or not
i=0;
while(i<n)
{
if(a[i]==7)
{
System.out.println("7 exists in the list");
break;
}
++i;
}
//4. count no. of 7 in the list
int count = 0;
while(i<n)
{
if(a[i]==7)
count++;
++i;
}
System.out.println("7 occurs "+ count+" times in the list");
}
}
==================================================================
Do While
package dd;
import java.util.Scanner;
public class Javaprogram38
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int n;
System.out.println("Enter number of elements in a list: ");
n = kb.nextInt();
int[] a = new int[n];
int i=0;
do{
System.out.println("Enter element of list: ");
a[i] = kb.nextInt();
++i;
}while(i<n);
//displaying the list
System.out.println("List elements are: ");
i=0;
do{
System.out.print(a[i]+ " ");
++i;
} while(i<n);
System.out.println(" ");
//1. displaying the other element
System.out.println("List alternative elements are: ");
i=0;
do{
System.out.print(a[i]+ " ");
i = i+2;
}while(i<n);
System.out.println(" ");
//2. First and last element
System.out.println("First element of list: " + a[0]);
System.out.println("Last element of list: " + a[n-1]);
//3. checking no. 7 exists in the list or not
i=0;
do{
if(a[i]==7)
{
System.out.println("7 exists in the list");
break;
}
++i;
}while(i<n);
//4. count no. of 7 in the list
int count = 0;
do{
if(a[i]==7)
count++;
++i;
} while(i<n);
System.out.println("7 occurs "+ count+" times in the list");
}
}
Thanks, let me know if there is any concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.