1-Write a program of Fibonaccci Series in C++ without Recursion.
#include <iostream>
using namespace std;
int main () {
int n1=0,n2=1,n3,i,number;
cout <<"Enter the number of elements: ";
cin >>number;
cout <<n1<<" "<<n2<<" ";
for (i=2; i<number; ++i)
{
n3= n1+n2;
cout <<n3<<" ";
n1=n2;
n2=n3;
}
getch();
}
Output :- Enter the number of elements: 10
0 1 1 2 3 5 8 13 21 34
Some basic C language program using do while loop concept in C , C++ and java
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main ()
{
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
}
Output :- Enter the number of elements: 15
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
#include <iostream>
using namespace std;
int main()
{
int n, i, m=0, flag=0;
cout << "Enter the Number to check Prime: ";
cin >> n;
m=n/2;
for (i = 2; i <= m; i++)
{
If (n % i == 0)
{
cout<<"Number is not Prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "Number is Prime."<<endl;
getch();
}
Output:- Enter the Number to check Prime: 17
Number is Prime.
4-Write a program Sum of digits algorithm in c++.
#include <iostream>
using namespace std;
int main ()
{
int n,sum=0,m;
cout <<"Enter a number: ";
cin >>n;
while(n>0)
{
m=n%10;
sum= sum+m ;
n=n/10;
}
cout<<"Sum is= "<<sum<<endl;
getch();
}
Output:- Enter a number: 624
Sum is= 12
#include < iostream >
using namespace std;
int main ()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
a=a*b; //a=50 (5*10)
b=a/b; //b=5 (50/10)
a=a/b; //a=10 (50/5)
cout<<"After swap a= "<<a<<" b= "<<b<<endl;
return 0;
}
Output:- Before swap a= 5 b= 10
After swap a= 10 b= 5
Some basic programs of Operators in c language
Fabonaccci |
#include <iostream>
using namespace std;
int main () {
int n1=0,n2=1,n3,i,number;
cout <<"Enter the number of elements: ";
cin >>number;
cout <<n1<<" "<<n2<<" ";
for (i=2; i<number; ++i)
{
n3= n1+n2;
cout <<n3<<" ";
n1=n2;
n2=n3;
}
getch();
}
Output :- Enter the number of elements: 10
0 1 1 2 3 5 8 13 21 34
Some basic C language program using do while loop concept in C , C++ and java
2- Write a program of Fibonaccci Series in C++ with Recursion.
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main ()
{
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
}
Output :- Enter the number of elements: 15
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
3-Write a program of Prime Number Program in C++.
#include <iostream>
using namespace std;
int main()
{
int n, i, m=0, flag=0;
cout << "Enter the Number to check Prime: ";
cin >> n;
m=n/2;
for (i = 2; i <= m; i++)
{
If (n % i == 0)
{
cout<<"Number is not Prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "Number is Prime."<<endl;
getch();
}
Output:- Enter the Number to check Prime: 17
Number is Prime.
4-Write a program Sum of digits algorithm in c++.
#include <iostream>
using namespace std;
int main ()
{
int n,sum=0,m;
cout <<"Enter a number: ";
cin >>n;
while(n>0)
{
m=n%10;
sum= sum+m ;
n=n/10;
}
cout<<"Sum is= "<<sum<<endl;
getch();
}
Output:- Enter a number: 624
Sum is= 12
5- Write a program to swap two numbers without using third variable.
#include < iostream >
using namespace std;
int main ()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
a=a*b; //a=50 (5*10)
b=a/b; //b=5 (50/10)
a=a/b; //a=10 (50/5)
cout<<"After swap a= "<<a<<" b= "<<b<<endl;
return 0;
}
Output:- Before swap a= 5 b= 10
After swap a= 10 b= 5
Some basic programs of Operators in c language
0 Comments