sabertooth09

Assignment_P_GCD_FACT

Oct 9th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #define ll  long long int
  4.  
  5. ll fact(ll n)
  6. {
  7.     ll i,s=1;
  8.     for(i=2; i<=n; i++)
  9.         s*=i;
  10.     return s;
  11. }
  12. ll prime(ll n)
  13. {
  14.     ll i,j;
  15.     if(n==1)
  16.         return 0;
  17.     else if(n%2==0 && n!=2)
  18.         return 0;
  19.     else
  20.     {
  21.         for(i=3; i*i<=n; i+=2)
  22.         {
  23.             if(n%i==0 && n!=i)
  24.                 return 0;
  25.         }
  26.     }
  27.     return 1;
  28. }
  29.  
  30. ll gcd(ll a,ll b)
  31. {
  32.     while(a!=b)
  33.     {
  34.         if(a>b)
  35.             a-=b;
  36.         else
  37.             b-=a;
  38.     }
  39.     return a;
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45.     ll m,n,q;
  46.     char t;
  47.     printf("============================================================================\n================================ | WELCOME | ===============================\n============================================================================\n");
  48.     printf("1 | Type 'F/f' and press enter for Calculating factorial\n2 | Type 'P/p' and press enter to check if the number is prime\n3 | Type 'G/g' and press enter for Calculating GCD of 2 numbers\n");
  49.     printf("5 | Type anything and press enter to exit\n============================================================================\n");
  50.     while(1)
  51.     {
  52.         scanf("%c",&t);
  53.         if(t=='F' || t=='f')
  54.         {
  55.             printf("Give a input for Calculating Factorial\n");
  56.             scanf("%lld",&n);
  57.             printf("Factorial Of %lld is : %lld\n",n,fact(n));
  58.         }
  59.         else if(t=='P' || t=='p')
  60.         {
  61.             printf("Give a input for checking If it is prime\n");
  62.             scanf("%lld",&n);
  63.             if(prime(n))
  64.                 printf("%lld Is a Prime Number\n",n);
  65.             else
  66.                 printf("%lld is not a Prime number\n",n);
  67.         }
  68.         else if(t=='G' || t=='g')
  69.         {
  70.             printf("Give 2 inputs for both numbers\n");
  71.             scanf("%lld %lld",&n,&m);
  72.             printf("GCD of %lld %lld is : %lld\n",n,m,gcd(n,m));
  73.         }
  74.         else
  75.         {
  76.             printf("HAPPY CODING :)\n");
  77.             break;
  78.         }
  79.         getchar();
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment