Funcation 2 2020

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include <iostream>

#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int rand1(int n);
int main () {
int n, j;
int r;
srand(time (NULL));
cout << "Enter number of dice: ";
cin >> n;
for (j = 1; j <= n; j++) {
r = rand1(5) + 1;
cout << r << " "<<endl;
}
system("PAUSE");
return 0;
}
int rand1(int n) {
return rand () % n;
}

___________________________________________________________-

#include <iostream>
#include <stdlib.h>
using namespace std;
int main( )
{
int number;
int i;
srand(time(0));
for(i = 1; i <= 1000; i++)
{
number = rand() % 100;
cout << number << endl;
}
}

_________________________________________________________

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int i, number;
time_t nTime;
number = 5;
srand((unsigned) time(&nTime));
printf("Random numbers are: \n");
for( i = 0 ; i < number ; i++ )
{
printf("%d\n", rand() % 1000);
}
return(0);
}
_____________________________________________________

#include <iostream>
#include <math.h>
using namespace std;
int main() {
int x; // Declaring a variable x
cout << "Please enter the number : "; // cout to get the input value from user
cin >> x;
cout << "Here is the list of all the prime numbers Below "<< x << endl;
for ( int m=2; m<x; m++) //implementing for loop to find out prime numbers
for ( int n=2; n*n<=m; n++)
{
if ( m % n == 0)
break;
else if ( n+1 > sqrt (m)) {
cout << m << endl;
}
}
return 0;
}
#############
_________________________________________________________________________________

#include <iostream>
using namespace std;
int main ()
{
int number, x, count = 0;
cout << "Please enter the number to check if it's prime or not : " ;
cin >> number;
if ( number == 0)
{
cout << "\n" << number << " This number is not prime";
exit(1);
}
else {
for ( x=2; x < number; x++)
if ( number % x == 0)
count++;
}
if ( count > 1)
cout << "\n" << number << " This number is not prime.";
else
cout << "\n" << number << " This is prime number.";
return 0;
}

#####

____________________________________________________________________________

/*Example #3
Finding a prime number using WHILE loop with if-else

Code:*/

#include <iostream>
using namespace std;
int main()
{
int lower, higher, flag, temporary;
cout << "Please enter the two numbers for finding prime numbers between them: ";
cin >> lower >> higher;
if ( lower > higher) { //It will swap the numbers if lower number is greater
than higher number.
temporary = lower;
lower = higher;
higher = temporary;
}
cout << "Hence the Prime numbers between the number " << lower << " and " << higher
<< " are: "<< endl;
while ( lower < higher)
{
flag = 0;
for ( int x = 2; x <= lower/2; ++x)
{
if ( lower % x == 0)
{
flag = 1;
break;
}
}
if ( flag == 0)
cout << lower << " ";
++lower;
}
return 0;
}
##
___________________________________________________________________________________
__________________________
Example #1
Here is a simple example of a Fibonacci series of a number. The below program
includes a call to the recursive
function defined as fib (int n) which takes input from the user and store it in
‘n’. The next step includes
taking into for loop to generate the term which is passed to the function fib ()
and returns the Fibonacci series.
The base case is set with the if statement by checking the number =1 or 2 to print
the first two values. finally,
this recursive function goes on with the loop to print the series 1,1,2.

Code:

#include<iostream>
using namespace std;
int fib_r (int s)
{
if(s==1||s==2)
return 1;
else
return (fib_r(s-1) +fib_r(s-2)); // fib(n-1) + fib(n-2) for adding successive
terms
}
int main ()
{
int k,n;
cout<<"Enter no.of n terms: ";
cin>>n;
cout<<" calculated fibonacci numbers are"<<endl;
for (k=1; k<=n; k++)
cout<<fib_r(k)<<endl;
return 0;
}

___________________________________________________________________________________
__________________________
Example #2
Checking for the palindrome number using a recursive function.

Code:

#include <iostream>
using namespace std;
int palim(int a, int t)
{
if (a == 0)
return t;
t = (t * 10) + (a % 10);
return palim(a / 10, t);
}
int main()
{
int n;
cout<<"Enter the number :"; cin>>n;
int result = palim(n, 0);
if (result == n)
cout << "Number "<<n<<" is a palindrome" << endl;
else
cout << "Number "<<n<<" is not a palindrome"<< endl;
return 0;
}

___________________________________________________________________________________
__--

Example #3
Program with a random-number generator.

Code:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int rand1(int n);
int main () {
int n, j;
int r;
srand(time (NULL));
cout << "Enter number of dice: ";
cin >> n;
for (j = 1; j <= n; j++) {
r = rand1(5) + 1;
cout << r << " ";
}
system("PAUSE");
return 0;
}
int rand1(int n) {
return rand () % n;
}

___________________________________________________________________________________
__________

You might also like