如何编写数组元素代码来查找可分数 [英] how to write array element code to find divisible number
本文介绍了如何编写数组元素代码来查找可分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int n=0;
unsigned int i=0;
clrscr();
while(n<=10)
{
if(i%15==0 && i%27==0)
{
printf("%d\n",i);
++n;
}
++i;
} getch();
}
i写了这个正常的代码..但我怎么能用array..please帮助编写这段代码..
i have written this normal code..but how i can write this code using array..please help..
推荐答案
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int n=0;
unsigned int i=0,x[10];
clrscr();
while(n<=10)
{
if(i%15==0 && i%27==0)
{
x[n]=i; //i value is saved in array of x[n]
++n;
}
++i;
}
//print array elements
for(n=1;n<=10;n++)
printf("\t %d",x[n]);
getch();
}
这是你想要的吗?
Is it what you want?
#include<stdio.h>
#include<conio.h>
//define the constant as a macro instead of hard-coding it
#define MAX_N 10
void main()
{
unsigned int n = 0;
unsigned int i = 0;
//the array will contain MAX_N + 1 elements
unsigned int arr[MAX_N + 1];
clrscr();
while (n <= MAX_N)
{
if(i % 15 == 0 && i % 27 == 0)
{
//store the result in the array at the current position
arr[n] = i;
++n;
}
++i;
}
//prints the array
for (i = 0; i <= MAX_N; i++)
printf("%d\n", arr[i]);
getch();
}
在开始编程之前先说出你对程序外的期望......
before going to program first say what do you expect the out of program...
这篇关于如何编写数组元素代码来查找可分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文