0% found this document useful (0 votes)
30 views

Coding Bootcamp: Hello World Program

This document contains code snippets demonstrating different programming concepts: 1) A "Hello World" program using cout to print text. 2) Code to calculate the factorial of a number using both a for loop and while loop. 3) A recursive function to calculate the factorial of a number. 4) Code to calculate Fibonacci series terms using both a for loop and recursive function.

Uploaded by

Noins1946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Coding Bootcamp: Hello World Program

This document contains code snippets demonstrating different programming concepts: 1) A "Hello World" program using cout to print text. 2) Code to calculate the factorial of a number using both a for loop and while loop. 3) A recursive function to calculate the factorial of a number. 4) Code to calculate Fibonacci series terms using both a for loop and recursive function.

Uploaded by

Noins1946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Coding Bootcamp

Hello world program

#include <iostream>

using namespace std;

int main() {

// your code goes here

cout << "Hello World";

return 0;

Factorial of number
For loop
#include <iostream>

using namespace std;

int main() {

// your code goes here

cout << "Factorial of number\n\n";

int i,n;

int fact;

//n = 3;

fact =1;

cin>>n ;

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

fact = fact*i;

printf("Factoial is : %d",fact);

return 0;
}

#include <iostream>

using namespace std;

While loop
int main() {

// your code goes here

cout << "Factorial of number\n\n";

int itr,n;

int fact;

//n = 3;

fact =1;

itr=1;

cin>>n ;

while(itr<=n)

fact = fact*itr;

itr=itr+1;

printf("Factoial is : %d",fact);

return 0;

Recursive loop
#include <iostream>

using namespace std;

int fac(int n){

if(n<1)

{return 1;}

return n*fac(n-1);

}
int main() {

int n;

cin >> n;

cout<< fac(n)<< endl;

//endl is end of line.

Fibonacci series
For loop

#include <iostream>

using namespace std;

int main() {

int a=0, b=1,sum =0 ,i;

int n;

cin >> n;

//printf("%d",n);

if( n==1)

{printf("%dst term is %d",n,a);}

else if (n==2)

{printf("%dnd term is %d",n,a);}

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

sum = a+b;

a=b;

b=sum;
}

printf("%dth term is %d",n,sum);

Recursive loop
#include <iostream>

using namespace std;

int fib(int n){

if (n==1)

return 0;

else if (n==2)

return 1;

else

return fib(n-1)+fib(n-2);

int main() {

int n;

cin>>n;

cout<<fib(n)<<endl;

return 0;

You might also like