Computer Programming Lab 5
Computer Programming Lab 5
2020
LAB REPORT
Class Section: C
Objectives:
1. Write a program to input twelve numbers from user using array and display all values on
console (3 values in a row) (use separate loops for input and output operation).
SAMPLE OUTPUT:
Value 1=?? Value 2=?? Value 3= ??
Value 4=?? Value 5=?? Value 6= ??
Value 7=?? Value 8=?? Value 9= ??
Value 10=?? Value 11=?? Value 12= ??
#include<iostream>
main()
{
int x[12]; int
h=0; for(int
i=0;i<12;++i)
cin>>x[i];
for(int i=1;i<5;i++)
for(int j=0;j<3;j++)
cout<<"value #"<<h+1<<"="<<x[h]<<"\t";
h++;
cout<<endl;
}
2. Write a program to input ten numbers from user using array and find the average of numbers
(use separate loops for input operation and calculation).
#include<iostream>
main()
num"<<endl; for(int
i=0;i<10;i++)
cin>>x[i];
sum=sum+x[i];
}
avg=sum/10; cout<<"average of the num u
entered is"<<avg;
3. Write a program to input multiple values from user using array and find the maximum ( use
separate loops for input operation and calculation ).
#include<iostream>
main()
cout<<endl;
for(i=0;i<k;++i)
{
cout<<"enter num"<<i+1<<":";
cin>>arr[i];
for(i=1;i<k;i++)
if(arr[0]<arr[i])
arr[0]=arr[i];
cout<<"largest element="<<arr[0];
4. Write a program to input two arrays from user and find the sum of arrays (element by
{
int x[3] ,y[2],sum=0;
for(int i=0;i<5;++i)
variablex\n";
cin>>x[i];
cin>>y[i];
cout<<endl;
for(int i=0;i<5;++i)
{ sum=x[i]+y[i];
}
5. Write a program to input array from user and display the elements of array in reverse order
(use separate loops for input and output operation).
SAMPLE OUTPUT:
Input = 1, 2, 3, 4, 5
Output = 5, 4, 3, 2, 1
#include<iostream>
main()
for(c=0;c<n;c++) cin>>a[c];
for (c=n-1,d=0;c>=0;c--,d++)
b[d]=a[c]; for(c=0;c<n;c++)
a[c]=b[c];
for(c=0;c<n;c++)
cout<<a[c]<<endl;
6. Write a program to input multiple values from user using array and then ask user to input
a key number and then compare it with values entered by user(for array) and then
display the index number of array with which value get matched (use seperate loops for
input operation and calculation) (use different loops for input operation and calculation).
#include<iostream>
using namespace std; int
main()
cond=false; cout<<"enter
for(int i=0;i<n;i++)
cout<<"enter value#"<<i+1<<":";
cin>>x[i];
number:"; cin>>key;
for(j=0;j<n;j++)
if(x[j]==key)
cond==true;
m=j; }
if(cond)
cout<<"key you entered is matched"<<m<<"index of