Tags
Programm to find the entered series of five numbers are consecutive or not
eg; Input-> 5,6,7,8,9 or 100,101,102,103,104 Output-> Array has consecutive elements
eg: Input-> 5,6,7,8,4 or Output-> Array Dont have the consecutive elements
Using While loop
void main()
{
int a[5];
int n=5;
int i,j;
printf("Enter the values:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("*******OUTPUT********\n");
i=0;
j=1;
int T=0;
while(i<(n-1))
{
while(j<n)
{
if((a[j]==(a[i]+1))|| (a[j]==(a[i]-1)))
{
T++;
j++;
}
break;
}
i++;
}
if(T==(n-1))
{
printf("Array Has CONSECUTIVE ELEMENTS");
}
else
{
printf("Array Dont Have CONSECUTIVE elements");
}
}
Using for loop
void main()
{
int a[5];
int n=5;
int i;
printf("Enter the values:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("*******OUTPUT********\n");
i=0;
for (int i = 0; i< n-1; i++) {
if ((a[i+1] == a[i]+1) || (a[i+1] == a[i]-1)) {
}
else
{
printf("Array Dont Have CONSECUTIVE elements\n");
return 0;
}
}
printf("Array Has CONSECUTIVE ELEMENTS\n");
}