Array

C++ program :How to take user input for integer array

C++ program :How to take user input for integer array

In this article, we will discuss the concept of C++ program :How to take user input for integer array

In this post, we are going to learn how to write a program to  input  integer for  integer array from user  in C ++  language   using for, while and do-while  loop

Code to input integer of an array

Code to input integer for an array using for loop

In this code, we are going to learn how to input integer for an integer array using for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int SIZE;
    int arr[SIZE];//Declare an array of SIZE
    int i,N;
    cout<<"Enter size of array\n";//Read size of the array
    cin>>N;

    cout<<"Enter "<<N<< "elements in the array\n";
    //Read elements of the array
    //Read elements of the array
    for(i=0; i<N; i++){
            cout<<"Enter arr["<<i<<"] element in the array: ";
        cin>>arr[i];
    }
    cout<<"\nEntered elements of an array\n";
    //print given elements of the array
    for(i=0; i<N; i++){
       cout<<arr[i];
       cout<<"\n";
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter size of array
4

Enter 4 elements in the array
Enter arr[0] elements in the array: 234
Enter arr[1] elements in the array: 678
Enter arr[2] elements in the array: 765
Enter arr[3] elements in the array: 654

Entered elements of an array
234
678
765
654

Code to input integer for an array using while loop

In this code, we are going to learn how to input integer for an integer array using while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int SIZE;
    int arr[SIZE];//Declare an array of MAX_SIZE
    int i,N;
    cout<<"Enter size of array\n";//Read size of the array
    cin>>N;

    cout<<"Enter "<<N<< " elements in the array\n";
    //Read elements of the array
    i=0;
    while(i<N){
            cout<<"Enter arr["<<i<<"] element in the array: ";
        cin>>arr[i];
        i++;
    }
    cout<<"\nEntered elements of an array\n";
    //print given elements of the array
    i=0;
    while(i<N){
       cout<<arr[i];
       cout<<"\n";
       i++;
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter size of array
5

Enter 5 elements in the array is an
Enter arr[0] elements in the array: 4
Enter arr[1] elements in the array: 6
Enter arr[2] elements in the array: 8
Enter arr[3] elements in the array: 9
Enter arr[4] elements in the array: 5


Entered elements of an array
4
6
8
9
5

 

Code to input integer for an array using do-while loop

In this code, we are going to learn how to input integer for an integer array using do-while loop in C++ language

Program 3

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int SIZE;
    int arr[SIZE];//Declare an array of MAX_SIZE
    int i,N;
    cout<<"Enter size of array\n";//Read size of the array
    cin>>N;

    cout<<"Enter "<<N<< " elements in the array\n";
    //Read elements of the array
    i=0;
    do{
            cout<<"Enter arr["<<i<<"] element in the array: ";
        cin>>arr[i];
        i++;
    }while(i<N);
    cout<<"\nEntered elements of an array\n";
    //print given elements of the array
    i=0;
    do{
       cout<<arr[i];
       cout<<"\n";
       i++;
    }while(i<N);
    getch();
    return 0;
}


When the above code is executed, it produces the following result

Enter size of array
3

Enter 3 elements in the array is an
Enter arr[0] elements in the array: 417
Enter arr[1] elements in the array: 826
Enter arr[2] elements in the array: 578

Entered elements of an array
417
826
578


Suggested for you

For loop in C language

while loop in c language

do-while loop in c language

Operator in C language

One dimensional array in C language

 

For loop in C++ language

while loop in C++ language

Do while loop in C++ language

Operator in C++ language

One dimensional array in C++ language

 

For loop in Java language

While loop in Java language

Do-while loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

One dimensional array in Java

 

Similar post

Java program to read and print array elements using for loop

Java program to read and print array elements using while loop

Java program to read and print array elements using Do-while loop

C code to take and print array elements using for loop

C code to take and print array elements using while loop

C code to take and print array elements using do-while loop

C++ program to read and print array elements using for loop

C++ program to read and print array elements using while loop

C++ program to read and print array elements using do-while loop

 

 

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.