Ques:-25)  Given a number, write a program using while loop to reverse the digits of the number. For example, the number 12345 should be written as 54321. Also find the sum of all digits, even digits and odd digits.

Below is a C program that takes a number as input, reverses its digits, and calculates the sum of all digits, the sum of even digits, and the sum of odd digits using a while loop.

				
					#include"stdio.h"
#include"conio.h"
void main()
{
  long int num,revnum=0,rem=0,sum=0,ev_sum=0,od_sum=0;
  clrscr();
  printf("enter any number:");
  scanf("%ld",&num);
  printf("\nentered number is %ld",num);
  w hile(num>0)
  {
    rem=num%10;
    sum=sum+rem;
    if(rem%2==0)
      ev_sum=ev_sum+rem;
    else
      od_sum=od_sum+rem;
    revnum=(revnum*10)+rem;
    num=num/10;
  }
  printf("\nreverse number is %ld",revnum);
  printf("\nsum of all digits is %ld",sum);
  printf("\nsum of even digit is %ld and odd digit is %ld",ev_sum,od_sum);
  getch();
}

				
			

Output

Another Example

				
					#include <stdio.h>

int main() {
    int num, reversedNum = 0, remainder;
    int sumAll = 0, sumEven = 0, sumOdd = 0;

    // Requesting input from the user
    printf("Enter a number: ");
    scanf("%d", &num);

    int originalNum = num;

    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        sumAll += remainder;

        if (remainder % 2 == 0) {
            sumEven += remainder;
        } else {
            sumOdd += remainder;
        }

        num /= 10;
    }

    // Output results
    printf("Reversed number: %d\n", reversedNum);
    printf("Sum of all digits: %d\n", sumAll);
    printf("Sum of even digits: %d\n", sumEven);
    printf("Sum of odd digits: %d\n", sumOdd);

    return 0;
}

				
			

Explanation:

  1. Variable Initialization:

    • num: stores the original number input by the user.
    • reversedNum: stores the reversed number.
    • remainder: stores the current digit being processed.
    • sumAll, sumEven, sumOdd: store the sums of all digits, even digits, and odd digits respectively.
  2. Input Request:

    • The program requests a number from the user and stores it in num.
    • The original number is saved in originalNum for reference.
  3. While Loop:

    • The loop continues until num becomes 0.
    • In each iteration:
      • The last digit of num is obtained using num % 10 and stored in remainder.
      • This digit is then added to reversedNum after shifting reversedNum to the left by one digit.
      • The digit is also added to sumAll.
      • Depending on whether the digit is even or odd, it is added to sumEven or sumOdd.
      • num is then divided by 10 to remove the last digit.
  4. Output Results:

    • After the loop, the reversed number, the sum of all digits, the sum of even digits, and the sum of odd digits are printed.

Output:

Here is an example output of the program for different inputs:

  1. Example 1:

				
					Enter a number: 12345
Reversed number: 54321
Sum of all digits: 15
Sum of even digits: 6
Sum of odd digits: 9

				
			

Example 2:

				
					Enter a number: 24680
Reversed number: 08642
Sum of all digits: 20
Sum of even digits: 20
Sum of odd digits: 0


				
			

Example 3:

				
					Enter a number: 13579
Reversed number: 97531
Sum of all digits: 25
Sum of even digits: 0
Sum of odd digits: 25


				
			

To compile and run this program, you can use a C compiler such as gcc:

				
					gcc program.c -o program
./program



				
			

Replace program.c with the name of your file.

Scroll to Top