convert or translate this Java Code to ( C PROGRAMMING)(STDIO.H) //import java util class import java.util.*; //import staement to import ThreadLocalRandom class import java.util.concurrent.ThreadLocalRandom; //main class public class Main { //main method public static void main(String[] args) { //create a scanner class object Scanner sc = new Scanner(System.in); //declaring an integer variables int userInput = 0, i; // create an array with a size of 10 elements int[] IntArray = new int[10]; //initiating random class ThreadLocalRandom NumRandom = ThreadLocalRandom.current(); //using for loop to store the value in array created by random class for(i = 0; i < 10; i++) { //assigning random values to array IntArray[i] = NumRandom.nextInt(1, 31); } //Display output Message: System.out.println("Please Enter your Choice"); /**while loop to continuos run the loop ***until user press 4. ***4 is to exit the program.*/ while(userInput != 4) { //propmt user to enter the choice System.out.println("1: Enter 1 to see all array element."); System.out.println("2: Enter 2 to see all elemnts of an array less than 25."); System.out.println("3: Enter 3 to see all elemnts of an array greater than or equal to 25."); System.out.println("4: Enter 4 to exit."); //getting input choice of user userInput = sc.nextInt(); //switch statement switch(userInput) { //case 1 is used to display all the elements of the array. case 1: System.out.println("Random Generated Array's elements are: "); for(i = 0; i < 10; i++) { //display the array elements System.out.print(IntArray[i] + " "); } //for next line System.out.println(); break; //case 2 is used to display all the elements of an array less than 25. case 2: System.out.println("Display element of array less than 25"); for(i = 0; i < 10; i++) { if(IntArray[i] < 25) //display the array elements System.out.print(IntArray[i] + " "); } //for next line System.out.println(); break; //case 3 is used to display all the elements of an array greater than or equal to 25. case 3: System.out.println("Display element of array greater than or equal to 25"); for(i = 0; i < 10; i++) { if(IntArray[i] >= 25) //display the array elements System.out.print(IntArray[i] + " "); } //for next line System.out.println(); break; //end program case 4: break; //when user not enter correct choice default: System.out.println("Please enter the correct choice."); } } } }
convert or translate this Java Code to ( C
//import java util class
import java.util.*;
//import staement to import ThreadLocalRandom class
import java.util.concurrent.ThreadLocalRandom;
//main class
public class Main
{
//main method
public static void main(String[] args) {
//create a scanner class object
Scanner sc = new Scanner(System.in);
//declaring an integer variables
int userInput = 0, i;
// create an array with a size of 10 elements
int[] IntArray = new int[10];
//initiating random class
ThreadLocalRandom NumRandom = ThreadLocalRandom.current();
//using for loop to store the value in array created by random class
for(i = 0; i < 10; i++)
{
//assigning random values to array
IntArray[i] = NumRandom.nextInt(1, 31);
}
//Display output Message:
System.out.println("Please Enter your Choice");
/**while loop to continuos run the loop
***until user press 4.
***4 is to exit the program.*/
while(userInput != 4)
{
//propmt user to enter the choice
System.out.println("1: Enter 1 to see all array element.");
System.out.println("2: Enter 2 to see all elemnts of an array less than 25.");
System.out.println("3: Enter 3 to see all elemnts of an array greater than or equal to 25.");
System.out.println("4: Enter 4 to exit.");
//getting input choice of user
userInput = sc.nextInt();
//switch statement
switch(userInput)
{
//case 1 is used to display all the elements of the array.
case 1:
System.out.println("Random Generated Array's elements are: ");
for(i = 0; i < 10; i++)
{
//display the array elements
System.out.print(IntArray[i] + " ");
}
//for next line
System.out.println();
break;
//case 2 is used to display all the elements of an array less than 25.
case 2:
System.out.println("Display element of array less than 25");
for(i = 0; i < 10; i++)
{
if(IntArray[i] < 25)
//display the array elements
System.out.print(IntArray[i] + " ");
}
//for next line
System.out.println();
break;
//case 3 is used to display all the elements of an array greater than or equal to 25.
case 3:
System.out.println("Display element of array greater than or equal to 25");
for(i = 0; i < 10; i++)
{
if(IntArray[i] >= 25)
//display the array elements
System.out.print(IntArray[i] + " ");
}
//for next line
System.out.println();
break;
//end program
case 4:
break;
//when user not enter correct choice
default:
System.out.println("Please enter the correct choice.");
}
}
}
}
- The code is given in java programming language.
- The code given below is converted or translate to C programming.
#include <stdio.h>
#include<stdlib.h>
int main()
{
//int *IntArray= new int[10];
int arr[10],userInput=0;
for(int i=0;i<10;i++)
arr[i]=rand()%10;
printf("Please enter your choice\n");
while (userInput!=4){
printf("1: Enter 1 to see all the elements\n");
printf("2: Enter 2 to see all elemnts of an array less than 25.\n");
printf("3: Enter 3 to see all elemnts of an array greater than or equal to 25.\n");
printf("4: Enter 4 to exit.\n");
//getting input choice of user
scanf("%d",&userInput);
//switch statement
switch(userInput)
{
//case 1 is used to display all the elements of the array.
case 1:
printf("Random Generated Array's elements are: \n");
for(int i = 0; i < 10; i++)
{
//display the array elements
printf("%d",arr[i]);
printf(" ");
}
//for next line
printf("\n");
break;
//case 2 is used to display all the elements of an array less than 25.
case 2:
printf("Display element of array less than 25\n");
for(int i = 0; i < 10; i++)
{
if(arr[i] < 25)
//display the array elements
printf("%d",arr[i]);
printf(" ");
}
//for next line
printf("\n");
break;
//case 3 is used to display all the elements of an array greater than or equal to 25.
case 3:
printf("Display element of array greater than or equal to 25\n");
for(int i = 0; i < 10; i++)
{
if(arr[i] >= 25)
//display the array elements
printf("%d",arr[i]);
printf(" ");
}
//for next line
printf("\n");
break;
//end program
case 4:
break;
//when user not enter correct choice
default:
printf("Please enter the correct choice.\n");
}
}
//printf("%d",rand()%32);
return 0;
}
Step by step
Solved in 3 steps with 1 images