Thread: A problem in remainder

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    1

    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
    Last edited by Salem; 01-21-2019 at 08:55 PM. Reason: Use [code][/code] tags for C/C++

  2. #2
    Registered User
    Join Date
    Jan 2019
    Posts
    2
    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.

  3. #3
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    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);
        }
    }

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    955
    It's also important to describe what the code is supposed to do and what output you expect.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. remainder and floats
    By telmo_d in forum C Programming
    Replies: 15
    Last Post: 07-09-2015, 08:06 AM
  2. Need help with Remainder program
    By Gohmer in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2015, 09:42 PM
  3. how to get remainder of a value
    By seal in forum C Programming
    Replies: 2
    Last Post: 09-22-2005, 07:03 AM
  4. Finding a remainder when dividing
    By dustyd in forum C Programming
    Replies: 3
    Last Post: 01-22-2003, 09:31 PM
  5. Determining if a number has a remainder?
    By Captain Penguin in forum C Programming
    Replies: 3
    Last Post: 09-01-2001, 07:07 AM

Tags for this Thread