0% found this document useful (0 votes)
20 views13 pages

1st April Programs

some beginner level programs

Uploaded by

124003254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

1st April Programs

some beginner level programs

Uploaded by

124003254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.Cout.

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

//to use sqrt() function using cmath header file


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double number,answer;
cout<<"\n enter a numer "<<endl; //<< insertion operator-inserting the output
to the montior
cin>>number;//>> extraction operator -extracting the input from key board
answer=sqrt(number);
cout<<"\nsquare root of "<<number<<"is\t"<<answer;
return 0;
}
6. ctype header file
//ctype -this is used in all alphabet,strings, processing try with isupper()

#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;
}

9.calcualtion of pounds,shillings and


pences
#include<iostream>
using namespace std;
int main()
{
int
pound1,shillings1,pences1,pound2,shillings2,pences2,poundsum=0,shillingssum=0,p
encessum=0;
char ch;
do{

cout<<"\n enter pound for first amount";


cin>>pound1;
cout<<"\n enter shillings for first amount";
cin>>shillings1;
cout<<"\n enter pences for first amount";
cin>>pences1;
cout<<"\n enter pound for second amount";
cin>>pound2;
cout<<"\n enter shillings for first amount";
cin>>shillings2;
cout<<"\n enter pences for first amount";
cin>>pences2;
poundsum=pound1+pound2;
shillingssum=shillings1+shillings2;
pencessum=pences1+pences2;
if(pencessum>=12)
{
shillingssum+=(pencessum/12);
pencessum=pencessum%12;

}
if(shillingssum>=20)
{
poundsum+=(shillingssum/20);
shillingssum=shillingssum%20;

}
cout<<"total is "<<poundsum<<"\t"<<shillingssum<<"\t"<<pencessum;

cout<<"\n do you want to continue";


cin>>ch;
}while(ch=='y');
return 0;

}
10.Printing a pattern
//print '*' if j%8 is not zer0

#include<iostream>

using namespace std;

int main()

char ch;int j;

for(j=0;j<80;j++)

ch=(j%8==0)?'*':' '; // if j%8 ==0

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;
}

You might also like