1、显示所有的小于输入的整数的所有2的乘方
while语句:
#include <stdio.h>
int main(void)
{
int i = 2, j;
puts("please input an integer.");
printf("j = "); scanf("%d", &j);
while (i <= j)
{
printf("%d ", i);
i *= 2;
}
putchar('\n');
return 0;
}
for语句:
#include <stdio.h>
int main(void)
{
int i, j;
puts("please input an integer.");
printf("j = "); scanf("%d", &j);
for(i = 2; i <= j; i *= 2)
{
printf("%d ", i);
}
putchar('\n');
return 0;
}
do语句
#include <stdio.h>
int main(void)
{
int i = 2, j;
puts("please input an integer.");
printf("j = "); scanf("%d", &j);
do
{
printf("%d ", i);
i *= 2;
}
while(i <= j);
putchar('\n');
return 0;
}
2、逆向输出输入的正整数(直接输出、保存新变量后输出)
while语句:
直接输出
#include <stdio.h>
int main(void)
{
int i;
puts("please input an integer.");
do
{
printf("i = "); scanf("%d", &i);
if(i <= 0)
puts("the range of i is > 0");
}
while(i <= 0);
while(i > 0)
{
printf("%d", i % 10);
i /= 10;
}
putchar('\n');
return 0;
}
保存变量后输出:
#include <stdio.h>
int main(void)
{
int i, j = 0;
puts("please input an integer.");
do
{
printf("i = "); scanf("%d", &i);
if(i <= 0)
puts("the range of i is > 0");
}
while(i <= 0);
while(i > 0)
{
j = j * 10 + i % 10;
i /= 10;
}
printf("%d\n", j);
return 0;
}
2、for语句
直接输出
#include <stdio.h>
int main(void)
{
int i;
puts("please input an integer.");
do
{
printf("i = "); scanf("%d", &i);
if(i <= 0)
puts("the range of is > 0 ");
}
while(i <= 0);
for(i; i > 0; i /= 10)
{
printf("%d", i % 10);
}
putchar('\n');
return 0;
}
保存变量输出
#include <stdio.h>
int main(void)
{
int i, j = 0;
puts("please input an integer.");
do
{
printf("i = "); scanf("%d", &i);
if (i <= 0)
puts("the range is > 0");
}
while(i <= 0);
for(i; i > 0; i /= 10)
{
j = j * 10 + i % 10;
}
printf("%d\n", j);
return 0;
}
3、do语句
直接输出
#include <stdio.h>
int main(void)
{
int i;
puts("please input an integer.");
do
{
printf("i = "); scanf("%d", &i);
if(i <= 0)
puts("the range is > 0");
}
while(i <= 0);
do
{
printf("%d", i % 10);
i /= 10;
}
while(i > 0);
putchar('\n');
return 0;
}
保存变量后输出:
#include <stdio.h>
int main(void)
{
int i, j = 0;
puts("please input an integer.");
do
{
printf("i = "); scanf("%d", &i);
if (i <= 0)
puts("the range is > 0");
}
while(i <= 0);
do
{
j = j * 10 + i % 10;
i /= 10;
}
while(i > 0);
printf("%d\n", j);
return 0;
}