## Count The Number Of Solutions

Discuss Computer Science and Programming related problems

Moderators: bristy1588, Labib

Fahim Shahriar
Posts: 138
Joined: Sun Dec 18, 2011 12:53 pm

### Count The Number Of Solutions

Suppose, I want to find the number of solutions of this equation \$a+b+c+d=7\$ ; where \$a,b,c,d\$ are natural numbers.

To find the solutions I wrote the following code.

Code: Select all

``````#include <stdio.h>

int main ()
{
int a,b,c,d,n;
for(a=1;a>=0 && a<=7;a++)
{for(b=1;b>=0 && b<=7;b++)
{for(c=1;c>=0 && c<=7;c++)
{for(d=1;d>=0 && d<=7;d++)
{
n = a + b + c + d;
if(n==7)
{
printf("%d %d %d %d\n",a,b,c,d);
}}}}}
system("pause");
return 0;
}
``````
Now, add something to the code that will count the number of solutions.
Name: Fahim Shahriar Shakkhor
Notre Dame College

rakeen
Posts: 384
Joined: Thu Dec 09, 2010 5:21 pm
Location: Dhaka

### Re: Count The Number Of Solutions

Code: Select all

``````int main ()
{
int a,b,c,d,n,count=0;
for(a=1;a>=0 && a<=7;a++){
for(b=1;b>=0 && b<=7;b++){
for(c=1;c>=0 && c<=7;c++){
for(d=1;d>=0 && d<=7;d++){
n = a + b + c + d;
if(n==7){
count++;
printf("%d %d %d %d\n",a,b,c,d);
}
}
}
}
}
printf("%d",count);
system("pause");
return 0;
}``````
r@k€€/|/

Masum
Posts: 592
Joined: Tue Dec 07, 2010 1:12 pm