使用 C 程式語言區分模和除法?
取模 − 表示為 % 運算子。
返回整數除法的餘數。
除法 − 表示為 / 運算子。
返回除法的商。
程式 1
#include<stdio.h>
int main(){
int a,b,c;
printf("enter a,b,c values:");
scanf("%d%d%d,&a,&b,&c);
printf("a/b=%d a%b=%d
",a/b,a%b);
printf("(a+10)%b=%d (a+10)/b=%d
",(a+10)%b,(a+10)/b);
}輸出
enter a,b,c values:2 4 6 a/b=0 ab=2 (a+10)b=0 (a+10)/b=3
程式 2
應用指標變數進行取模和除法運算 −
#include<stdio.h>
void main(){
//Declaring pointers and variables//
int num1,num2;
int *p1,*p2;
p1=&num1;
p2=&num2;
int div,mod;
//Reading User I/p//
printf("Enter the values of num1 & num2: ");
scanf("%d,%d",&num1,&num2);
div=*p1/ *p2;
mod=*p1%*p2;
//Printing O/p//
printf("Division value = %d
",div);
printf("Modulus value = %d
",mod);
}輸出
Enter the values of num1 & num2: 30,20 Division value = 1 Modulus value = 10
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP