Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#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 have written this normal code..but how i can write this code using array..please help..
Posted
Comments
Richard MacCutchan 22-Sep-14 3:34am    
You cannot write that code using an array, as that makes no sense. What data would you expect to hold in the array, and how would you process it? As it stands your if statement will always be true, since the value of i never gets above 10.
Abhinav Chaudhary 22-Sep-14 4:35am    
Actually i want to put the numbers as an array element....they should give 1st 10 number divisible by 15 and 27 in array element
Abhinav Chaudhary 22-Sep-14 4:42am    
I just want to show to output in array element

C#
#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();
}
 
Share this answer
 
Comments
Abhinav Chaudhary 23-Sep-14 8:49am    
Thank you!...
Olivier Levrey 23-Sep-14 8:58am    
I voted 4 because this code will crash sooner or later. You declared an array of 10 elements but n goes from 0 to 10, so it makes 11 elements.
The statement x[n] = i; will have unexpected results when n=10.
Abhinav Chaudhary 23-Sep-14 9:12am    
yes you are right....
Is it what you want?

C++
#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();
}
 
Share this answer
 
Comments
Abhinav Chaudhary 23-Sep-14 8:50am    
Yes..Thank You!..
before going to program first say what do you expect the out of program...
 
Share this answer
 
Comments
Abhinav Chaudhary 22-Sep-14 4:35am    
Actually i want to put the numbers as an array element....they should give 1st 10 number divisible by 15 and 27 in array element
Abhinav Chaudhary 22-Sep-14 4:43am    
I want to put the output in array...and show them as an array element

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900