Today I have some question and their solution ,but I have problem with some.


Q1: Write a program that checks if a sequence of positive numbers is in increasing order or not.
I don't know what is missing her?!

Solution:

#include<iostream>
using namespace std;
int main(){
	int value,check=1;
	cout<<"Enter a number:  ";
	cin>>value;
	while(value>=0)
	{
	int newvalue;
	cout<<"Enter another value:  ";
	cin>>newvalue;
	if(value>newvalue)
	  cout<<" ....Not sequence";
	  value=-1;
	else
	value=nwvalue;
	if(check==1)
	  cout<<"....is increasing";
	}

return 0;
}

Q 2: Let f(x,n) = x –x3/3! + x5/5! - …+(-1)n x2n+1 /(2n+1)!
Using a loop write a program to calculate f(x,n).

Solution:

#include<iostream>
#include<cmath>
using namespace std;
int main(){
	cout<<"Enter a number ";
	int m;
	cin>>m;
	for(double x=1;x<=m;x++)
	{
	  for(double n=1;n<=x;n++)
	  {
	  n=2*n+1;
	  double fact=1; 
	  fact*=n;
	  double f=1;
	  f=pow(x,n)/fact;
	  cout<<f<<endl;
	  f*=-1;
	  } 
	double f;
	cout<< f<<endl;
	}
	


return 0;
}

Q3: Write a program that prints all possible combinations of 3 numbers between 1 and n.
After I run the compiler ,the output doesn't stop.
Solution:

#include<iostream>
using namespace std;
int main(){
	cout<<"Enter a number : ";
	int num;
	cin>>num;
	for(int i=1;i<=num-2;i++)
	{
	 cout<<i<<endl;
	
	  for(int j=1;j=num-1;j++)
	  {
	     
	     cout<<j<<endl;
	   
		for(int k=1;k<=num;k++)
	        {
		cout<<k<<endl;
		}
	   }
	 
	}

return 0;
}

Please tell me, What are my mistakes?

Recommended Answers

All 11 Replies

Q1:

#include<iostream>
using namespace std;
int main(){
int value,check=1;
cout<<"Enter a number: ";
cin>>value;
while(value>=0)
{
int newvalue;
cout<<"Enter another value: ";
cin>>newvalue;
if(value>newvalue)
{
cout<<" ....Not sequence";
value=-1;
}
else
{
value=nwvalue;
if(check==1)
cout<<"....is increasing";
}
}

return 0;
}

you need to make sure your brackets are being opened and closed correctly. If you do not put brackets around what you want in the if and else statement, it only wraps the if around the first line of code after.

I did small changes in Q1,

#include<iostream>
using namespace std;
int main(){
	int value,check=1;
	cout<<"Enter a number:  ";
	cin>>value;
	while(value>=0)
	{
	int newvalue;
	cout<<"Enter another value:  ";
	cin>>newvalue;
	if(value>newvalue)
	  cout<<" is increasing";
	  check=0;
	  value=-5;
	else
	value=nwvalue;
	if(check==1)
	value==-5
	  cout<<" this is  not increasing order";
        else
	cout<<"This is increasing order";
	}

return 0;
}

I'm trying to point out the errors in your code, so here we go ..

#include<iostream>
using namespace std;

int main()
{
    int value,check=1;
    cout<<"Enter a number: ";
    cin>>value;

    while(value>=0)
    {
        int newvalue;
        cout<<"Enter another value: ";
        cin>>newvalue;
        
        if(value>newvalue)
            cout<<" is increasing";
            check=0;    [B]// you are not using braces {}, hence your [/B]
            value=-5;   [B]// compiler chokes up on these two lines[/B]
        else
            value=nwvalue;

        if(check==1)
            value==-5    [B]// you probably meant: [B]value = -5;[/B] [/B]
        cout<<" this is not increasing order";
            else
        cout<<"This is increasing order";
    }

    return 0;
}

So, try to learn to use the curly braces {}, i.e the if-else statement above should be;

if(value>newvalue)
        {   [B]// <- if block begins here[/B]
             cout<<" is increasing";
             check=0;
             value=-5;
        }   [B]// <- if block ends here[/B]
        else
        {   [B]// <- else block begins here[/B]
             value=nwvalue;
        }   [B]// <- else block ends here[/B]

Lastly, you have been using the inline code tags (i.e. icode/icode), please switch to the simple (code/code) tags instead.

Hi mitrmkar ;
I am afriad i don't know what do you mean by (icode/icode).?
However,
I have another 2 questions and there solutions,please if anyone see any mistakes tell me.
Q4 : Starting with the first value of a list (ending with -1) print only the sub-list of values in increasing order.
Example:
Input: 1 4 2 7 3 9 6 -1
Output: 1 4 7 9
Solution:

#include<iostream>
using namespace std;
int main(){
	int value,newvalue;
	cout<<"Enter 2 numbers ,and -1 means exit:  ";
	cin>>value>>newvalue;
	do
	{
	if(newvalue>value)
	  cout<<value;
	else
	  cout<<"  ";
	value=newvalue;
	
	 
	}
	while(value!=-1);

return 0;

}

After i write newvalue the compiler run with out any result,only blank screen.

Q 5:Write a program that accepts a positive integer and gives its prime factorization; that is expresses the integer as a product of primes or indicates that it is a prime.
Solution:

#include<iostream>
using namespace std;
int main(){

	cout<<"Enter any number: ";
	int n;
	cin>>n;
	int i=2;
	while(i<=n)
	{
	if(n%i==0)
	  cout<<i<<"*";
	  n=n/i;
	else  i++;
	 }
	cout<<n;
return 0;
}

In this program i have an error is( expected ';'before "else") ,but i can't understand this error because it is clear that i put semi colon.


Thanks alot.

After i write newvalue the compiler run with out any result,only blank screen.

When the program enters the do-while() loop you have there, it does not ask for any new value, so it simply enters an infinite loop (as instructed). So you need to add code to get a new value from the user.

You are still having problems with correct usage of curly braces {} with your if-else blocks, i.e. the problem is not about a missing semicolon but instead lack of curly braces.

#include<iostream>
using namespace std;
int main()
{
    <snip>

    while(i<=n)
    {
        if(n%i==0)
        [B]{[/B] // <- the if block begins here
            cout<<i<<"*";    // This line is part of the if block
            n=n/i;           // And this line too is part of the if block
        [B]}[/B] // <- the if block ends here
        else
        {  // <- it really doesn't hurt to have even a single 
           //   line of code enclosed in braces, so ...
            i++;
        }
    }

    <snip>

}

it is clear that i put semi colon

The compiler error/warning messages can at times be somewhat misleading/confusing. If you have an IDE which has the error/warning codes documented, then looking them up in the documentation might prove helpful.

Thanks alot.
I will be more careful about braces {}.

Do i have another mistakes??

Do i have another mistakes??

Now that your code is in the shape in which it compiles properly, you can start testing your programs to see if they work as expected.

Now that your code is in the shape in which it compiles properly, you can start testing your programs to see if they work as expected.

Thanks alot i will run it and if i have any problem i will tell you ,otherwise i will mark this Thread as solved.

Thanks again

i have to check this.
after checking i will tell you.

I try to solve this question in two ways,but it didn't work as i want .
1'st way

#include<iostream>
using namespace std;
int main(){
    int value,newvalue;
    cout<<"Enter a number ,and -1 means exit:  ";
    cin>>value;
    do
    {
    cout<<"Enter a new value : ";
    int newvalue;
    cin>>newvalue;
    if(newvalue>value)
    {
      cout<<value;
    }//end of if
    else{
      cout<<"  ";
    }//end of else
    value=newvalue;


    }//end of do
    while(value!=-1);

return 0;

}

I always ask the user to enter new value and that not what i want .

2'nd way:

#include<iostream>
using namespace std;
int main(){
    int value,newvalue;
    cout<<"Enter a number ,and -1 means exit:  ";
    cin>>value;
    cout<<"Enter a new value : ";
    cin>>newvalue;
    while(value!=-1);
    {

    if(newvalue>value)
    {
      cout<<value;
    } //end of if
    else{
      cout<<"  ";
    } //end of else
    value=newvalue;


    } //end of do


return 0;

}

It asks user to enter a value and new value once ,after that it stopped.
One thing i can't understand it and is why the program do not do (value=newvalue)?in both program.

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.