Login


Lost your password?

Don't have an account ? Register ×
  
CPP: All Recent Posts Grouped
Concept Learning Code Sheets View all
using setprecision without fixed or scientific format. #4341  (23-May-2018)
In Topic(s) : User Defined Data Types   

#include <iostream>
typedef double amount;
using namespace std;
int main()
{
amount principal=100.50;
amount term=2.5;
amount rate=5.5;
amount simple_interest=principal*term*rate/100.0;
cout<<"Simple Interest = "<<simple_interest<<endl;
return 0;
}

typedef as replacement for using a friendlier name.


using setprecision without fixed or scientific format. #4326  (18-May-2018)
In Topic(s) : Display Using IO Manipulation   

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double d=123.5613;
cout<<d<<endl;
cout<<setprecision(4)<<d<<endl;
cout<<setprecision(7)<<d<<endl;
cout<<setprecision(8)<<d<<endl;
cout<<setprecision(3)<<d<<endl;
cout<<setprecision(2)<<d<<endl;
return 0;
}

setprecision to set number of digits for any decimal containing floating number.


setf method for left and right alignment in a display field #4318  (15-May-2018)
In Topic(s) : Display Using IO Manipulation   

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str[]="Hello";
cout.setf(ios::left);
cout<<setw(10)<<str<<setw(10)<<str<<endl;
cout.unsetf(ios::left);
cout<<setw(10)<<str<<setw(10)<<str<<endl;
cout.setf(ios::right);
cout<<setw(10)<<str<<setw(10)<<str<<endl;
return 0;
}

Use of setf method of cout to align content as left or right in a display field.


Testing setw() for persistence of its effect #4312  (15-May-2018)
In Topic(s) : Display Using IO Manipulation   

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str[]="Hello";
cout<<setw(7)<<endl;
int a=10;
int b=15;
cout<<str<<endl;
cout<<str<<endl;
cout<<setw(10)<<str<<str<<endl;
cout<<setw(10)<<str<<setw(6)<<str<<endl;
return 0;
}

Seeing in what condition effect of setw is persistent and when it is not.


Use of setw() manipulator #4310  (15-May-2018)
In Topic(s) : Display Using IO Manipulation   

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str[]="Hello";
cout<<setw(5)<<str<<endl;
cout<<setw(6)<<str<<endl;
cout<<setw(7)<<str<<endl;
cout<<str<<endl;
float f=3.1416;
cout<<f<<endl;
cout<<setw(10)<<f<<endl;
return 0;
}

Demonstration of setw manipulation for setting display width


Modulus operator for integers #3589  (24-Feb-2018)
In Topic(s) : Operators   

#include <iostream>
using namespace std;
int main()
{
int a=22,b=7;
cout<<a/b<<endl;
cout<<a%b<<endl;
return 0;
}

Getting the remainder of an integer division using the modulus operator.


Find prime factors of a number #3218  (31-Jan-2018)
In Topic(s) : Loop Applications - Handling numerals    Number Varieties and Crunches   

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a positive integer: ";
cin>>n;
cout<<"The prime factors are: ";
for (int i=2; i <= n; i++)
{
while(n % i == 0)
{
n /= i;
cout<<i<<" ";
}
}
return 0;
}

Finding prime factors of a number input by the user.


Checking for buzz number #3176  (29-Jan-2018)
In Topic(s) : Simple If and If Else    Number Varieties and Crunches   

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter a number: ";
cin>>n;
if(n%7 == 0 || n % 10 == 7)
cout << n << " is a buzz number "<<endl;
else
cout << n<< " is not a buzz number "<<endl;
return 0;
}

Test if a number is divisible by 7 or the last digit is 7, means whether the number is a buzz number.


Pascal’s Triangle Printing #3173  (29-Jan-2018)
In Topic(s) : Pattern printing loops   

#include <iostream>
using namespace std;
int main()
{
int r;
cout<<"Enter number of r: ";
cin>>r;
int m=1;
for(int i=0;i<r;++i)
{
for(int sp = 1; sp <= r-i; sp++)
cout <<" ";
for(int j=0;j<=i;j++)
{
if (j==0||i==0)m=1;
else m = m*(i-j+1)/j;
cout<<m<< " ";
}
cout << endl;
}
return 0;
}

Printing a pascal’s triangle of given number of rows.


Printing a damru/dumbbell pattern #3128  (28-Jan-2018)
In Topic(s) : Pattern printing loops   

#include <iostream>
using namespace std;
int main()
{
int n=5;
int i,j;
int sp=0;
for(int i=0;i<=n-1;++i)
{
for (j=1;j<=sp;j++) cout<<" ";
sp++;
for (j=1;j<=2*(n-i)-1;j++) cout<<"*";
cout<<endl;
}
sp=n-1;
for(int i=1;i<=n;++i)
{
for (j=1;j<=sp; j++) cout<<" ";
sp--;
for (j=1;j<=2*i-1;j++) cout<<"*";
cout<<endl;
}
return 0;
}

A program that prints a damru or dumb bell pattern of stars where a size unit is given in form of half of the damru height.


Solved ProblemsView all
problem based on setw, setf and unsetf #4321 (15-May-2018)
In Topic(s) : Display Using IO Manipulation   

print the following table of temperature record of a patient properly using setw,setf,unsetf methods.

 

Readings   Values
-----------------
Morning     100.4
Afternoon    99.8
Evening      98.8

 

print the following table of temperature record of a patient properly using



Frequency of a given character in a sentence #3243 (2-Feb-2018)
In Topic(s) : String Handling (Null Terminated)   

Write a program to input a small sentence from the user and ask about frequency of which character is to be found. Print how many times the character occurred in the given string. Do it for a a c-style (null-terminated string). Please note that the character frequency is to be found  irrespective of its lower or upper case.

Write a program to input a small sentence from the user and ask about frequen


Suggesting Error Corrections in Natural Number Addition #3221 (31-Jan-2018)
In Topic(s) : Simple Loops   

A student has written this program to sum natural numbers from 1 to 10. His program is not giving correct results. Please help in correcting this program with right explanations as well.

A student has written this program to sum natural numbers from 1 to 10. His




Finding input variables errors #3190 (30-Jan-2018)
In Topic(s) : Simple User Input   

Find error in the following program and suggest corrections. Assume that includes and namespace is already given.

Find error in the following program and suggest corrections. Assume that incl





Concept Notes and ResourcesView all

Some rules related to C++ identifiers are as follows: Can be arbitrarily long alpha numeric (letters and numbers) sequence. The first character must be a letter. As an exception _ (underscore) is considered as a letter. Upper and lower case letters considered different All characters in the name are considered significant. C++ Keywords can not […]










Practice ProblemsView all


Finding errors in cout statements #2323 (7-Jan-2018)
In Topic(s) : Beginning to Program   

Find errors in the following cout statement constructs. 1)  cout<<“This is my work” “; 2)  cout<<\n<<“Show me next line”; 3) cout<<cout<<“This is cout chain”; 4)  cout>>a<<b; 5) return cout; 6) cout<<x=10; 7) cout<<“Student marks are “;<<marks;  



Back