Move all zeros to the Start of an Array

Problem statement:- Program to Move all zeros to the Start of an Array.

Example:-

              Input : Given 

                                size=5

                                 arr[]=[4,0,7,0,0] 

             Output:

                        arr[]=[0,0,0,4,7]            

Data requirement:-

   Input Data:- size, arr

  Output Data:-arr

  Additional Data:- i, c

Program in C

Here is the source code of the C Program to Move all zeros to the Start of an Array.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i,j;
    printf("Enter the Element of the array:\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }

    int c=size-1;

    for(i=size-1;i>=0;i--)
    {
        if(arr[i]!=0)
        {
            arr[c]=arr[i];
            c--;
        }
    }
    for(i=c;i>=0;i--){
        arr[c]=0;
        c--;
    }
    printf("After Move all zeros to Start, Array is:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4 0 7 0 0
After Move all zeros to Start, Array is:0 0 0 4 7

Program in C++

Here is the source code of the C++ Program to Move all zeros to the Start of an Array.

Code:

#include<iostream>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,temp=0,j;
    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }
    int c=size-1;

    for(i=size-1;i>=0;i--)
    {
        if(arr[i]!=0)
        {
            arr[c]=arr[i];
            c--;
        }
    }
    for(i=c;i>=0;i--){
        arr[c]=0;
        c--;
    }
    cout<<"After Move all zeros to Start, Array is:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
}

Input/Output:
Enter the size of the array:6
Enter the Element of the array:
9 0 0 10 0 6
After Move all zeros to Start, Array is:0 0 0 9 10 6

Program in Java

Here is the source code of the Java Program to Move all zeros to the Start of an Array.

Code:

import java.util.Scanner;
public class MoveAllZeorsToStart {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,i;
     size=sc.nextInt();
     int arr[ ]=new int[size];
  System.out.println("Enter the Element of the array:");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
      int c=size-1;

      for(i=size-1;i>=0;i--)
      {
          if(arr[i]!=0)
          {
              arr[c]=arr[i];
              c--;
          }
      }
      for(i=c;i>=0;i--){
          arr[c]=0;
          c--;
      }
      System.out.println("After Move all zeros to Start, Array is:");
      for(i=0;i<size;i++)
      {
      System.out.print(arr[i]+" ");
      }
          sc.close();
}
}

Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
0 22 0 3 0
After Move all zeros to Start, Array is:
0 0 0 22

Program in Python

Here is the source code of the Python Program to Move all zeros to the Start of an Array.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

c=size-1
for i in range(size-1,-1,-1):
    if arr[i]!=0:
        arr[c]=arr[i]
        c-=1

for i in range(c,-1,-1):
        arr[c]=0
        c-=1

print("After Move all zeros to Start, Array is:")
print(arr)

Input/Output:
Enter the Element of the array:
5
0
6
0
After Move all zeros to Start, Array is:
[0, 0, 5, 6]


Post a Comment

0 Comments