Advertisement
inhuman_Arif

Ques 1

Oct 6th, 2021
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.     char arr[8];
  7.     scanf("%s",arr); //take input
  8.     int sign=1, ind=0, num=0;
  9.     //check for negative number
  10.     if(arr[0]=='-')
  11.     {
  12.         sign = -1;
  13.         ind = 1;
  14.     }
  15.     //convert char array into integer
  16.     while(arr[ind]!='\0')
  17.     {
  18.         if(arr[ind]>='0' && arr[ind]<='9')
  19.             num = num*10+arr[ind]-'0';
  20.         else
  21.             break;
  22.         ind++;
  23.     }
  24.     //if input was given negative it'll turn into negative number
  25.     num*=sign;
  26.     printf("%d\n",num); //print output
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement