You are on page 1of 3

Name: Rodrigo Chiva Jr.

BSIT 2D

Practice Question

1. Take 20 integer inputs from user and store them in an array and print them on screen.

Answer:
#include<iostream>
using namespace std;

int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cout << "Enter a number\n";
cin >> a[i];
}
cout << "Numbers are:\n";
for(int i=0;i<10;i++)
{
cout << a[i] << "\n";
}
return 0;
}

2. Take 10 integer inputs from user and store them in an array. Again ask user to give a number.
Now, tell user whether that number is present in array or not.

Answer:

import java.util.*;
public class isItPresent {
public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 int [] a = new int[10];
 boolean present = true;
 for(int i = 0; i < a.length; i++) {
  a[i] = input.nextInt();//take 10 inputs from the user
 }
System.out.println("Please enter the number you want to check: ");
int number = input.nextInt();
for(int j : a)  
if(j != number)
 present = false;
System.out.println(present);
}
}
3. Write a program to find the sum and product of all elements of an array.

Answer:
#include<iostream>
using namespace std;
int main()
{
    int array[10],n,i,sum=0,product=1;
    cout<<"enter the size of array"<<endl;
    cin>>n;
    cout<<"enter the element of array"<<endl;
    for(i=0;i<n;i++)
    cin>>array[i];
    for(i=0;i<n;i++){
        sum=sum+array[i];
        product=product*array[i];
    }
    cout<<"sum :"<<sum<<endl;
    cout<<"product :"<<product<<endl;
    return 0;
}

4. Take 10 integer inputs from user and store them in an array. Now, copy all the elements in an
another array but in reverse order.

Answer:

#include<iostream>
using namespace std;

int main()
{
int a[10], b[10];
for(int i=0;i<10;i++)
{
cout << "Enter a number\n";
cin >> a[i];
}
int j = 0;
for(int i=9;i>=0;i--)
{
b[i] = a[j];
j++;
}
for(int i=0;i<10;i++)
{
cout << b[i] << "\n";
}
return 0;
}
5. Consider an integer array, the number of elements in which is determined by the user. The
elements are also taken as input from the user. Write a program to find those pair of elements that
has the maximum and minimum difference among all element pairs.

Answer:

#include<iostream>
using namespace std;

int maxDiff(int arr[], int arr_size)


{     
  int max_diff = arr[1] - arr[0];
  for (int i = 0; i < arr_size; i++)
  {
    for (int j = i+1; j < arr_size; j++)
    {     
      if (arr[j] - arr[i] > max_diff) 
        max_diff = arr[j] - arr[i];
    } 
  }         
  return max_diff;

You might also like