Given an array of random numbers, Write a c code to move all zeros to end of an array. An array contains only positive number.
For example -
Input
Output
// Move all zero at the end
How to Move all Zeros to End of an Array
Traverse an array and push all non-zero element in an array. Maintain the count. After traversing an array all non-zero element has been moved, now push all the zero at the end.
You can also check video tutorial at the end of this post for more clarity.
C program to reverse a number
Programming questions on strings
In this program, we have given an array of positive random numbers. We have to write a code to separate all zero's from non-zero element of an array.
C program to print factorial of a number using recursion
C program to print fibonacci series using recursion
Sorting algorithm and their time complexities
For example -
Input
int arr[] = {1,6,0,3,8,9,0,2};
Output
// Move all zero at the end
int arr[] = {1,6,3,8,9,2,0,0};
How to Move all Zeros to End of an Array
Traverse an array and push all non-zero element in an array. Maintain the count. After traversing an array all non-zero element has been moved, now push all the zero at the end.
You can also check video tutorial at the end of this post for more clarity.
C program to reverse a number
Programming questions on strings
C Program to Move all Zeros at The End of an Array
In this program, we have given an array of positive random numbers. We have to write a code to separate all zero's from non-zero element of an array.
#include <stdio.h>
int main() {
int arr[] = {1,6,0,3,8,9,0,2};
int i, count=0;
int n = 8;
/*
* Traverse an array,
* Push all non-zero element first
*/
for(i = 0; i < n; i++) {
if(arr[i] > 0){
arr[count++] = arr[i];
}
}
// At the end push all zero element
while(count < n){
arr[count++] = 0;
}
//Print the array values
for(i = 0; i < n; i++) {
printf(" %d ",arr[i]);
}
return 0;
}
C program to print factorial of a number using recursion
C program to print fibonacci series using recursion
Sorting algorithm and their time complexities


No comments:
Post a Comment