Accept array input and print
Table of Contents
In this article, we will discuss the concept of C++ program to get array input and print using While loop
In this post, we are going to learn how to write a program to read array input and print given elements in an array using while loop in C++ language
In this code, we are going to learn how to read integer array input given by user and print the them using while loop in C++ language
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,len;//integer variable declaration
cout<<"Enter the Array length: ";
//take input for array length
cin>>len;
//Reading the input from user for array length
int num[len];
//one dim Array declaration
cout<<"\n";//move to next line
cout<<"Enter the Number for array :";
//take input for elements(marks)
i=0;
while(i<len){//input using while loop
cin>>num[i];
//Reading the array elements from user
i++;
}
cout<<"\ndisplay the number\n";
i=0;
while(i<len){//display elements using for loop
cout<<num[i];
cout<<("\n");
i++;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,len;//integer variable diclaration
cout<<"Enter the Array length: ";
//request input for array length
cin>>len;
//Reading the input for array length
int num[len];
//Array declaration - single dimension
cout<<"\n";//move to next line
i=0;
//Request input for elements(number)
while( i<len){//input using while loop
cout<<"Enter the marks "<<"marks["<<i<<"] :";
cin>>num[i];
//Reading the array elements from user
i++;
}
cout<<"\ndisplay the marks\n";
i=0;
while(i<len){//display elements using while loop
cout<<"marks["<<i<<"] :"<<num[i];
cout<<("\n");
i++;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter the array length: 5 Enter the marks marks[0] :456 Enter the marks marks[0] :712 Enter the marks marks[0] :984 Enter the marks marks[0] :376 Enter the marks marks[0] :512 Display the marks marks[0] :456 marks[0] :712 marks[0] :984 marks[0] :376 marks[0] :512
In this code, we are going to learn how to read string array input given by user and print the them using while loop in C++ language
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
string str[20];
//String array declaration
int len;
//Variable declaration - integer
cout<<"Enter array length\n";
//Ask input for array length
cin>>len;
//Reading the array length
cout<<"\nEnter students name\n";
int i=0;
while(i<len){
cin>>str[i];
i++;
}//Enter the student name using while loop
cout<<"\nEntered students names are:\n";
int j=0;
while(j<len){
cout<<str[j]<<"\n";
j++;
}// Display the student name using while loop
getch();
return 0;
}
When the above code is executed, it produces the following result
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
string str[20];
//String array declaration
int len;
//Variable declaration - integer
cout<<"Enter array length\n";
//Ask input for array length
cin>>len;
//Reading the array length
cout<<"\nEnter students name\n";
int i=0;
while(i<len){
cout<<"Enter the string"<<" str=["<<i<<"]: ";
cin>>str[i];
i++;
}//Enter the student name using while loop
cout<<"\nEntered students names are:\n";
int j=0;
while(j<len){
cout<<"Your entered string is:"<<" str=["<<j<<"]: ";
cout<<str[j]<<"\n";
j++;
}// Display the student name using while loop
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter the array length: 5 Enter students name Enter the string str[0] :Kumuthini Enter the string str[1] :Mahilini Enter the string str[2] :Nimalini Enter the string str[3] :Sutharsini Enter the string str[4] :kegini Entered students names are: you entered string is: str[0] :Kumuthini you entered string is: str[1] :Mahilini you entered string is: str[2] : Nimalini you entered string is: str[3] :Sutharsini you entered string is: str[4] :kegini
In this code, we are going to learn how to read character array input given by user and print the them using while loop in C++ language
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,len;
//variable declaration for integr
cout<<"Enter the Array length: ";
//Request input for array length
cin>>len;
//Reading the input for array length
char char_array[len];
//character Array declaration
cout<<"\n";//move to next line
cout<<"Enter the characters for array :";
//Request input for elements(numbers)
i=0;
while(i<len){//input using while loop
cin>>char_array[i];
//Reading the array elements from user
i++;
}
cout<<"\ndisplay the characters\n";
i=0;
while(i<len){//display elements using while loop
cout<<char_array[i];
cout<<("\n");
i++;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,len;
//variable declaration for integer
cout<<"Enter the Array length: ";
//Request input for array length
cin>>len;
//Reading the input for array length
char char_array[len];
//Array declaration
cout<<"\n";//move to next line
cout<<"Enter the characters for array :\n";
//Request input for elements(numbers)
i=0;
while(i<len){//input using while loop
cout<<"char_array["<<i<<"]:";
cin>>char_array[i];
//Reading the array elements from user
i++;
}
cout<<"\ndisplay the characters\n";
i=0;
while(i<len){//display elements using while loop
cout<<"char_array["<<i<<"]:"<<char_array[i];
cout<<("\n");
i++;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Suggested for you
Variable in C++ language
Data type in C++ language
One dimensional array in C++ language
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 read and print array elements using for loop
C code to read and print array elements using while loop
C code to read 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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.