1st April Programs
1st April Programs
precision() function
#include <iostream>//preprocesor directive -has lot of predefined classes with
functions
using namespace std;
int main()
{
//cout<<"Hello World"; //::-scope resolution
float a=51.123879;
cout.precision(6); //.-operator ,use object.function() //cout and
cin are objects
cout<<a<<endl;
float grade=86.1263f;
cout.precision(4);
cout<<grade;
return 0;
}
2.
2.Inline function-
this function is supposed to have only
one line of statement
#include<iostream>
using namespace std;
inline int mult(int x,int y)
{
return(x*y);
}
inline int add(int x,int y)
{
return(x+y);
}
int main()
{
int x=5,y=6;
cout<<"the product of x and y is "<<mult(x,y)<<endl;
cout<<"the sum of x and y is "<<add(x,y);
return 0;
}
3.Inline function
//inline function to change a positive
number to a negative number
#include<iostream>
using namespace std;
inline int changenegative(int x)//to change the postive number to a negative
number ,use ternary operator
{
return(x>0)?-x:x;
}
int main()
{
int x=905;
cout<<changenegative(x);
return 0;
4.Setw()Function
//setw() is a function for formatting the
output ,for using setw() u have to
include iomanip
//to display location ,population of
cities of your choice //setw()-by default
it is right justified
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
long pop1=2425785,pop2=475,pop3=9768;
cout<<setw(8)<<"LOCATION"<<setw(20)<<"POPULATION"<<endl
<<setw(8)<<"portcity"<<setw(20)<<pop1<<endl
<<setw(8)<<"trichy"<<setw(20)<<pop2<<endl
<<setw(8)<<"vallam"<<setw(20)<<pop3<<endl;
return 0;
}
5. cmath header file
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
char x;
cout<<"\n enter a character";
cin>>x;
if(islower(x))
cout<<x<<"\t is a lowercase";
else
cout<<x<<"\t is a uppercase";
return 0;
}
7.pyramid program
//pyramid of x to be printed
#include<iostream>
using namespace std;
int main()
{
int i,space,j,rows;
cout<<"\n enter number of rows";
cin>>rows;
int c=5;
for(i=1;i<=5;i++)
{
for(space=1;space<c;space++)
{
cout<<" "; // 7 rows,prints 6 spaces
}
for(j=1;j<=(2*i)-1;j++)
{
cout<<"X"; // at the 7th space x gets printed
}
cout<<endl;
c--; //for reduction of spaces in subsequent rows
return 0;
}
8.getche()
//getche() in cpp simliar to getchar() ,can be used in DEV compiler ,for the
same program we can use getchar() -do it in online compiler
//type a sentence ,this program should couting the number of words and
also the total diigits in the sentence
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int wordcount=1;
int charactercount=0;
char ch='a';
while(ch!='\r') //carraige return
{
ch=getche();
if(ch==' ')
wordcount++;
else
charactercount++;
}
cout<<"\n words is "<<wordcount<<"letters is "<<(charactercount-1)<<endl;
return 0;
}
}
if(shillingssum>=20)
{
poundsum+=(shillingssum/20);
shillingssum=shillingssum%20;
}
cout<<"total is "<<poundsum<<"\t"<<shillingssum<<"\t"<<pencessum;
}
10.Printing a pattern
//print '*' if j%8 is not zer0
#include<iostream>
int main()
char ch;int j;
for(j=0;j<80;j++)
cout<<ch;
return 0;
}
11. Count word, characters and space of a
string
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
char str[100];//declare and initialize char array
int i;
int words=1,characters=0,space=0;
cout<<"Please enter the string \n";
gets(str);
for(i=0; str[i] != '\0'; i++){
if(str[i]!=' '){//check characters
characters++;
}
else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ //check words
words++;
}
}
cout<<"\nTotal words: "<<words; //display total numbers of words
cout<<"\nTotal characters: "<<characters; //display total numbers of characters
cout<<"\nSpace: "<<(words-1); ////display total numbers of space
getch();
return 0;
}