Monday 7 April 2014

Complex recursion problem in C++ Ackermann function



The Ackermann function is a computable function which grows very, very quickly as its inputs grow. For example, while   and  are equal to  and  respectively, .
The Ackermann function can be defined as follows:
What are the last 3 digits of  

#include<iostream>
using namespace std;
int ack(long int m, long int n);
int main()
{
long int m, n,a ;
cout << "enter the m and n  : ";
cin >> m>>n;
a = ack(m, n);
cout << "Ackermann function (" << m << "," << n << ")" << "=" << a <<endl;
return 0;
}

int ack(long int m, long int n)
{

if (m==0) return (n+1);
if (m > 0 && n == 0) return (ack(m - 1, 1));
if (m > 0 && n > 0) return (ack(m - 1, ack(m, n - 1)));




}
output:
enter the m and n  : 3 9
Ackermann function (3,9)=4093

Process returned 0 (0x0)   execution time : 6.553 s

Press any key to continue.


Difficult array problem in C++ /Array operation in C++

C++ program to

Print an array ,reverse an array ,divide it in block of 3 ,then reverse again .

#include <iostream>

using namespace std;
int main()
{
int a[80],i, j, n, k, l;
cout << "enter the n :";
cin >> n;
cout << "enter the series :";
for (i = 0; i <n; i++)
{

cin >> a[i];
cout << "a[" << i << "]=" << a[i];
cout << endl;
}

cout << "now reverse:";
cout << endl;
for (i = n-1; i>=0; i--)
{
cout << a[i]<<" ";

}
cout << endl;
cout << "Now in a block of three:::"<<endl ;
if (n%3==0)
{
for (i = 0; i < n; i++)
{


if (i% 3== 0)
{
cout << endl;

}
cout << a[i]<<",";

}
}cout << endl;


cout << endl;
cout << "Now reversing each  block of three:::" << endl;
if (n % 3 == 0)
{
for (i = n-1; i >= 0; i--)
{

cout << a[i] << ",";
if (i % 3 == 0)
{
cout << endl;

}


}
}cout << endl;

return 0;
}



enter the n :6
enter the series :1 2 3 4 5 6
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
a[5]=6
now reverse:
6 5 4 3 2 1
Now in a block of three:::

1,2,3,
4,5,6,

Now reversing each  block of three:::
6,5,4,
3,2,1,

Press any key to continue . . .

C++ program to print a pattern reverse pyramid

#include <iostream>
using namespace std;
int main()
{
int i, j, n, k, l;
cout << "enter the n/column height :";
cin >> n;
for (i = n; i >0; i--)
{

for (l = 0; l <n-i ; l++)
{
cout << " ";
}

for (j = 0; j < 2 * i - 1; j++)
{
cout << "#";

}
//for (k = 0; k< i - 1; k++)
//{ cout << " "; }

cout << endl;
}

return 0;
}



enter the n/column height :15
#############################
 ###########################
  #########################
   #######################
    #####################
     ###################
      #################
       ###############
        #############
         ###########
          #########
           #######
            #####
             ###
              #
Press any key to continue . . .


C++ programme to print pattern / pyramid

#include <iostream>
using namespace std;
int main()
{
int i, j, n, k,l;
cout << "enter the n/column height :";
cin >> n;
for (i = 0; i < n; i++)
{

for (l =0 ; l < n - i - 1; l++)
{
cout << " ";
}

for (j =0 ; j < 2 * i - 1; j++)
{
cout << "#";

}
//for (k = 0; k< i - 1; k++)
//{ cout << " "; }

cout << endl;
}

return 0;
}

enter the n/column height :15

             #
            ###
           #####
          #######
         #########
        ###########
       #############
      ###############
     #################
    ###################
   #####################
  #######################
 #########################
###########################
Press any key to continue . . .

C++ program to print a pattern /right triangle

C++ program to print a pattern /right triangle



#include <iostream>
using namespace std;
int main()
{
int i, j, n, k;
cout << "enter the n/column height :";
cin >> n;
for (i = 0; i < n; i++)
{



for (j = n-i-1; j <  n; j++)
{
cout << "#";

}
for (k = 0; k< i - 1; k++)
{
cout << " ";
}

cout << endl;
}

return 0;
}



enter the n/column height :20
#
##
###
####
#####
######
#######
########
#########
##########
###########
############
#############
##############
###############
################
#################
##################
###################
####################
Press any key to continue . . .

C++ programme to print patterns 1

C++  programme to print * star or # pattern or any pattern .

#include <iostream>
using namespace std;
int main()
{
int i, j, n,k;
cout << "enter the n/column height :";
cin >> n;
for (i = 0; i < n; i++)
{



for (j = i; j < n - 1; j++)
{
cout << " ";
}
for (k = 0; k< i+1 ; k++)
{
cout << "#";
}
   
cout << endl;
}

return 0;
}



enter the n/column height :30
                             #
                            ##
                           ###
                          ####
                         #####
                        ######
                       #######
                      ########
                     #########
                    ##########
                   ###########
                  ############
                 #############
                ##############
               ###############
              ################
             #################
            ##################
           ###################
          ####################
         #####################
        ######################
       #######################
      ########################
     #########################
    ##########################
   ###########################
  ############################
 #############################
##############################
Press any key to continue . . .

C++ program to print all Prime numbers in a given range using functions

Using function  //

C++ program  to print all Prime numbers from zero to n Using a function ,given by users .The programme has been run successfully in VS (Visual studio) express 2013 and codeblocks and its working fine with no error .




#include<iostream>
using namespace std;
int prime(int n);
int main()
{
int n, i, j ;
cout << "enter the limit greater than : ";
cin >> n;

for (i = 2; i <= n; i++)

{
if (prime(i))

{
cout << i << " is prime " << endl;
}
}



return 0;
}

int prime(int n)
{
int i;
for (i = 2; i <= n - 1; i++)
{

if (n%i == 0){ break; }


}
if (n == i){ return 1; }
else
return 0;



}



output:

enter the limit greater than : 100
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
Press any key to continue . . .