Checking if Array Contains Consecutive Numbers

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");
}​

C – Program’s For Printing Patterns – 2.

Tags

​​C Program to print the below pattern in the console

—————–

          * 

         * * 

        * * * 

       * * * * 

      * * * * * 

     * * * * * * 

    * * * * * * * 

   * * * * * * * * 

  * * * * * * * * * 

 * * * * * * * * * *

——————

#include<stdio.h>
#include<stdlib.h>

 

void main()
{
    int i,j,k;
    int n=10;
    for(i=1;i<=10;i++)
    {
        for(k=n;k>0;k--)
        {
            printf(" ");
        }
        for(j=0;j<i;j++)
        {
            printf("* ");
        }
        printf("\n");
        n=n-1;
    }
}

 

Please comment if you have better approach.

C – Program’s For Printing Patterns – 1

Tags

 

C Program to print the below pattern in the console
—————–

*

**

***

****

*****

******

*******

********

*********

**********

——————————
#include<stdio.h>
#include<stdlib.h>

 

main()
{
    int i,j;
    for(i=1;i<=10;i++)
    {
        for(j=0;j<i;j++)
        {
            printf("*");
        }
        printf("\n");
    }
}