0% found this document useful (0 votes)
54 views

C++ Programming Exercises Basic Concepts

The document contains 6 code snippets that demonstrate different programming concepts: 1) A program that reads a value N and calculates the average of the next N input elements. 2) A program that reads a value N and calculates the average of the next N input elements greater than 10. 3) Programs that find the minimum and maximum values in an array with and without using an array. 4) A program that reads N values, sorts the array, and displays the third smallest number. 5) A function that finds the greatest common divisor of two input integers. 6) A function that finds the smallest common factor of two input integers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

C++ Programming Exercises Basic Concepts

The document contains 6 code snippets that demonstrate different programming concepts: 1) A program that reads a value N and calculates the average of the next N input elements. 2) A program that reads a value N and calculates the average of the next N input elements greater than 10. 3) Programs that find the minimum and maximum values in an array with and without using an array. 4) A program that reads N values, sorts the array, and displays the third smallest number. 5) A function that finds the greatest common divisor of two input integers. 6) A function that finds the smallest common factor of two input integers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1)

/* Program that reads a value of N and computes average of next N


elements of inputs */

#include <iostream>
using namespace std;

int main()
{

int n, i, j; //Declare variables


double sum = 0;
double average = 0;

cout << " Enter the limit of values N: "; //Prompt user to enter value for N
cin >> n;
cout << "\n"; cout<< endl;

for (i = 1; i <= n; i++) //Condition to accept and add values


{
cout << "Enter number: " <<endl;
cin >> j;

sum=sum + j;
}

average = sum / n; //Compute average

cout << "\n The Average is : " << average << endl; //Display average

return 0;
}
2)
/* Program that reads a value of N and computes average of next N
elements of inputs greater than 10 */

#include <iostream>
using namespace std;

int main()
{

int n, i, N ,v=0; //Declare variables


double sum = 0;
double average = 0;

cout << " Enter the limit of values N: "; //Prompt user to enter value for N
cin >> N;
cout << "\n"; cout<< endl;

for (i = 0; i <N; i++) //Condition to accept and add values


{
cout << "Enter number: " <<endl;

cin >> n;

if ( n>10 ) //Check if value entered is greater than 10


{
sum=sum + n;
v=v+1;
}

average = sum /v; //Compute average

cout << "\n The Average is : " << average << endl;

return 0;
}
3)
Without Array

#include <iostream>
using namespace std;

int main()
{
//variables
int min, max;
int n, i, num;

min = INT_MAX; //assign maximum value for a int variable type to min
max = INT_MIN; //assign minimum value for a int variable type to max

//prompt user inputs for N


cout << "Enter number for N: ";
cin >> n;

//finding smallest and largest values


for(i=0; i<n; i++)
{
cout << "Please enter a number: " ;
cin >> num;

if(num>max)
{
max = num;
}

if(num<min)
{
min = num;
}
}

//display smallest and largest values


cout << "Smallest element = " << min << endl;
cout << "Largest element = " << max << endl;
}
With Array

/* Program to read n values and display


largest and smallest numbers entered */

#include<iostream>
using namespace std;
int main ()
{
int arr[10], n, i, max, min;

cout << "Enter the size of the array : "; cout << "\n";
cin >> n;
cout << "Enter the elements of the array : "; cout << "\n";

for (i = 0; i < n; i++)


cin >> arr[i];
max = arr[0];

for (i = 0; i < n; i++)


{
if (max < arr[i])
max = arr[i];
}

min = arr[0];

for (i = 0; i < n; i++)


{

if (min > arr[i])


min = arr[i];
}

cout << "Largest element : " << max; cout <<"\n";


cout << "Smallest element : " << min;
return 0;
}
4)
/* Program to read N values and display third
smallest value */

#include <iostream>
#include <bits/stdc++.h> //includes every standard library
using namespace std;

int main()
{
int N, i;

cout << "Enter number for N: "; //prompt user inputs for N
cin >> N;

//check if N>=3, else reenter

int arr[N]; //define array

for (i=0; i<N; i++) //promt user inputs to array


{
cout << "Please enter a number: " ;
cin >> arr[i];
}

cout << "\nArray: "; //output array


for(i=0; i<N; i++)
{
cout << arr[i] << " ";
}

//sort the array in ascending order


int p = sizeof(arr) / sizeof(arr[0]); // find the point we want the array to be
sorted
sort(arr, arr + p); // sorting statement

//output after sort


cout << "\nArray after sorting: ";
for (int i = 0; i < p; ++i)
cout << arr[i] << " ";

//since the 2nd position is the third smallest number...


cout << "\nThird smallest number is: " << arr[2];

return 0;
}
5)
/* Function to find Greatest Common Divisor
for 2 input integers */

#include <iostream>
using namespace std;

void greatestCommonDivisor() //define the function


{
int n1, n2, gcd, i;

cout << "Input the 1st integer: "; //get user inputs and assign them
cin >> n1;
cout << "Input the 2nd integer: ";
cin >> n2;

for (i=1; i<=n1 && i<=n2; i++) //calculate gcd


{
if (n1%i == 0 && n2%i == 0)
{
gcd = i;
}
}

cout << "Greatest Common Divisor: " << gcd << endl; //display gcd
}
int main()
{
greatestCommonDivisor(); //call the function
return 0;
}
6)
/* Function to compute smallest common factor
for 2 input integers */

#include <iostream>
using namespace std;

void smallestCommonFactor()//define the function


{
int n1, n2, gcd, scf, i;

//getting user inputs and assigning them


cout << "Input the 1st integer: ";
cin >> n1;
cout << "Input the 2nd integer: ";
cin >> n2;

//to calculate scf


for (i=1; i<=n1 && i<=n2; i++) //calculate gcd first
{
if (n1%i == 0 && n2%i == 0)
{
gcd = i;
}
}
scf = (n1*n2)/gcd; //then calculate scf

//display scf
cout << "Smallest Common Factor: " << scf << endl;
}

int main()
{
smallestCommonFactor(); //call the function
return 0;
}

You might also like