Search code examples
cmenu

Why the output is returning zero after one loop


I am a beginner at programming and I am learning C. I was given a task to create a menu driven program with following menu: Reverse a number, Sum of digits, sum of 1st and last number. The program was executed without errors but the output is returning zero after running it second time


**My Code is: **

#include<stdio.h>
void main()
{
  int num, temp, rev, rem, sum, i, choice, last, first, yes_or_no;
  printf("Enter the number: ");
  scanf("%d", &num);
  do
  {
      printf("\nMenu");
      printf("\n1.Reverse a number.\n2.Sum of digits of a number.\n3.Sum of first and last numbers.");
      printf("\nEnter your choice: ");
      scanf("%d", &choice);
      switch (choice)
      {
      case 1:
         //Reverse the given number
         temp=num;
         rev=0;
         while (num>0)
         {
           rem=num%10;
           rev=rev*10+rem;
           num=num/10;
         }
         printf("\nThe reverse of %d is %d", temp, rev);
         break;
      case 2:
         //Sum of digits of a number
         temp=num;
         sum=0;
         while (num>0)
         {
           rem=num%10;
           sum=sum+rem;
           num=num/10;
         }
         printf("\nThe sum of digits of %d is %d", temp, sum);
         break;
      case 3:
         //Sum of first and last number
         temp=num;
         last=num%10;
         while(num>=10)
         {
           num=num/10;
         }
         first=num;
         sum=first+last;
         printf("\nThe sum of first and last digit of %d is %d", temp, sum);
         break;
      default:
         printf("\nOption does not exist");
         break;
      }
   } 
   while (choice<=3);  
}

Here is my output

Enter the number: 67

Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 2

The sum of digits of 67 is 13
Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 3

The sum of first and last digit of 0 is 0
Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 2

The sum of digits of 0 is 0
Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 1

The reverse of 0 is 0

Why is this happening???

I expected that if i enter num=67 then for

  • first case it returns 76,
  • second case it returns 13,
  • third case also 13

Solution

  • You are changing num inside the loop so after the first iteration num is zero. For instance, it's done by this code:

         while (num>0)
         {
           rem=num%10;
           rev=rev*10+rem;
           num=num/10;
         }
    

    A simple fix - save a copy of num and restore it at the end of the loop:

      do
      {
          int saved_num = num;  // Add this to save the value of num before changing it
    
          ....
          Your current code which change num
          ....
    
          num = saved_num;      // Add this to restore num before next iteration
      } 
      while (choice<=3);