C language simulation exercises (6)

code:

#include<stdio.h>
#include<stdlib.h>

//1> Implement a function, print the multiplication formula table, specify the number of rows and columns of the formula table by yourself,
//input 9, output 9*9 formula table, Input 11, output 11*11 multiplication table.

void print_multipilication_table(int value){
int i = 1;
printf("%d*%d multiplication table:\n",value,value);
for(;i<=value;++i){
int j = 1;
for(;j<=i;++j){
printf("%2d * %2d = %2d ",i, j ,i*j);
}
printf("\n");
}
}
void text_print_multipilication_table() {
int value ;
printf("Please enter the n value of the multiplication table: "");
scanf("%d",&value);
print_multipilication_table(value);
}


//2> Use a function to exchange two numbers.
void swap(int* a,int* b){
int tmp = *a;
*a = *b;


void text_swap(){
int value1= 10;
int value2= 12;
printf("before swapping:\n");
printf("value1 = %d value2 = %d \n",value1,value2); //call swap Function, exchange the value of value1 and value2 //At this time, we should pay attention to the incoming value, which should be the parameter address value (passing address) swap(&value1,&value2); printf("After the exchange\n"); printf(" value1 = %d value2 = %d \n",value1,value2); } //3> Implement a function to determine whether the year is a leap year. int is_leap_year(int year){ //To judge whether the year is a leap year, we should judge whether it is divisible by 4, //but not divisible by 100, or divisible by 400, if it can be expressed as // a leap year, return 1, otherwise return 0 .if((year%4==0&&year%100!=0)||year%400==0){ return 1; } return 0; } void text_is_leap_year(){ int year; int ret; printf("






















scanf("%d",&year);
//Call the function to judge
ret = is_leap_year(year);
if(ret == 1){
printf("%d is a leap year!\n",year);
}else{
printf("%d is not a leap year!\n",year);
}
return;
}
//4> Implement a function to determine whether a number is prime.
//The function returns 1, indicating that the number is a prime number, otherwise it is not a prime number

int is_prime(int value){
int i =2;
for(;i<=value/2;++i){
if(value%i==0 ){
return 0;
}
}
if(i>=value/2){
return 1;
}
}
void text_is_prime(){
int value;
int ret;
printf("Please enter a judgment number: "");
scanf("% d",&value);
//Call the function to determine whether the number is a prime number
ret = is_prime(value);


}else{
printf("%d 不是素数!\n",value);
}
return;
}


int main(){
text_print_multipilication_table();
text_swap();
text_is_leap_year();
text_is_prime();
return 0; 
}

Test results:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772288&siteId=291194637