Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

C Program to Move All Zeros to the End of the Array

C Program to Move Zeros to End and Sort Non-Zero Elements

✅ C Program to Move Zeros to End and Sort Non-Zero Elements

  
  /*
 *  A chocolate factory is packing chocolates into the packets. The chocolate packets here 
 * represent an array of N number of integer values. The task is to find the empty packets(0) of 
 * chocolate and push it to the end of the conveyor belt(array). Click here to see solution
Example 1 : N=8 and arr = [4,5,0,1,9,0,5,0].

There are 3 empty packets in the given set. These 3 empty packets represented as O should be pushed towards the end of the array
 */
#include <stdio.h>

int main() {
    int num;
    printf("Enter the size of the array:\n");
    scanf("%d",&num);

    if(num <=0) {
        printf("!Invalid array size\n");
        return 0;
    }

    int a[num];
    printf("Enter %d elements in array:\n", num);
    for(int i=0; i<num; i++) {
        scanf("%d", &a[i]);
    }

    // Step 1: Move all zeros to the end
    int j = 0;
    for(int i=0; i<num; i++) {
        if(a[i] != 0) {
            a[j] = a[i];
            j++;
        }
    }
    for(int i=j; i<num; i++) {
        a[i] = 0;
    }

    // Step 2: Sort only non-zero elements
    for(int i=0; i<j-1; i++) {
        for(int k=0; k<j-i-1; k++) {
            if(a[k] > a[k+1]) {
                int temp = a[k];
                a[k] = a[k+1];
                a[k+1] = temp;
            }
        }
    }

    printf("After moving zeros to the end and sorting:\n");
    for(int i=0; i<num; i++) {
        printf("%d ", a[i]);
    }

    return 0;
}
  

πŸ“˜ Explanation:

This program first moves all zeros in the array to the end and then sorts only the non-zero elements.

Steps:

  • Take input for array size and elements.
  • Move non-zero elements to the front using index j.
  • Fill the remaining positions with zeros.
  • Sort only the first j non-zero elements using bubble sort.

🧾 Sample Output:

Enter the size of the array:
6
Enter 6 elements in array:
4 0 2 0 5 1
After moving zeros to the end and sorting:
1 2 4 5 0 0
  

πŸ”‘ Keywords:

C program move zeros, sort non-zero elements, array manipulation, beginner C programs, bubble sort logic

πŸ“Œ Hashtags:

#CProgramming #ArrayLogic #MoveZeros #Sorting #InterviewPrep

πŸ” Search Description:

Learn how to move all zeros to the end of an array and sort non-zero elements in C. Includes clear explanation and sample output for beginners and interview preparation.

Comments

Popular Posts

πŸŒ™