Table of Contents
In this article, we will discuss the concept of C++ program to accept array input and print using For loop
In this post, we are going to learn how to write a program to read array input and print given elements of an array using for 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 for 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: "; //Ask input for array length cin>>len; //Reading the input for array length int num[len]; //Array declaration cout<<"\n";//move to next line cout<<"Enter the Number for array :"; //Ask input for elements(numbers) for(i=0; i<len; i++){//input using for loop cin>>num[i]; //Reading the array elements from user } cout<<"\ndisplay the number\n"; for(i=0; i<len; i++){//display elements using for loop cout<<num[i]; cout<<("\n"); } 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 declaration cout<<"Enter the Array length: "; //Ask input from the user for array length cin>>len; //Reading the input for array length int marks[len]; //Array declaration - integer cout<<"\n";//move to next line //Ask input for elements(number) for(i=0; i<len; i++){//input using for loop cout<<"Enter the marks "<<"marks["<<i<<"] :"; cin>>marks[i]; //Reading the array elements from user } cout<<"\ndisplay the marks\n"; for(i=0; i<len; i++){//display elements using for loop cout<<"marks["<<i<<"] :"<<marks[i]; cout<<("\n"); } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the array length: 5 Enter the marks marks[0]: 46 Enter the marks marks[1]: 78 Enter the marks marks[2]: 94 Enter the marks marks[3]: 36 Enter the marks marks[4]: 50 Display the marks marks[0]=46 marks[1]: 78 marks[2]: 94 marks[3]: 36 marks[4]: 50
In this code, we are going to learn how to read string array input given by user and print them using for loop in C++ language
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { string str[20]; //Single D array declaration int len;//Declare variable for length cout<<"Enter array length\n"; //Ask input from the user for length cin>>len; //Reading the input for length cout<<"\nEnter students name\n"; //Ask enter the student name for(int i=0; i<len; i++){ cin>>str[i]; }//Enter the students name using for loop cout<<"\nEntered students names are:\n"; for(int i=0; i<len; i++){ cout<<str[i]<<"\n"; //Display the students name } 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]; //Single D array declaration int len;//Declare variable for length cout<<"Enter array length\n"; //Ask input from the user for length cin>>len; //Reading the input for length cout<<"\nEnter students name\n"; //Ask enter the student name for(int i=0; i<len; i++){ cout<<"Enter the string"<<" str=["<<i<<"]: "; cin>>str[i]; }//Enter the students name using for loop cout<<"\nEntered students names are:\n"; for(int i=0; i<len; i++){ cout<<"Your entered string is:"<<" str=["<<i<<"]: "; cout<<str[i]<<"\n"; //Display the students name } getch(); return 0; }
When the above code is executed, it produces the following result
In this code, we are going to learn how to read character array input given by user and print the them using for 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: "; //request input for array length cin>>len; //Reading the input for array length char char_array[len]; //declare character Array cout<<"\n"; //move to next line cout<<"Enter the characters for array :"; //Request input for elements(numbers) for(i=0; i<len; i++){//input using for loop cin>>char_array[i]; //Reading the array elements from user } cout<<"\ndisplay the characters\n"; for(i=0; i<len; i++){//print elements using for loop cout<<char_array[i]; cout<<("\n"); } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the array length:10 Enter the characters for array :Code4javac display the characters C o d e 4 j a v a c
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int i,len; //variable declaration - 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 - char array cout<<"\n";//move to next line cout<<"Enter the characters for array :\n"; //Ask input for elements(numbers) for(i=0; i<len; i++){//input using for loop cout<<"char_array["<<i<<"]:"; cin>>char_array[i]; //Reading the array elements from user } cout<<"\ndisplay the characters\n"; for(i=0; i<len; i++){//display elements using for loop cout<<"char_array["<<i<<"]:"<<char_array[i]; cout<<("\n"); } 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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.