C基础(6-10)

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

int IsPrime(int num)
{
                 int i= 2;

                 if(num ==1)  //1既不是质数也不是合数
                                                 return 0;
                 for(;i<= num /2;i++)   //或者是for(;i<num;i++)
                {
                                 if(num %i==0)  //not the Prime
                                                 return 0;
                }
                 return 1;
}
void test()
{
                 int j=1;
                 int count=0;

                 for(;j<=100;j++)
                {
                                 if(IsPrime(j)==1)
                                {
                                                count++;
                                                printf( "%d ",j);
                                }
                }
                printf( "\nthe total of Prime in 1-100 is: %d\n" ,count);
}
int main()
{
                test();
                system( "pause");
                 return 0;
}


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

void test()
{
                 int a=0, b=0;
                 int i=0, j=0;

                scanf( "%d%d", &a, &b);
                 if(a<b)  //swap the num,to be sure a is the max num
                {
                                i=a;
                                a=b;
                                b=i;
                }
                 while(a%b!=0)
                {
                                j=a%b;
                                a=b;
                                b=j;  //余数保存在b中
                }

                printf( "the greatest common divisor is: %d\n" , b);
}
int main()
{
                test();

                system( "pause");
                 return 0;
}


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

void test()
{
                 int a=0, b=0;
                 int i=1;

                scanf( "%d%d", &a, &b);
                 while(!(i%a==0 && i%b==0))
                {
                                i++;
                }
                printf( "least common divisor is: %d\n" , i);

                 /*for(i=1;;i++)
                {
                                if(i%a==0&&i%b==0)
                                {
                                                printf("least common divisor is: %d\n", i);
                                                break;
                                }
                }*/
}
int main()
{
                test();
                system( "pause");
                 return 0;
}


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

void test()
{
                 int a=0, b=0;
                 int c=0;

                scanf( "%d%d", &a, &b);
                printf( "a=%d, b=%d\n", a, b);
                c= a;
                a= b;
                b= c;

                printf( "a=%d, b=%d\n", a, b);
}
int main()
{
                test();
                system( "pause");
                 return 0;
}



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

void swap(int * i, int* j)
{
                 int tmp= *i ;
                * i= *j ;
                * j= tmp;
}
void test()
{
                 int a=0, b=0;

                scanf( "%d%d", &a, &b);
                printf( "a=%d, b=%d\n", a, b);
                swap(&a, &b);

                printf( "a=%d, b=%d\n", a, b);
}
int main()
{
                test();
                system( "pause");
                 return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值