Below is a C program which accepts a maximum of 10 characters from the user, stores them in an array and prints the array back.
/*Program to read n characters into an array and print them*/
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],num,i;
printf("Enter the number of elements:");
scanf("%d",&num);
if(num>10)
{
printf("No of elements cannot be more than 10. Try again..!");
}
else
{
/*Reading the array elements*/
printf("Enter the elements:\n");
for(i=0;i<num;i++)
{
fflush(stdin);
scanf("%c",&a[i]);
}
/*Printing the array elements*/
printf("Elements in the array are:\n");
for(i=0;i<num;i++)
printf("%c ",a[i]);
}
getch();
}
Sample input and output:
Enter the number of elements:5
Enter the elements:
a
f
u
w
k
Elements in the array are:
a f u w k
No comments:
Post a Comment