I want to calculate remainder of all the values inside nested loop with all the numbers inside the loop. for example
when

a=1 , b=1, c=remainder
a=2,  b=1, c=remainder
a=3,  b=1, c=remainder
a=1,  b=2, c=remainder
a=2,  b=2, c=remainder
a=3,  b=2, c=remainder
a=1,  b=3, c=remainder
a=2,  b=3, c=remainder
a=3,  b=3, c=remainder

and here is what i've coded.

int main(void)
{
         int a,b,c,d;
         for (a=1;a<4;a++)
         {
               for (b=1;b<4;b++)
               c=a%b;
               printf ("a=%d,b=%d,c=%d\n",a,b,c);
         }
getche ();
}

When i'm writing c=a%b over for (b=1;b<4;b++) nothing is happening and program is not running and when i am writing c=a%b under printf ("a=%d,b=%d,c=%d\n",a,b,c); the Output is

a=1, b=1, c=-28692
a=1, b=2, c=-28692
a=1, b=3, c=-28692
a=2, b=1, c=1
a=2, b=2, c=1
a=2, b=3, c=1
a=3, b=1, c=2
a=3, b=2, c=2
a=3, b=3, c=2

why it is showing the incorrect remainder

You've missed the braces { } round the 2 lines of code that should be in the inner loop so that the printf statement is only executed once for every iteration of the outer loop.

Hey Thanks...and how to calculate remainder of a and b only if b is one less than a ??
i mean when the values of a and b are same then the remainder must not be calculated ?

Just put an if statement round the contents of the inner loop testing the condition a != b

BTW the is little point doing the calculation for b == 1 because a % 1 == 0 for all a.

Hey Thanks...and how to calculate remainder of a and b only if b is one less than a ??
i mean when the values of a and b are same then the remainder must not be calculated ?

Put an if condition

if(/*check if b is less than a */)
{
      // Calculate the remainder
}
else
{
     // Do nothing
}

thanks alot,
can i get any help in this program to find first 20 prime numbers? ?

I'm stuck here!!! :-( , i want to exclude those numbers from the list which are giving 0 remainder.

int main(void)
{
int a,b,c;
for (a=1;a<21;a++)
{
for (b=21;b>2;b--)
if (b<a)
{
c=a%b;
if (c==0)
break;
else
printf ("%d",a);
}
}
getche ();
}

This is a basic algo for checking if a number is prime or not

Store the number in a

Loop: From i= 2 till i= sqrt(a)+1
          if(a%i==0)
              break
          else 
               Do nothing

if(i==sqrt(a)+1)
     This means that no number between 2 and sqrt(a) is a factor for a.   which means a is a prime number
else
      a is not a prime number

Now You can extend the above logic to solve your problem

i dont want to check the number is prime or not , i want to print the prime numbers

There is very little difference between those 2 questions .....
Think how can you use my explanation to solve your problem

i already have my program to check that the input number is prime or not and your program is for the specific number while i want to calculate first 20 primes.
and here is what i've made but it is not working,

int main(void)
{
int a,b,c;
for (a=1;a<21;a++)
{
for (b=21;b>2;b--)
if (b<a)
{
c=a%b;
if (c==0)
break;
else
printf ("%d",a);
}
}
getche ();
}

Your program is wrong
This is the O/P of your program

4
5
5
6
6
7
7
7
7
8
8
8
9
9
9
9
9
10
10
10
10
11
11
11
11
11
11
11
11
12
12
12
12
12
13
13
13
13
13
13
13
13
13
13
14
14
14
14
14
14
15
15
15
15
15
15
15
15
15
16
16
16
16
16
16
16
17
17
17
17
17
17
17
17
17
17
17
17
17
17
18
18
18
18
18
18
18
18
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
20
20
20
20
20
20
20
20
20

so, cant you make correction in my Program ??

Right ......
I have given you the logic .....Make the correction on your own

i really didn't get you.....we are at basic level and didn't learn so much c language, here is my program that tells wether the number is prime or non prime.
i've tried every thing to print only prime numbers but i am not able to do this :s

int main(void)
{
here set a b and c as integers
here i stored number in a
c=a;
{
Loop from a=c till a>1
here i applied the condition if a is less than c
{
b=c%a;
if (b==0)
break;
}
if (b==0)
printf ("non Prime.");
else
printf ("Prime number");
}
}

this programs works well to check the prime number but i don't understand how to print those numbers whose remainder is not equals to 0

i already have my program to check that the input number is prime or not and your program is for the specific number while i want to calculate first 20 primes.
and here is what i've made but it is not working,

int main(void)
{
int a,b,c;
for (a=1;a<21;a++)
{
for (b=21;b>2;b--)
if (b<a)
{
c=a%b;
if (c==0)
break;
else
printf ("%d",a);
}
}
getche ();
}

You want to print the prime numbers, however the test for primality is not divisible by any integer less than itself except 1. That means you have to check every single integer (not true actually but in your simplistic algorithm it is) before you can be sure a number is prime. That means your print statement must be outside the inner loop and when you have exited that loop you need to be able to tell if the loop completed or if the break happened. If the break happened the number is not prime.

Also note your end condition for inner loop is wrong, you never check to see if a is divisible by 2. I think you may find that 4 is incorrectly identified as prime.

You say you want the first 20 primes but this actually calculates the number of primes <= 20, there will certainly not be 20 of them. The end condition for the outer loop should be on the number of primes found so far.

commented: thankx..xufyan +0

Problem solved...Thanks alot :)

now , this program is printing all the prime numbers < 15 ,

but it is printing them more than one time...why ? every prime number is printed there but since 9 is not a prime number why it is printed ?

No it doesn't for instance it prints 9 which is definately not prime.

You still have your print statement inside your inner loop. it needs to be outside your inner loop.

Thanks alot, problem solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.