Write a program to 50 % reversing of given number in c

07 Sep 2022 Balmiki Mandal 0 C Programming

C Program: Reverse 50% of Given Number

"50% reversing of a given number" means reversing only the first half of the digits in the given number. For example, if the input number is 123456, the first half (123) would be reversed to get 321456. If the input number has an odd number of digits, the first half is rounded down. For example, if the input number is 12345, the first half (12) would be reversed to get 32145.

#include<stdio.h>
int main()
{
    int a[10],i,j,t,ele;
    ele=sizeof(a)/sizeof(a[0]);
    printf("Enter array:");
    for(i=0;i<ele;i++)
    scanf("%d",&a[i]);
    printf("Before:");
    for(i=0;i<ele;i++)
    printf(" %d",a[i]);
    printf("\n");
    // for(i=0,j=5;j<ele;i++,j++)
    for(i=0,j=5;i<ele/2;i++,j++)
    {
        t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
    printf("After:");
    for(i=0;i<ele;i++)
    printf(" %d",a[i]);
    printf("\n");
}

Output:

Enter array: 60 70 80 90 100 10 20 30 40 50

Before: 60 70 80 90 100 10 20 30 40 50

After:   10 20 30 40 50 60 70 80 90 100

Top Resources


[ Copy in a reverse manner in c ]

Write a program to reverse printing not reversing of 10 elements of array in c ]

[ Write a program to reverse the bits of a given number using for loop ]

[ Design a function to reverse the given string ]

[ Design a function to reverse the word in given string ]

[ write a program to copy one array element to another array element in reverse manner. ]

[ write a program to reverse adjacent elements of an array. ]

[ write a program to reverse half of the elements of an array. ]

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.