Simple C Programs With Output

C programs

This post include list of basic/simple C programming examples with output.If you are beginner than it help to learn how to write a logic in simple way along with output to achieve a difficult task in simple ways.

This basic simple C programs help beginners to practice and learn for interviews.This post include programs on different topic such as string,pattern,number,array etc.

This post also include basic C programs examples PDF if you want than download C programming examples with output PDF from download section or Click here.

Numeric Array Programs

This section contains demonstration of C programming examples on Arrays. You will learn how to declare, initialize and, access array elements with the help of demonstrated examples.All examples along with its explanation are define in the given post.

These examples can be simple C programs or advanced C programs. So, they are suitable for any level of user to understand very easily.List of all C program based on numeric array are listed below.

1.Write a program to convert the given decimal number in to binary number.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int number=0,i=0,Binary[MAXSIZE],index=0;
    clrscr();
    printf("\n Enter the decimal number you want to convert : ");
    scanf("%d",&number);
    while (number > 0)
    {
        Binary[index] = number % 2;
        number = number / 2;
        index++;
    }
    printf("\n Binary of given decimal number is : ");
    for (i = index - 1; i >= 0; i--)
    {
        printf("%d",Binary[i]);
    }
    getch();
}

2.Write a program to delete an element from array.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int n,a[MAXSIZE],i=0,k=0,position;
    clrscr();
    printf("\n Enter size of array \n");
    scanf("%d",&n);
    printf("\n Enter array elements \n");
    for (i = 0; i < n; i++) {
        scanf("%d",&a[i]);
    }
    printf("\n\n Array elements are : \n");
    for (i = 0; i < n; i++) {
        printf("%d\t",a[i]);
    }
    printf("\n\n Enter the position of element to be deleted \n");
    scanf("%d",&position);
    position = position - 1;
    for (i = 0; i < n; i++) {
        if (i != position) {
            a[k] = a[i];
            k++;
        }
    }
    printf("\n Array elements are : \n");
    for (i = 0; i < k; i++) {
        printf("%d\t",a[i]);
    }
    getch();
}

3.Write a program to insert an element in array.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int n,a[MAXSIZE],i=0,position,element;
    clrscr();
    printf("\n Enter size of array \n");
    scanf("%d",&n);
    printf("\n Enter array elements \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\n\n Array elements are : \n");
    for (i = 0; i < n; i++)
    {
        printf("%d\t",a[i]);
    }
    printf("\n\n Enter the element to be inserted ");
    scanf("%d",&element);
    printf("\n\n Enter the position where %d is to be inserted ",element);
    scanf("%d",&position);
    position = position - 1;
    for (i = n - 1; i >= position; i--)
    {
        a[i + 1] = a[i];
    }
    a[position] = element;
    printf("\n Array elements are : \n");
    for (i = 0; i < n + 1; i++)
    {
        printf("%d\t",a[i]);
    }
    getch();
}

4.Write a program to remove consecutive duplicates from an array.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int n,a[MAXSIZE],i=0,k=1,element;
    clrscr();
    printf("\n Enter size of array \n");
    scanf("%d",&n);
    printf("\n Enter array elements \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
    }
    element = a[0];
    for (i = 1; i < n; i++) {
        if (a[i] != element) {
            a[k] = a[i];
            element = a[i];
            k++;
        }
    }
    printf("\n Array without consecutive duplicate \n");
    for (i = 0; i < k; i++) {
        printf("%d\t",a[i]);
    }
    printf("\n");
    getch();
}

5.Write a program to print array without duplicate numbers.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int n,a[MAXSIZE],b[MAXSIZE],i=0,j=0,k=0,flag=1;
    clrscr();
    printf("\n Enter size of array \n");
    scanf("%d",&n);
    printf("\n Enter array elements \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < k; j++) {
            if (a[i] == b[j]) {
                flag = 0;
                break;
            }
            else {
                flag = 1;
            }
        }
        if (flag == 1) {
            b[k] = a[i];
            k++;
        }
    }
    printf("\n Array without duplicates : \n");
    for (i = 0; i < k; i++) {
        printf("%d\t",b[i]);
    }
    printf("\n");
    getch();
}

6.Write a program to search an element in an array.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int n,a[MAXSIZE],location[MAXSIZE],i=0,k=0,ele;
    clrscr();
    printf("\n Enter size of array \n");
    scanf("%d",&n);
    printf("\n Enter array elements \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\n Enter an element to be searched ");
    scanf("%d",&ele);
    for (i = 0; i < n; i++)
    {
        if (a[i] == ele)
        {
            location[k] = (i + 1);
            k++;
        }
    }
    if (k > 0) {
        printf("\n Element %d is present at positions :",ele);
        for(i = 0; i < k; i++)
        {
            if(i < k-1) {
                printf("%d,",location[i]);
            }
            else
            {
                printf("%d",location[i]);
            }
        }
    }
    else {
        printf("\n Element %d not found",ele);
    }
    getch();
}

7.Write a program to find series of factorial number.

#include<stdio.h>
#include<conio.h>
#define MAX 500
int multiply(int x, int res[], int res_size);
void factorial(int n)
{
    int res[MAX],i=0,x=0,res_size = 1;;
    res[0] = 1;
    for (x=2; x<=n; x++)
    {
        res_size = multiply(x, res, res_size);
    }
    for (i=res_size-1; i>=0; i--)
    {
        printf("%d",res[i]);
    }
}
int multiply(int x, int res[], int res_size)
{
    int carry = 0,i=0,prod;
    for (i=0; i<res_size; i++)
    {
        prod = res[i] * x + carry;
        res[i] = prod % 10;
        carry  = prod/10;
    }
    while (carry)
    {
        res[res_size] = carry%10;
        carry = carry/10;
        res_size++;
    }
    return res_size;
}
void main()
{
    int n,i=0;
    clrscr();
    printf("\n Enter a number to which you want to print factorials of every number : ");
    scanf("%d",&n);
    printf("Factorial of every number till given number is \n");
    for(i=1; i<=n; i++)
    {
        factorial(i);
        printf("\n");
    }
    getch();
}

8.Write a program to sort an array in descending order.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int n,i=0,j=0,a[MAXSIZE],t;
    clrscr();
    printf("\n Enter size of array \n");
    scanf("%d",&n);
    printf("\n Enter array elements \n");
    for (i = 0; i < n; i++) {
        scanf("%d",&a[i]);
    }
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (a[i] < a[j]) {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    }
    printf("\n Array elements in descending order: \n");
    for (i = 0; i < n; i++) {
        printf("%d\t",a[i]);
    }
    getch();
}

9.Write a program to sort an array in ascending order.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
    int n,i=0,j=0,a[MAXSIZE],t;
    clrscr();
    printf("\n Enter size of array \n");
    scanf("%d",&n);
    printf("\n Enter array elements \n");
    for (i = 0; i < n; i++) {
        scanf("%d",&a[i]);
    }
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (a[i] > a[j]) {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    }
    printf("\n Array elements in ascending order: \n");
    for (i = 0; i < n; i++) {
        printf("%d\t",a[i]);
    }
    getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *