Header Ads

21 - WAP to Read Integer and Print it without using 'scanf' operator in C

This Program reads the Value of a Integer and prints the same, without using the scanf() function / operator.

//this program is to display the  output without using printf function
#include 
int main()
{
 //Declaring variables
 char ch, choice, sign;
 int number, flag;
 do
 {
  number = 0;
  flag = 0;
  //reading the number from the user
  printf ("Enter the Number : ");
  do
  {
   ch = getchar();
   // checking for sign
   if (flag == 0)
   {
    sign = ch;
    flag = 1;
   }
   //checking for new line
   if (ch != '\n' )
   {
    //checking for negative sign
    if ( ch != '-' )
    {
     number = ( number * 10 ) + ( ch - 48 );
    }
   }
   //this is to run the loop again
  }while ( ch != '\n' );
  //for negative sign this loop will execute
  if (sign == '-')
  {
   printf ("The Number Entered is : '%d' \n", -number);
  }
  //for positive number this loop will run
  else
  {
   printf ("The Number Entered is : '%d'\n", number);
  }
  printf ("\n");
  //asking the user to continue or not
  printf ("\nTo Continue press ['Y'/'n'] : ");
  scanf ("%c", &choice);
  getchar();
  //to run the loop again
 }while ( choice == 'y' || choice == 'Y');
 return 0;
}

Program Output: 

No comments