查詢座標所在象限的C程式。


問題

編寫一個程式來查詢給定座標所在的象限。

使用者必須在執行時輸入座標,我們需要找到這些座標所在的象限。

解決方案

  • 如果兩個數字都是正數,則顯示第一象限。
Example: Input =2, 3
Output = 1st quadrant
  • 如果第一個數字為負,第二個數字為正,則顯示第二象限。
Example: Input = -4, 3
Output= 2nd quadrant
  • 如果第一個數字為負,第二個數字也為負,則顯示第三象限。
Example: Input = -5,-7
Output= 3rd quadrant
  • 如果第一個數字為正,第二個數字為負,則顯示第四象限。
Example: Input = 3,-5
Output = 4th quadrant

示例

以下是查詢給定座標所在象限的 C 程式:

 線上演示

#include <stdio.h>
int main(){
   int a,b;
   printf("enter two coordinates:");
   scanf("%d %d",&a,&b);
   if(a > 0 && b > 0)
      printf("1st Quadrant");
   else if(a < 0 && b > 0)
      printf("2nd Quadrant");
   else if(a < 0 && b < 0)
      printf("3rd Quadrant");
   else if(a > 0 && b < 0)
      printf("4th Quadrant");
   else
      printf("Origin");
   return 0;
}

輸出

執行上述程式後,會產生以下輸出:

Run 1:
enter two coordinates:-4 6
2nd Quadrant
Run 2:
enter two coordinates:-5 -3
3rd Quadrant

更新於:2021年3月26日

1K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.