How to display a count

//input two integers a and b. find out small and big values between a and b.
//Example if a is 12 and b is 5 then small will be b and big will be a.
//Use a loop to output multiples of 3 from small to big.
//Display their count and sum


The program works fine but i cant display the count of the multiples of 3.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
using namespace std;
int main()
{
   int a, b, i, sum=0, count=0;
   cout<<"Enter two numbers:";
   cin>>a>>b;
   cout<<" Multiples of 3 from " << a << " to " << b <<":"<<endl;

if (a<=b)
{
    for (i=a; i<=b; i++)
    {
        if (i % 3 == 0)
        cout<<i<<endl;
        count ++;

    }
}
 else {for (i=b; i<=a; i++)

 {
     if (i % 3 == 0)
     cout<<i<<endl;
     count ++;

 }


      }
sum= sum + i;
cout<<"Count ="<<count<<endl;
cout<<"Sum= "<<sum<<endl;


    return 0;
}
The problem here is your if statements need braces to include 2 lines of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
using namespace std;
int main()
{
   int a, b, i, sum=0, count=0;
   cout<<"Enter two numbers:";
   cin>>a>>b;
   cout<<" Multiples of 3 from " << a << " to " << b <<":"<<endl;

if (a<=b)
{
    for (i=a; i<=b; i++)
    {
        if (i % 3 == 0)
        {
        cout<<i<<endl;
        count ++;
        }
    }
}
 else 
 { 
    for (i=b; i<=a; i++)
    {
     if (i % 3 == 0) 
     {
         cout<<i<<endl;
         count ++;
     }
    }

 }
sum= sum + i;
cout<<"Count ="<<count<<endl;
cout<<"Sum= "<<sum<<endl;


    return 0;
}
Oops didn't pay attention to that, thankyou for your help
Topic archived. No new replies allowed.