Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Functions (Built-in)
Concept Notes and Resources
Concept Learning Code Sheets

Math functions – sqrt/ pow/ log/ log10 #2127

See the working of some common math functions like sqrt, pow, log and log10

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n=25;
double d=30.25;
cout<<"sqrt("<<n<<") = "<<sqrt(n)<<endl;
cout<<"sqrt("<<d<<") = "<<sqrt(d)<<endl;
cout<<"pow("<<n<<",2) = "<<pow(n,2)<<endl;
cout<<"pow("<<d<<",2) = "<<pow(d,2)<<endl;
cout<<"log("<<n<<") = "<<log(n)<<endl;
cout<<"log("<<d<<") = "<<log(d)<<endl;
cout<<"log10("<<n<<") = "<<log10(n)<<endl;
cout<<"log10("<<d<<") = "<<log10(d)<<endl;
}

Basic Trigonometric functions #2130

Using trigonometric functions like sin, cos and tan etc. in c++

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
const double PI=3.1416;
int a=90,b=0,c=45;
double ar=a*PI/180;
double br=b*PI/180;
double cr=c*PI/180;
cout<<"sin("<<ar<<") = "<<sin(ar)<<endl;
cout<<"cos("<<br<<") = "<<cos(br)<<endl;
cout<<"tan("<<cr<<") = "<<tan(cr)<<endl;
}

Maths – Rounding and related Functions #2135

Rounding and related functions like ceil, floor, round, rint, trunc etc. in c++

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double a=2.34;
double b=-5.61;
double c=3.55;
cout<<"ceil("<<a<<") = "<<ceil(a)<<endl;
cout<<"floor("<<b<<") = "<<floor(b)<<endl;
cout<<"round("<<c<<") = "<<round(c)<<endl;
cout<<"rint("<<c<<") = "<<rint(c)<<endl;
cout<<"truc("<<c<<") = "<<trunc(c)<<endl;
}

Absolute value functions abs() fabs() #2137

Using absolute value functions like fabs() and abs().

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double a=-7.32;
cout<<"fabs("<<a<<") = "<<fabs(a)<<endl;
cout<<"abs((int)"<<a<<") ="<<abs((int) a)<<endl;
}

fmod( ) – Modulus function for fractions #2143

Using the specially created modulus function fmod() for fractions like float and double data type.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double n=7.32, d=5.12;
int nn=7, nd=5;
cout<<"fmod("<<n<<","<<d<<") = "<<fmod(n,d)<<endl;
cout<<"fmod("<<n<<","<<0<<") = "<<fmod(n,0)<<endl;
cout<<"fmod("<<nn<<","<<nd<<") = "<<fmod(nn,nd)<<endl;
return 0;
}

modf() – Function to separate integer and fraction part #2145

Using the modf() function to separate the integer and fraction part of fractional value.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double d=73.325;
double ipart;
cout<<"Fraction part of "<<d<<" = "<<modf(d,&ipart)<<endl;
cout<<"Integer part of "<<d<<" = "<<ipart<<endl;
return 0;
}

Uppercase/lowercase and vice-versa conversion of alphabets #2147

Using toupper() and tolower() function for character level conversion in its ascii integer form.

#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char c='a';
char d='A';
cout<<"toupper("<<c<<") = "<<(char) toupper(c)<<endl;
cout<<"tolower("<<d<<") = "<<(char) tolower(d)<<endl;
int a=97;
int b=65;
cout<<"toupper("<<a<<") = "<<toupper(c)<<endl;
cout<<"toupper("<<b<<") = "<<tolower(c)<<endl;
int p=34;
cout<<"toupper("<<p<<") = "<<toupper(p)<<endl;
cout<<"tolower("<<p<<") = "<<tolower(p)<<endl;
return 0;
}

Check character group as alpha, digit, control, punctuation, lower, upper etc. #2150

Check for character grouping in the ascii 7 bit range of control character, digit, punctuation, alphabet, alphanumeric, lowercase, uppercase, printable, graphical, white space etc.

#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
int alpha=0, digit=0, alnum=0, lower=0,
upper=0, print=0, graph=0, cntrl=0,
punct=0, space=0;
for(int i=0;i<=127;++i)
{
if(islower(i)) ++lower;
if(isupper(i)) ++upper;
if(isalpha(i)) ++alpha;
if(isdigit(i)) ++digit;
if(isalnum(i)) ++alnum;
if(isprint(i)) ++print;
if(isgraph(i)) ++graph;
if(iscntrl(i)) ++cntrl;
if(ispunct(i)) ++punct;
if(isspace(i)) ++space;
}
cout<<"Lowercase = "<<lower<<endl;
cout<<"Uppercase = "<<upper<<endl;
cout<<"Alphabets = "<<alpha<<endl;
cout<<"Digits = "<<digit<<endl;
cout<<"Alpha-Numerics = "<<alnum<<endl;
cout<<"Printable = "<<print<<endl;
cout<<"Graphical = "<<graph<<endl;
cout<<"Punctuations = "<<punct<<endl;
cout<<"Control Chars = "<<cntrl<<endl;
cout<<"Whitespace Chars = "<<space<<endl;
return 0;
}

getchar() putchar() : C-Style character level console I/O functions #2155

The functions which are frequently used in C, getchar() and putchar() provide a feature of character level console input and output facility.

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
char c;
cout<<"Enter a single character :";
c=getchar();
cout<<"You have entered = ";
putchar(c);
return 0;
}

get() put() functions of stream I/O #2160

Using get() and put functions of I/O stream methods like cin and cout.

#include <iostream>
using namespace std;
int main()
{
char c;
cout<<"Enter a single character :";
cin.get(c);
cout<<"You entered ";
cout.put(c);
return 0;
}

getline() and write() functions of stream I/O #2175

Use of functions getline() and write of cin and cout steam I/O objects.

#include <iostream>
using namespace std;
int main()
{
char s[100];
cout<<"Enter a sentence :";
cin.getline(s,100);
cout<<"You entered ";
int i;
for(i=0;s[i]!='';++i);
cout<<i<<" characters. They are as follows."<<endl;
cout.write(s,i);
return 0;
}
Solved Problems

Print next alphabet if possible #2157

Write a c++ program to collect an alphabet from the user and then print the n

Count punctuation in user given text stream (using get) #2167

Write a program to find punctuation in the user given text data. Use the func

Count punctuation in user given text stream (using getline) #2173

Write a program to find punctuation in the user given text data. Use the func

Output Writing – isupper(), islower, isalpha() #2677

Write output of the following program.

Sum of integer array #3088

Write a program to collect 5 integer values from user in an integer array an

Marking header files #4445

Mark the name of required C++ header files for each of the following function


Concept Notes:1  Code Sheets:11  Solved Problems:6 
Back