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

Programming Control Structures on Console

The document contains multiple C++ programs demonstrating various functionalities such as inputting and displaying a person's information, reversing an array, sorting an array, finding duplicates, calculating the sum of array elements, and identifying even and odd numbers. Each program includes user prompts for input and outputs the results accordingly. The examples illustrate basic programming concepts and array manipulations in C++.

Uploaded by

ashergundumura
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Programming Control Structures on Console

The document contains multiple C++ programs demonstrating various functionalities such as inputting and displaying a person's information, reversing an array, sorting an array, finding duplicates, calculating the sum of array elements, and identifying even and odd numbers. Each program includes user prompts for input and outputs the results accordingly. The examples illustrate basic programming concepts and array manipulations in C++.

Uploaded by

ashergundumura
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

#include <iostream>

using namespace std;

struct Person
{
char name[50];
int age;
float salary;
};

int main()
{
Person p1;

cout << "Enter Full name: ";


cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;

cout << "\nDisplaying Information." << endl;


cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;

return 0;
}

Output

Enter Full name: Magdalena Dankova


Enter age: 27
Enter salary: 1024.4

Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4
#include <iostream>
using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};

int main()
{
Person p1;

cout << "Enter Full name: ";


cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;

cout << "\nDisplaying Information." << endl;


cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;

return 0;
}

Output

Enter Full name: Magdalena Dankova


Enter age: 27
Enter salary: 1024.4

Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4
#include <iostream>
using namespace std;

struct Person
{
char name[50];
int age;
float salary;
};

int main()
{
Person p1;

cout << "Enter Full name: ";


cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;

cout << "\nDisplaying Information." << endl;


cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;

return 0;
}

Output

Enter Full name: Magdalena Dankova


Enter age: 27
Enter salary: 1024.4

Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4

/* C++ Program to Reverse an Array using functions */

#include <iostream>
using namespace std;

void Reverse_Array(int array[],int size);

int main()

int i,a[50],size;

cout<<"Enter array size( Max:50 ) :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

cout<<"\nEnter arr["<<i<<"] Element :: ";

cin>>a[i];

cout<<"\nStored Data in Array :: \n\n";

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

cout<<" "<<a[i]<<" ";


}

// Calling Reverse Array Values Function

Reverse_Array(a,size);

cout << "\n\nReversed Array Values are :: " << endl;

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

cout<<" "<<a[i]<<" ";

cout<<"\n";

return 0;

//------Reverse Array Function---------------

void Reverse_Array(int array[],int size)

int temp;

size--;

for (int i=0;size>=i;size--,i++)

{
temp=array[i];

array[i]=array[size];

array[size]=temp;

OUTPUT : :

/* C++ Program to Reverse an Array using functions */

Enter array size( Max:50 ) :: 8

Enter array elements ::

Enter arr[0] Element :: 1

Enter arr[1] Element :: 2

Enter arr[2] Element :: 3

Enter arr[3] Element :: 4


Enter arr[4] Element :: 5

Enter arr[5] Element :: 6

Enter arr[6] Element :: 7

Enter arr[7] Element :: 8

Stored Data in Array ::

1 2 3 4 5 6 7 8

Reversed Array Values are ::

8 7 6 5 4 3 2 1

Process returned 0

/* C++ Program to Reverse an Array using functions */

#include <iostream>

using namespace std;


void Reverse_Array(int array[],int size);

int main()

int i,a[50],size;

cout<<"Enter array size( Max:50 ) :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

cout<<"\nEnter arr["<<i<<"] Element :: ";

cin>>a[i];

cout<<"\nStored Data in Array :: \n\n";

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

cout<<" "<<a[i]<<" ";

// Calling Reverse Array Values Function


Reverse_Array(a,size);

cout << "\n\nReversed Array Values are :: " << endl;

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

cout<<" "<<a[i]<<" ";

cout<<"\n";

return 0;

//------Reverse Array Function---------------

void Reverse_Array(int array[],int size)

int temp;

size--;

for (int i=0;size>=i;size--,i++)

temp=array[i];

array[i]=array[size];
array[size]=temp;

OUTPUT : :

/* C++ Program to Reverse an Array using functions */

Enter array size( Max:50 ) :: 8

Enter array elements ::

Enter arr[0] Element :: 1

Enter arr[1] Element :: 2

Enter arr[2] Element :: 3

Enter arr[3] Element :: 4

Enter arr[4] Element :: 5


Enter arr[5] Element :: 6

Enter arr[6] Element :: 7

Enter arr[7] Element :: 8

Stored Data in Array ::

1 2 3 4 5 6 7 8

Reversed Array Values are ::

8 7 6 5 4 3 2 1

Process returned 0

/* C++ Program to Sort Array Elements in Ascending order */

#include<iostream>

using namespace std;

int main()
{

int i,j,temp,a[50],size;

cout<<"Enter array size( Max:50 ) :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

cout<<"\nEnter arr["<<i<<"] Element :: ";

cin>>a[i];

cout<<"\nStored Data Before Sorting In Array :: \n\n";

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

cout<<" "<<a[i]<<" ";

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

for(j=0;j<size-i-1;j++)
{

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

cout<<"\n\nStored Data After Sorting In Array :: \n\n";

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

cout<<" "<<a[i]<<" ";

cout<<"\n";

return 0;

}
OUTPUT : :

/* C++ Program to Sort Array Elements in Ascending order */

Enter array size( Max:50 ) :: 8

Enter array elements ::

Enter arr[0] Element :: 4

Enter arr[1] Element :: 1

Enter arr[2] Element :: 5

Enter arr[3] Element :: 8

Enter arr[4] Element :: 0

Enter arr[5] Element :: 9

Enter arr[6] Element :: 4


Enter arr[7] Element :: 1

Stored Data Before Sorting In Array ::

4 1 5 8 0 9 4 1

Stored Data After Sorting In Array ::

0 1 1 4 4 5 8 9

Process returned 0

/* C++ Program to Find Duplicate Elements in an Array */

#include<iostream>

using namespace std;

int main()

int i,j,a[50],size;

cout<<"Enter array size( Max:50 ) :: ";


cin>>size;

cout<<"\nEnter array elements :: \n";

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

cout<<"\nEnter arr["<<i<<"] Element :: ";

cin>>a[i];

cout<<"\nStored Data in Array :: \n\n";

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

cout<<" "<<a[i]<<" ";

cout<<"\n\nDuplicate Values in Given Array are :: \n\n";

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

for(j=i+1;j<size;j++)

if(a[i]==a[j])
{

cout<<" "<<a[i]<<" ";

cout<<"\n";

return 0;

OUTPUT : :

/* C++ Program to Find Duplicate Elements in an Array */

Enter array size( Max:50 ) :: 8

Enter array elements ::

Enter arr[0] Element :: 1


Enter arr[1] Element :: 2

Enter arr[2] Element :: 3

Enter arr[3] Element :: 4

Enter arr[4] Element :: 5

Enter arr[5] Element :: 2

Enter arr[6] Element :: 3

Enter arr[7] Element :: 6

Stored Data in Array ::

1 2 3 4 5 2 3 6

Duplicate Values in Given Array are ::

2 3
Process returned 0

/* C++ Program to Find Sum of Elements of an Array */

#include<iostream>

using namespace std;

int main()

int i,a[50],sum=0,size;

cout<<"Enter array size( Max:50 ) :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

cout<<"\nEnter arr["<<i<<"] Element :: ";

cin>>a[i];

cout<<"\nStored Data in Array :: \n\n";


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

cout<<" "<<a[i]<<" ";

cout<<"\n\nSum of all Elements of array is :: ";

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

sum=sum+a[i];

cout<<sum<<"\n";

return 0;

OUTPUT : :

/* C++ Program to Find Sum of Elements of an Array */


Enter array size( Max:50 ) :: 8

Enter array elements ::

Enter arr[0] Element :: 1

Enter arr[1] Element :: 2

Enter arr[2] Element :: 3

Enter arr[3] Element :: 4

Enter arr[4] Element :: 5

Enter arr[5] Element :: 6

Enter arr[6] Element :: 7

Enter arr[7] Element :: 8

Stored Data in Array ::


1 2 3 4 5 6 7 8

Sum of all Elements of array is :: 36

Process returned 0

/* C++ Program to Reverse elements in an array */

#include<iostream>

using namespace std;

int main()

int a[20],b[20],i,j,size;

cout<<"Enter array size( Max:50 ) :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

cout<<"\nEnter arr["<<i<<"] Element :: ";


cin>>a[i];

cout<<"\nThe Entered Array is :: \n\n";

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

cout<<" "<<a[i]<<" ";

cout<<"\n\nReverse of Given Array is :: \n\n";

for(i=size-1,j=0; i>=0;i--,j++)

b[i]=a[j];

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

cout<<" "<<b[i]<<" ";

}
cout<<"\n";

return 0;

OUTPUT : :

/* C++ Program to Reverse elements in an array */

Enter array size( Max:50 ) :: 7

Enter array elements ::

Enter arr[0] Element :: 1

Enter arr[1] Element :: 2

Enter arr[2] Element :: 3

Enter arr[3] Element :: 4


Enter arr[4] Element :: 5

Enter arr[5] Element :: 6

Enter arr[6] Element :: 7

The Entered Array is ::

1 2 3 4 5 6 7

Reverse of Given Array is ::

7 6 5 4 3 2 1

Process returned 0

/* C++ Program to Find Even and Odd Numbers using array */

#include<iostream>

using namespace std;

int main()

{
int arr[20],even[20],odd[20],i,j=0,k=0,size;

cout<<"Enter array size( Max:50 ) :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

cout<<"\nEnter arr["<<i<<"] Element :: ";

cin>>arr[i];

cout<<"\nStored Data in Array :: \n\n";

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

cout<<" "<<arr[i]<<" ";

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

if(arr[i]%2==0)
{

even[j]=arr[i];

j++;

else

odd[k]=arr[i];

k++;

cout<<"\n\nEven Elements in Array are :: \n\n";

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

cout<<" "<<even[i]<<" ";

cout<<"\n\nOdd Elements in Array are :: \n\n";

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

{
cout<<" "<<odd[i]<<" ";

cout<<"\n";

return 0;

OUTPUT : :

/* C++ Program to Find Even and Odd Numbers using array */

Enter array size( Max:50 ) :: 10

Enter array elements ::

Enter arr[0] Element :: 1

Enter arr[1] Element :: 2


Enter arr[2] Element :: 3

Enter arr[3] Element :: 4

Enter arr[4] Element :: 5

Enter arr[5] Element :: 6

Enter arr[6] Element :: 7

Enter arr[7] Element :: 8

Enter arr[8] Element :: 9

Enter arr[9] Element :: 0

Stored Data in Array ::

1 2 3 4 5 6 7 8 9 0

Even Elements in Array are ::


2 4 6 8 0

Odd Elements in Array are ::

1 3 5 7 9

Process returned 0

/* C++ Program to Find the Smallest Number in an array */

#include<iostream>

using namespace std;

int main()

int small, arr[50], m, j;

cout<<"Enter size of Array ( Max:50 ) :: ";

cin>>m;

cout<<"\nEnter Elements to Array Below :: \n";

for(j=0;j<m;++j)

{
cout<<"\nEnter arr["<<j<<"] Element :: ";

cin>>arr[j];

cout<<"\nSearching for smallest element ...\n\n";

small=arr[0];

for(j=0; j<m; j++)

if(small>arr[j])

small=arr[j];

cout<<"Smallest Element in Array = "<<small<<"\n";

return 0;

OUTPUT : :
/* C++ Program to Find the Smallest Number in an array */

Enter size of Array ( Max:50 ) :: 8

Enter Elements to Array Below ::

Enter arr[0] Element :: 0

Enter arr[1] Element :: 3

Enter arr[2] Element :: 5

Enter arr[3] Element :: 1

Enter arr[4] Element :: 8

Enter arr[5] Element :: 5

Enter arr[6] Element :: 3

Enter arr[7] Element :: 9


Searching for smallest element ...

Smallest Element in Array = 0

Process returned 0

/* C++ Program to Find Largest of n Numbers in an Array */

#include<iostream>

using namespace std;

int main()

int large, arr[100], size, i;

cout<<"Enter Size of Array :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

{
cout<<"\nEnter ["<<i+1<<"]"<<" Element :: ";

cin>>arr[i];

cout<<"\nSearching for largest number ...\n\n";

large=arr[0];

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

if(large<arr[i])

large=arr[i];

cout<<"Largest Number in Array = "<<large<<"\n";

return 0;

OUTPUT : :
/* C++ Program to Find Largest of n Numbers in an Array */

Enter Size of Array :: 8

Enter array elements ::

Enter [1] Element :: 5

Enter [2] Element :: 1

Enter [3] Element :: 2

Enter [4] Element :: 9

Enter [5] Element :: 4

Enter [6] Element :: 0

Enter [7] Element :: 7

Enter [8] Element :: 5


Searching for largest number ...

Largest Number in Array = 9

Process returned 0

/* C++ Program to Find Largest of n Numbers in an Array */

#include<iostream>

using namespace std;

int main()

int large, arr[100], size, i;

cout<<"Enter Size of Array :: ";

cin>>size;

cout<<"\nEnter array elements :: \n";

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

{
cout<<"\nEnter ["<<i+1<<"]"<<" Element :: ";

cin>>arr[i];

cout<<"\nSearching for largest number ...\n\n";

large=arr[0];

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

if(large<arr[i])

large=arr[i];

cout<<"Largest Number in Array = "<<large<<"\n";

return 0;

OUTPUT : :
/* C++ Program to Find Largest of n Numbers in an Array */

Enter Size of Array :: 8

Enter array elements ::

Enter [1] Element :: 5

Enter [2] Element :: 1

Enter [3] Element :: 2

Enter [4] Element :: 9

Enter [5] Element :: 4

Enter [6] Element :: 0

Enter [7] Element :: 7

Enter [8] Element :: 5


Searching for largest number ...

Largest Number in Array = 9

Process returned 0

/* C++ Program to find Average of n Numbers using array */

#include <iostream>

using namespace std;

int main()

int n, i;

float a[100], sum=0.0 , average;

cout<<"Enter size of Array (Value of N ):: ";

cin>>n;

cout<<"\nEnter elements to the array :: \n";

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

{
cout<<"\nEnter "<<i+1<<" element :: ";

cin>>a[i];

sum += a[i];

average = sum / n;

cout << "\nAverage of "<<n<<" Numbers is = " << average<<"\n";

return 0;

OUTPUT : :

/* C++ Program to find Average of n Numbers using array */

Enter size of Array (Value of N ):: 6

Enter elements to the array ::

Enter 1 element :: 1
Enter 2 element :: 2

Enter 3 element :: 3

Enter 4 element :: 4

Enter 5 element :: 5

Enter 6 element :: 6

Average of 6 Numbers is = 3.5

Process returned 0

You might also like