Posts

Showing posts from March, 2021

Write the program for finding aprroxiamate root of a function by Bisection method

Image
The method is applicable for numerically solving the equation  f ( x ) = 0 for the  real  variable  x , where  f  is a  continuous function  defined on an interval [ a ,  b ] and where  f ( a ) and  f ( b ) have opposite signs. In this case   a  and  b  are said to bracket a root since, by the  intermediate value theorem , the continuous function  f  must have at least one root in the interval ( a ,  b ). At each step the method divides the interval in two by computing the midpoint  c  = ( a + b ) / 2 of the interval and the value of the function  f ( c ) at that point. Unless  c  is itself a root (which is very unlikely, but possible) there are now only two possibilities: either  f ( a ) and  f ( c ) have opposite signs and bracket a root, or  f ( c ) and  f ( b ) have opposite signs and bracket a root. [5]  The method sele...

Write the program to determine the maximum possible way to reach the end from starting in game board.

Image
         #include <iostream> #include <string> using namespace std;   int maxpath( int str, int end)    {   if (str==end)            {  return 1;} //when number of blocks be lessthen no of jumps(max jump 6)      if (str>end)       {return 0;} // then this conditio n will follow satisfy.                             int count=0;          for ( int i=1;i<=6;i++)         {  count =count + maxpath(str+i,end);}       return count;        }                int main() {    int   str=1; //str = ...

Write the program to make Permutation(arrangements of string ) of a string .

Image
http://cpp.sh/6tjpq   some points :- 1_ )Permutation :- A permutation is a mathematical technique      that determines the number of possible arrangements in a set     when the order of the arrangements matters.     2_ ) Syntex   :: string.substr(i  , ln  )       Use of this Syntex :- to make        substring (i.e make small         String from big string).      i = i represent index.      ln= ln represent lenth of string           ex:- let string  a = "abgs" ;             cout<<a.substr(0,0) //print nothing lenth is zero             cout<<a.substr(0,1) //print 'a' because at zero index 'a' is present and length is 1     ...

Write the program to print all possible words from keypad 2 and 3 from keypad mobile.

Image
  /* click on below the  link and  run this program.     http://cpp.sh/8g5dbz */ Some points :  1) if we want  to add characters make a string than we should  make a string variable f=""; and add in intial with characters example " let suppose we want add char a[0]+char b[0] and to make a string if we add simply then we get 197 but if we add f+a[0]+b[0 ] then we will get string ( ad ). 2)to change  ASCII code of string or character into Integer then simply      suppose char a ="A"; to_string(a)//output 65(ASCII code of A)  in string form.       */ #include <iostream> #include <string> using namespace std; string keypadarr[]={ " " , "/" , "abc" , "def" , "ghi" , "jkl" , "mno" , "pqr","so on" }; void keypadword(string a,string b ) //user define function {         if (a.size()==0)        { return ;}   ...

Write the program to make substring of a string.

Image
#include <iostream> #include <string> using namespace std;   void substring(string a,string ans)   { if (a.size()==0)        {cout<<ans <<endl;      return ;}      char c=a[0];      string ros=a.substr(1);      substring(ros,ans);      substring(ros ,ans+c);     }   int main() {     string a = "ABC" ;     string b = "" ;          substring(a,b);         return 0; }

Write the program to replace pi with 3.14 in string. input = pippdfpig output = 3.14ppdf3.14g

Image
  #include <iostream> using namespace std; void removepi(string a)    { if (a.size()==0)         { return ;}        if (a[0]== 'p' && a[1]== 'i' )      {cout<< "3.14" ;       removepi(a.substr(2));}       else {cout<<a[0];        removepi(a.substr(1));}             } int main() {     string a;      a= "pippdfpig" ;     removepi(a);     return 0; }

Write a program to reverse the string. input = google output = elgoole */

Image
  #include <iostream>   #include <string>   using namespace std; void rvrsstr(string a) //rvrsstr=reverse string:user function {     if (a.size()==0)         { return ;}         rvrsstr(a.substr(1));         cout<<a[0];                           }     int main()     {   string a;          a= "google" ;          rvrsstr(a);       return 0;      }

X arrange in last by using recursion

  // Arrange the all x in last. //input          : "xxaxiixi"; // output      :  "aiiixxx" #include <iostream> #include <string> using namespace std;     string k(string n)     {  string ans;         if (n.size()==0)     { return "" ;} //this ""sign reveal about string.      if (n[0]== 'x' ) //in comparing always, use sign '' instead of "".         { ans=(k(n.substr(1))+ 'x' );}         else { ans=(n[0]+k(n.substr(1)));}         return ans;} int main() {   string b= "xxaxiixi" ;   k(b); cout<<k(b); int i=0;     return 0; }

Use of Recursion

Image
//cheak sorted array or not #include<iostream> using namespace std; bool check(int arr[],int n) {   if (arr[n-2]==arr[0])         {return 1;}     if(arr[n-1]>arr[n-2])        { return check(arr,n-1) ;}   else{ return 0;} } // write the sequence of decnbr void decnmbr (int n) {     cout<<n<<" ";    if(n==0){return;}    decnmbr(n-1);    } // find first occurance int firstoccurance (int a[],int n,int key,int i) {     if(i>n)      {return -1;}     if(a[i]==key)         {return i;}         firstoccurance( a,n,key,i+1);                    }   //find last occurrence...

Use of recursion

Image
include<iostream> using namespace std; int factorial( int n) {   if(n<2)   {    return 1;       }     int prvsnmbr=factorial(n-1);               return n*factorial(n-1);     } int sum(int n) {    if (n<1)       {return 0;}       return (n+sum(n-1));                }               int fibonacciseries(int n) {     if(n==1)           { return 1;}         if(n==0)         { return 0;}      int prevsnmbr = fibonacciseries(n-1)+fibonacciseries(n-2);  ...

Important

Image
  1)Inlinefunction: inline int function ( )=replace the whole function in main function for reduce complexity i.e remove return and call function but it work general function not user define function. It is a request from compiler. 2)Static Variable(static int ) = work only one time ,it is use for only  initialisation .  3)Default value = a value which is used by function if user does not give value       Ex :-int function (int a,int b=4) Call function ( x  ) then automatically fnctn use b=4 in rslt and  We provide function  (x ,y ) then use y in place b(4). Uses: 1) use to different values for different purposes without assign two value of y. 4) Recursion : call again same function in function 5)to_string(int) = It convert int into string.

find the prime nmbr by using sieve of erathosthene

Image
  // find prime nmbr  1 to nmbr // and prime factorisation of this nmbr //by using sieve of erathosthene #include <iostream> #include <conio.h> using namespace std; void checkprime( int a[], int n, int k) { int i,j;      for (i=2;i<=n;i++)     {     for (j=i*i;j<n;j=j+i)        {  a[j]=1;   }  }        for (i=2;i<n;i++)     {  if (a[i]==0){cout<<i<< " " ;}}         cout<<endl<<endl;    cout<< " prime factorisation of nmbr " ;     for (i=2;i<k;i++) {        if (a[i]==0)     {    if (k%i==0)       {cout<<i<< " " ;}} } } int main() { int i;   int a[100]={0}; int k; cout << "p...

2) .cheak even and odd nmbr\\\

//Check even or odd number #include <iostream> using namespace std; int main() {  int a,b;   cin>>a;     if (   a%2==0  )       {cout<< " a is even nmber" ;     }     else cout << "a is odd nmbr" ;       return 0;     }    

Learn C++ programming with examples

Image