Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program to fill and print an array of characters from user in C++

Java program to fill and print an array of characters

Posted on January 15, 2022

Table of Contents

  • Java program to fill and print an array of characters
    • Code to fill and print Characters of an array
      • Code to  input and print characters of an array using for loop
      • Code to  input and print characters of an array using while loop
      • Code to  input and print characters of an array using do-while loop

Java program to fill and print an array of characters

In this article, we will discuss the concept of Java program to fill and print an array of characters

In this post, we are going to learn how to write a program to fill and print array of characters (Character elements in single dimensional array)  using  for, while and do-while  loop in Java language

Code to fill and print Characters of an array

Code to  input and print characters of an array using for loop

In this code, we are going to learn how to fill and print character elements (user input) in single dimensional array using for loop in Java language

Program 1

//read and Print character of an array using for loop
import java.util.Scanner;
class Disp_Array_Of_Char_For{
public static void main (String args[]){
  Scanner sc=new Scanner(System.in);
  //Scanner class for user input
  
System.out.println("Enter array length: ");
int len=sc.nextInt();
char[] charArr=new char[len]; 
//Declaring a character Array 
  System.out.println("\n*** Read Characters ***");
System.out.println("Enter the "+len+" Characters");
for(int i=0; i<len; i++){
charArr[i]=sc.next().charAt(0);	
//Reding the input(characters) from the user
}
System.out.println("\n*** Print Characters ***");
System.out.println("Entered characters are:");
for(int j=0; j<len; j++){
System.out.println(charArr[j]);	
}//display all entered characters
}
}

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

Enter Array length
4

Enter the 4 characters
C
o
d
e

*** Print Characters ***
Entered characters are:
C
o
d
e


Code to  input and print characters of an array using while loop

In this code, we are going to learn how to fill and print character elements (user input) in single dimensional array using while loop in Java language

Program 2

//read and Print character of an array using while loop
import java.util.Scanner;
class Disp_Array_Of_Char_While{
public static void main (String args[]){
  Scanner sc=new Scanner(System.in);
  //Scanner class for user input
  
System.out.println("Enter array length: ");
int len=sc.nextInt();
char[] charArr=new char[len]; 
//Declaring a character Array 
  System.out.println("\n*** Read Characters ***");
System.out.println("Enter the "+len+" Characters");
int i=0; 
while(i<len){
charArr[i]=sc.next().charAt(0);	
//Reding the input(characters) from the user
 i++;
}
System.out.println("\n*** Print Characters ***");
System.out.println("Entered characters are:");
int j=0; 
while(j<len){
System.out.println(charArr[j]);	
 j++;
}//display all entered characters
}
}

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

Enter Array length
6

*** Read Characters ***
Enter the 6 characters
L
e
n
g
t
h

*** Print Characters ***
Entered characters are:
L
e
n
g
t
h


Code to  input and print characters of an array using do-while loop

In this code, we are going to learn how to fill and print character elements (user input) in single dimensional array using do-while loop in Java language

Program 3

//read and Print character of an array using do-while loop
import java.util.Scanner;
class Disp_Array_Of_Char_DoWhile{
public static void main (String args[]){
  Scanner sc=new Scanner(System.in);
  //Scanner class for user input
  
System.out.println("Enter array length: ");
int len=sc.nextInt();
char[] charArr=new char[len]; 
//Declaring a character Array 
  System.out.println("\n*** Read Characters ***");
System.out.println("Enter the "+len+" Characters");
int i=0; 
do{
charArr[i]=sc.next().charAt(0);	
//Reding the input(characters) from the user
 i++;
}while(i<len);
System.out.println("\n*** Print Characters ***");
System.out.println("Entered characters are:");
int j=0; 
do{
System.out.println(charArr[j]);	
 j++;
}while(j<len);
  //display all entered characters
}
}

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

Enter Array length
6

*** Read Characters ***
Enter the 6 characters
S
c
h
o
o
l


*** Print Characters ***
Entered characters are:
S
c
h
o
o
l







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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes