C++ Logics


OBJECT :
          Write a program that asks the user to input the temperature of a particular city for a week. The program then finds and displays the average temperature. Use while loop.


CODING :
    
#include <iostream>
using namespace std;
int main()
{
int b=1,c,d=0,f;
while(b<=7)
{
cout<<"Enter temperature=";
cin>>c;
++b;
d=d+c;
}
cout<<"Average temperature of the week is="<<d/7<<"\n";
return 0;
}

OUTPUT:


OBJECT :
Write a program that reads five numbers (each between 1 and 30). For each number that is input, print a line containing that number of adjacent asterisk. For example if the input number is 7, the program should display *******.

CODING :
    
#include <iostream>
using namespace std;
int main()
{
       int a,i,j=1;
       while(j<=5)
       {
       cout<<"Enter asterisk length=";
       cin>>a;
       cout<<"\n";
       for(i=0;i<=a;i++)
       {
              cout<<"*";
       }
       ++j;
       cout<<"\n\n";
       }
       return 0;
}

OUTPUT:

OBJECT :
1.      Write a program segment (including a while loop) that reads one number into a variable named n and reads another number into a variable called maxPower. Then your statements should raise n to each power 0, 1, 2, ..., maxPower, and print a table like the following (suppose n is 2 and maxPower is 4):
       
n raised
n     power     to power
--------------------------
 2       0          1
 2       1          2
 2       2          4
 2       3          8
 2       4         16


CODING :
    
#include<iostream>
using namespace std;
int main()
{
int n,maxpow,b=1,c=1;
cout<<"Enter the number=";
cin>>n;
cout<<"Enter the maximum power=";
cin>>maxpow;
cout<<"n"<<"\tpower"<<"\tn rais to power\n";
for(int i=0;i<=30;i++)
       {
              cout<<"-";
       }
cout<<"\n";
cout<<n<<" \t 0"<<"\t\t1\n";
while(b<=maxpow)
{
cout<<n;
cout<<" \t "<<b;
c=c*n;
cout<<"\t\t"<<c<<endl;
++b;
}
return 0;
}


OUTPUT:



OBJECT :
   Write a program segment using nested loops to create the following output:

    *
   ***
  *****
 *******
*********

CODING :
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
 int s, c;
 for(s=25, c=1; c<=10; c+=2, s--)
 {
  cout<<setw(s);
  for(int i=1; i<=c; i++)
   cout<<"*";
  cout<<endl;
 }
 return 0;
}

    
OUTPUT:


OBJECT :
Write Write a C++ program that takes an integer value and decide if it is even or odd. Continue asking for numbers till the user enters a negative value. The program then displays the total number of even and odd integers given. Use While statement.


CODING :
    
#include <iostream>
using namespace std;
int main()
{
       int num=1,even=0,odd=0;
       while(num>=0)
       {
              cout<<"\n Enter the number is (negative to exist)=";
              cin>>num;
              if(num%2==0)
              {
                     cout<<"its an even number";
                     even++;
              }
              else
              {
                     cout<<"its an odd number";
                     odd++;
              }
       }
       cout<<"total even numbers are ="<<even<<"\n";
       cout<<"total odd numbers are="<<odd<<"\n";
       return 0;
}


OUTPUT:

Write a C++ program that asks user to input the number of rows and produces the following output using nested loop (if rows is equal to 4 output should be)
1
1 2
1 2 3
1 2 3 4

    
#include<iostream>
using namespace std;
int main()
{
int a,i,j,b=1;
cout<<"number of rows is=";
cin>>a;"\n";
for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
{
cout<<j;
}
b++;
cout<<endl;
}
return 0;
}


Write a C++ program that inputs an integer larger than 1 and calculates the sum of squares from 1 to that integer. For example, if the integer equals 4, the sum of squares is 30 (1+4+9+16). The output should be the value of the integer and the sum, properly labeled. Continue the program till the user enters a negative value which signals the end of the program. Use a While statement.

#include <iostream>
using namespace std;
int main()
{
       float a,r;
       cout<<"enter number=";
       cin>>a;
           int square , sum=0;
       for(int i=1;i<=a;i++)
       {
       square=i*i;
       sum=sum+square;
       }
       cout<<"Sum of squares="<<sum<<"\n";
       return 0;
}














No comments:

Post a Comment