- Write a program that takes a character from A to Z and convert it into lower-case character. (use type casting)
Thursday, November 27, 2014
4. Write a program that takes a character from A to Z and convert it into lower-case character. (use type casting)
display a * pattern bynb setwidth manipulator
- Write a program to display the following pattern using setw:
*
***
*****
*******
#include <iostream>
#include <iomanip> //introducing library for setw() manipulator
using namespace std;
int main()
{
cout<<setw(6)<<"*\n" //setting set width
<<setw(7)<<"***\n" //setting set width
<<setw(8)<<"*****\n" //setting set width
<<setw(9)<<"*******\n"; //setting set width
return 0;
}
Find the no of cookies!!!!!!!!!!
- A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 “servings” in the bag and that a serving equals 300 calories. Write a program that asks the user to input his/her name and how many cookies he or she actually ate and then reports how many total calories were consumed.
Wednesday, November 26, 2014
1. Write a program that asks for five test scores. The program should calculate the average test score and display it. The number displayed should be formatted in fixed-point notation, with one decimal point of precision. (use of while loop is optional)
1.
Write a program that asks for
five test scores. The program should calculate the average test score and
display it. The number displayed should be formatted in fixed-point notation,
with one decimal point of precision. (use of while loop is optional)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int s1,s2,s3,s4,s5;
double avg,sum;
cout<<"Enter first test score = ";
cin>>s1;
cout<<"Enter 2nd test score = ";
cin>>s2;
cout<<"Enter 3rd test score = ";
cin>>s3;
cout<<"Enter 4rth test score = ";
cin>>s4;
cout<<"Enter fifth test score = ";
cin>>s5;
sum=s1+s2+s3+s4+s5;
avg=sum/5;
cout<<"The average= "<<setprecision(1)<<fixed<<avg<<endl;
return 0;
}
Use of setwidth operator in c++
Use of setwidth operator in c++
/*use of setw() output formator usually it is automatically right
sewidth which are formatted numbers are written on right side spaces are placed at
left this is right justified but adding only word left it will become
left justified we have to only add <<left before <<setw()<<value;
numbers are automatically riht justified but if we want to place right word just like
<<right before stw()<<value */
/*in order to make following program right justified just remove <<left from syntax */
#include <iostream>
using namespace std;
#include <iomanip>
int main()
{
int n1=3,n2=4,n3=435,n4=2453,n5=4535,
n6=43232,n7=231534,n8=25434,n9=2423;
cout<<left<<setw(7)<<n1<<left<<setw(7)<<n2<<left<<setw(7)<<n3<<endl
<<left<<setw(7)<<n4<<left<<setw(7)<<n5<<left<<setw(7)<<n6<<endl
<<left<<setw(7)<<n7<<left<<setw(7)<<n8<<left<<setw(7)<<n9;
return 0;
}
/*use of setw() output formator usually it is automatically right
sewidth which are formatted numbers are written on right side spaces are placed at
left this is right justified but adding only word left it will become
left justified we have to only add <<left before <<setw()<<value;
numbers are automatically riht justified but if we want to place right word just like
<<right before stw()<<value */
/*in order to make following program right justified just remove <<left from syntax */
#include <iostream>
using namespace std;
#include <iomanip>
int main()
{
int n1=3,n2=4,n3=435,n4=2453,n5=4535,
n6=43232,n7=231534,n8=25434,n9=2423;
cout<<left<<setw(7)<<n1<<left<<setw(7)<<n2<<left<<setw(7)<<n3<<endl
<<left<<setw(7)<<n4<<left<<setw(7)<<n5<<left<<setw(7)<<n6<<endl
<<left<<setw(7)<<n7<<left<<setw(7)<<n8<<left<<setw(7)<<n9;
return 0;
}
Tuesday, November 25, 2014
c++ program to find factorial of any positive number;
c++ program to find factorial of any positive number;
/*This is a c++ program to find factorial of any number given by user.
we use while loop in it.
store 1 in fact then store fact*i=1*1 in it
when i will come after inrement 2 will store in it
when i will 3 and come ,2 is stored stored in fact now 2*3= 6 will store in it
It will going on till i becomes equal to num*/
#include <iostream> //library
using namespace std;
int main() //tell about main function the execution will start from here
{
int i=1,num; //we declare num and inizialize the count i.e i
double fact=1; //we also start fact from 1
cout<<"Enter number = "; //display message on console
cin>>num; //input number
if (num>0) //applying if so that number should be positive
{
while (i<=num) //applying condion that number should equal to or greater than i
{
fact=fact*i; //now store the product value of fact*i in fact
i++;
}
cout<<"The Factorial = "<<fact<<endl; //we should use {} braces to enclose the body of if,else if,else ;
} //loop and condition in parenthesis ()
else if (num==0) //we can use if separatly but no else if,else
{
cout<<"The factorial = 1"<<endl;
}
else if (num<0)
{
cout<<"Please enter positive number.\n";
}
return 0;
}
/*This is a c++ program to find factorial of any number given by user.
we use while loop in it.
store 1 in fact then store fact*i=1*1 in it
when i will come after inrement 2 will store in it
when i will 3 and come ,2 is stored stored in fact now 2*3= 6 will store in it
It will going on till i becomes equal to num*/
#include <iostream> //library
using namespace std;
int main() //tell about main function the execution will start from here
{
int i=1,num; //we declare num and inizialize the count i.e i
double fact=1; //we also start fact from 1
cout<<"Enter number = "; //display message on console
cin>>num; //input number
if (num>0) //applying if so that number should be positive
{
while (i<=num) //applying condion that number should equal to or greater than i
{
fact=fact*i; //now store the product value of fact*i in fact
i++;
}
cout<<"The Factorial = "<<fact<<endl; //we should use {} braces to enclose the body of if,else if,else ;
} //loop and condition in parenthesis ()
else if (num==0) //we can use if separatly but no else if,else
{
cout<<"The factorial = 1"<<endl;
}
else if (num<0)
{
cout<<"Please enter positive number.\n";
}
return 0;
}
Thursday, November 20, 2014
The c++ program to find the size of data types and then tell also about the data type
Q1) suppose you are programmer but you
have no user manual to see the size of different data types like int, double ,
float , char and long. Write a programm
that will calculate the size of this data types and display data type on
screen.
using namespace std;
int main()
{
int in,size_in;
float fl,size_fl;
double db,size_db;
long lg,size_lg;
short sh,size_sh;
cout<<"Enter int = ";
cin>>in;
size_in=sizeof (in);
cout<<"The size of int(data type) = "<<size_in<<endl<<"Data type is int. "<<endl<<endl;
cout<<"Enter float = ";
cin>>fl;
size_fl=sizeof (fl);
cout<<"The size of float(data type) = "<<size_fl<<endl<<"Data type is float. "<<endl<<endl;
cout<<"Enter double = ";
cin>>db;
size_db=sizeof (db);
cout<<"The size of double(data type) = "<<db<<endl<<"Data type is double."<<endl<<endl;
cout<<"Enter long = ";
cin>>lg;
size_lg=sizeof (lg);
cout<<"The size of long(data type) = "<<size_lg<<endl<<"Data type is long."<<endl<<endl;
cout<<"Enter short = ";
cin>>sh;
size_sh=sizeof (sh);
cout<<"The size of short(data type) = "<<size_sh<<endl<<"Data type is short"<<endl<<endl;
return 0;
}
Subscribe to:
Posts (Atom)