#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
cout<<"\n Type a Character : ";
char ch = getchar();
cout << "\n The entered Character is: ";
putchar(ch);
return 0;
}
Output:
Type a Character : T
The entered Character is: T
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str[50];
cout<<"Enter a string : ";
gets(str);
cout<<"You entered: "
puts(str);
return(0);
}
Enter a string : Computer Science
You entered: Computer Science
#include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
char ch;
int r;
cout<<"\n Type a Character :";
ch = getchar();
r = isalnum(ch);
cout<<"\nThe Return Value of isalnum(ch) is :"<<r;
}
Type a Character :A
The Return Value of isalnum(ch) is :1
Type a Character :?
The Return Value of isalnum(ch) is :0
#include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
char ch;
cout << "\n Enter a charater: ";
ch = getchar();
cout<<"\n The Return Value of isalpha(ch) is :" << isalpha(ch) ;
}
Enter a charater: A
The Return Value of isalpha(ch) is :1
Enter a charater: 7
The Return Value of isalpha(ch) is :0
using namespace std;
#include<iostream>
#include<ctype.h>
int main()
{
char ch;
cout << "\n Enter a Character: ";
cin >> ch;
cout<<"\n The Return Value of isdigit(ch) is :" << isdigit(ch) ;
}
Enter a Character: 3
The Return Value of isdigit(ch) is :1
Enter a Character: A
The Return Value of isdigit(ch) is :0
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char source[] = "Computer Science";
char target[20]="target";
cout<<"\n String in Source Before Copied :"<<source;
cout<<"\n String in Target Before Copied :"<<target; strcpy(target,source);
cout<<"\n String in Target After strcpy function Executed :"<<target; return 0;
}
String in Source Before Copied :Computer Science
String in Target Before Copied :target
String in Target After strcpy function Executed :Computer Science
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char source[ ] = "Computer Science";
cout<<"\nGiven String is "<<source<<" its Length is "<<strlen(source);
return 0;
}
Given String is Computer Science its Length is 16
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char string1[] = "Computer";
char string2[] = "Science";
int result;
result = strcmp(string1,string2);
if(result==0)
{
cout<<"String1 : "<<string1<<" and String2 : "<<string2 <<"Are Equal";
}
if (result<0)
{
cout<<"String1 :"<<string1<<" and String2 : "<<string2 <<" Are Not Equal";
}
}
String1 : Computer and String2 : Science Are Not Equal
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char target[50] = "Learning C++ is fun";
char source[50] = " , easy and Very useful";
strcat(target, source);
cout << target ;
return 0;
}
Learning C++ is fun , easy and Very useful
using namespace std;
#include<iostream>
#include<ctype.h>
#include<string.h>
int main()
{
char str1[50];
cout<<"\nType any string in Lower case :";
gets(str1);
cout<<"\n Converted the Source string “<<str1<<into Upper Case is "<<strupr(str1); return 0;
}
Type any string in Lower case : computer science
Converted the Source string computer science into Upper Case is COMPUTER SCIENCE
using namespace std;
#include<iostream>
#include<ctype.h>
#include<string.h>
int main()
{
char str1[50];
cout<<"\nType any string in Upper case :";
gets(str1);
cout<<"\n Converted the Source string “<<str1<<into Lower Case is "<<strlwr(str1);
}
Type any string in Upper case : COMPUTER SCIENCE
Converted the Source string COMPUTER SCIENCE into lower Case is computer science
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 0.5, result;
result = cos(x);
cout << "COS("<<x<<")= "<<result;
}
COS(0.5)= 0.877583
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x = 625, result;
result = sqrt(x);
cout << "sqrt("<<x<<") = "<<result;
return 0;
}
sqrt(625) = 25
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
double base, exponent, result;
base = 5;
exponent = 4;
result = pow(base, exponent);
cout << "pow("<<base << "^" << exponent << ") = " << result;
double x = 25;;
result = sin(x);
cout << "\nsin("<<x<<")= "<<result;
return 0;
}
pow(5^4) = 625
sin(25)= -0.132352
#include<iostream>
#include<cstdlib.h>
using namespace std;
int main()
{
int random = rand(); /* No srand() calls before rand(), so seed = 1*/
cout << "\nSeed = 1, Random number = " << random; srand(10);
/* Seed = 10 */
random = rand();
cout << "\n\nSeed = 10, Random number = " << random;
return 0;
}
Seed = 1, Random number = 41
Seed = 10, Random number = 71
#include <iostream>
using namespace std;
double area(const double r,const double pi=3.14)
{
return(pi*r*r);
}
int main ()
{
double rad,res;
cout<<"\nEnter Radius :";
cin>>rad;
res=area(rad);
cout << "\nThe Area of Circle ="<<res;
return 0;
}
Enter Radius :5
The Area of Circle =78.5
#include<iostream>
using namespace std;
void display(int x)
{
int a=x*x;
cout<<"\n\nThe Value inside display function (a * a):"<<a;
}
int main()
{
int a;
cout<<”\nExample : Function call by value:”;
cout<<"\n\nEnter the Value for A :";
cin>>a;
display(a);
cout<<"\n\nThe Value inside main function "<<a;
return(0);
}
Example : Function call by value
Enter the Value for A : 5
The Value inside display function (a * a) : 25
The Value inside main function 5
#include<iostream>
using namespace std;
void display(int &x) //passing address of a//
{
x=x*x;
cout<<"\n\nThe Value inside display function (n1 x n1) :"<<x ;
}
int main()
{
int n1;
cout<<"\nEnter the Value for N1 :";
cin>>n1;
cout<<"\nThe Value of N1 is inside main function Before passing : "<< n1; display(n1);
cout<<"\nThe Value of N1 is inside main function After passing (n1 x n1) : "<< n1; return(0);
}
Enter the Value for N1 :45
The Value of N1 is inside main function Before passing : 45
The Value inside display function (n1 x n1) :2025
The Value of N1 is inside main function After passing (n1 x n1) : 2025
#include <iostream>
using namespace std;
inline float simpleinterest(float p1,float n1, float r1)
{
float si1=(p1*n1*r1)/100;
return(si1);
}
int main ()
{
float si,p,n,r;
cout<<"\nEnter the Principle Amount Rs. :";
cin>>p;
cout<<"\nEnter the Number of Years :";
cin>>n;
cout<<"\nEnter the Rate of Interest :";
cin>>r;
si=simpleinterest(p,n,r);
cout << "\nThe Simple Interest = Rs."<<si;
return 0;
}
Enter the Principle Amount Rs. :60000
Enter the Number of Years :10
Enter the Rate of Interest :5
The Simple Interest = Rs.30000
#include<iostream>
using namespace std;
void display()
{
cout<<"First C++ Program with Function";
}
int main()
{
display(); // Function calling statement//
return(0);
}
First C++ Program with Function
#include<iostream>
using namespace std;
int display()
{
int a, b, s;
cout<<"Enter 2 numbers: ";
cin>>a>>b;
s=a+b;
return s;
}
int main()
{
int m=display();
cout<<"\nThe Sum="<<m;
return(0);
}
Enter 2 numbers: 10 30
The Sum=40
#include<iostream>
using namespace std;
void display(int x, int y)
{
int s=x+y;
cout<<"The Sum of Passed Values: "<<s;
}
int main()
{
int a,b;
cout<<"\nEnter the First Number :";
cin>>a;
cout<<"\nEnter the Second Number :";
cin>>b;
display(a,b);
return(0);
}
Enter the First Number :50
Enter the Second Number :45
The Sum of Passed Values: 95
#include<iostream>
using namespace std;
int display(int x, int y)
{
int s=x+y;
return s;
}
int main()
{
int a,b;
cout<<"\nEnter the First Number :";
cin>>a;
cout<<"\nEnter the Second Number :";
cin>>b;
int s=display(a,b);
cout<<”\nExample:Function with Return Value and with Arguments”;
cout<<"\nThe Sum of Passed Values: "<<s; return(0);
}
Enter the First Number :45
Enter the Second Number :67
Example: Function with Return Value and with Arguments
The Sum of Passed Values: 112
#include<iostream>
#include<string.h>
using namespace std;
char *display()
{
return (“chennai”);
}
int main()
{
char s[50];
strcpy(s,display());
cout<<”\nExample:Function with Non Integer Return”<<s;
return(0);
}
Example: Function with Non Integer Return Chennai
#include<iostream>
using namespace std;
int main()
{
int n1=150;
int &n1ref=n1;
cout<<"\nThe Value of N1 = "<<n1<<" and n1Reference = "<<n1ref;
n1ref++;
cout<<"\nAfter n1 increased the Value of N1 = "<<n1;
cout<<" and n1Reference = "<<n1ref;
return(0);
}
The Value of N1 = 150 and n1Reference = 150
After n1 increased the Value of N1 = 151 and n1Reference = 151
#include <iostream>
using namespace std;
int factorial(int); // Function prototype //
int main()
{
int no;
cout<<"\nEnter a number to find its factorial: ";
cin >> no;
cout << "\nFactorial of Number " << no <<" = " << factorial(no);
return 0;
}
int factorial(int m)
{
if (m > 1)
{
return m*factorial(m-1);
}
else
{
return 1;
}
}
Enter a number to find its factorial: 5
Factorial of Number 5 = 120
Program 11.27
#include <iostream>
using namespace std;
//Function to find HCF //
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
int main()
{
int num1, num2;
cout << "Enter two positive integers: ";
cin >> num1 >> num2;
cout << "Highest Common Factor (HCF) of " << num1;
cout<< " & " << num2 << " is: " << hcf(num1, num2); return 0;
}
Enter two positive integers: 350 100
Highest Common Factor (HCF) of 350 & 100 is: 50
//Demo to test Local Scope//
#include<iostream>s
using namespace std;
int main ( )
{
int a, b ;
a = 10;
b = 20;
if (a > b)
{
int temp; //local to this if block//
temp = a;
a = b;
b = temp;
}
cout <<"\n Descending order .... \n";
cout <<a <<"\t"<<b;
return(0);
}
Descending order ….
10 20
//Demo to test Local Scope//
#include<iostream>s
using namespace std;
int main ( )
{
int a, b ;
a = 10;
b = 20;
if (a > b)
{
int temp; //local to this if block//
temp = a;
a = b;
b = temp;
}
cout <<temp;
return(0);
}
//Demo to test Function Scope//
#include<iostream>
using namespace std;
void add(int x, int y)
{
int m=x+y; //'m' declared within function add()//
cout<<"\nThe Sum = "<<m;
}
int main ( )
{
int a, b ;
a = 10;
b = 20;
add(a,b);
return(0);
}
//Demo to test Function Scope//
#include<iostream>
using namespace std;
void add(int x, int y)
{
int m=x+y; //'m' declared within function add()//
cout<<"\nThe Sum = "<<m;
}
int main ( )
{
int a, b ;
a = 10;
b = 20;
add(a,b);
cout<<m; //'m' declared within function add()//
return(0);
}
The Sum = 30
//Demo to test File or global Scope//
#include<iostream>
using namespace std;
int file_var=20; //Declared within File//
void add(int x, int y)
{
int m=x+y+file_var;
cout<<"\n The Sum = "<<m;
}
int main ( )
{
int a, b ;
a = 10;
b = 20;
add(a,b);
cout<<”\nThe File Variable = “<<file_var;
return(0);
}
The Sum = 50
The File Variable =20
//Program to show that we can access a global variable
//using scope resolution operator :: when there is a local
//variable with same name //
#include<iostream>
using namespace std;
int x=45; // Global Variable x
int main()
{
int x = 10; // Local Variable x
cout << "\nValue of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
Value of global x is 45
Value of local x is 10
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.