-
A problem in remainder
Hello, I am a student and a beginner learning c.
I need help where I did wrong in the following code
Code:
#include <stdio.h>
int main(void){ int s,j,i,r;
printf("Enter an integer: "); scanf("%d",&i);
s=i-1; int n[s];
for(j=1,s=0;s<i-1;j++,s++) { n[s]=j+1; r=n[s]%2; printf("n[%d]remainder2=%d \n",n[s]%2,r); }}
I am getting the output:
Code:
Enter an integer: 9
n[0]remainder2=0
n[1]remainder2=1
n[0]remainder2=0
n[1]remainder2=1
n[0]remainder2=0
n[1]remainder2=1
n[0]remainder2=0
n[1]remainder2=1
In the loop after executing two times, the same process repeats.
Thanks for the help
-
It's difficult to tell what you're trying to do here. Please use descriptive variable names (instead of s,j,i,r) and format your code properly.
-
Hello newstein, in the future post you code between [code][/code] tags and see that it maintains indentation from your editor, i.e.
Code:
#include <stdio.h>
int main(void)
{
int s, j, i, r;
printf("Enter an integer: ");
scanf("%d",&i);
s = i - 1;
int n[s];
for (j = 1, s = 0; s < i - 1; j++, s++) {
n[s] = j + 1;
r = n[s] % 2;
printf("n[%d]remainder2=%d \n", n[s]%2, r);
}
}
-
It's also important to describe what the code is supposed to do and what output you expect.