Showing posts with label Cpp Codes. Show all posts
Showing posts with label Cpp Codes. Show all posts

Tuesday, 27 November 2012

Write a program that will calculate the sum of expension of cosx in Maclaurin Series.


// 1 - x^2/2! + x^4/4! - x^6/6! + - - - - - - - - - - upto so on


#include <iostream>
#include <math.h>  // for power fucntion
using namespace std;
int main()
{
double x_value,fact=1.0,term,sum=0,max_power;
int sign=1;
cout << "Welcome to The WORLD OF C++ PROGRAMING"<<endl;
cout << endl;
cout << "This Program will generate the Maclauren Series of cos(x)."<<endl;
cout << endl;
cout << "Which is cos(x)= 1 - x^2/2! + x^4/4! - x^6/6! +  - - - - - - - "<<endl;
cout << endl;
cout << "Enter the Value or X: ";
cin >> x_value;
cout << "Enter Maximum Power of series: ";
cin >> max_power;
for (int i=0;i<=max_power;i=i+2)
{
fact=1;
for(int j=1;j<=i;j=j+1)
{
fact=fact*j;
}
term=pow(x_value,i)/fact;  // usage of power fucntion
sum=sum+(term*sign);
sign*=-1;
}
cout << "Sum is of Series is: " << sum <<endl;
return 0;
}


Write a program that will calculate the sum of expension of sinx in Maclaurin Series.


// x - x^3/3! + x^5/5! - x^7/7! + - - - - - - - - - - upto so on

#include <math.h> // to perform power fucntion.....
#include <iostream>
using namespace std;
int main()
{
double x_value,fact=1.0,term,sum=0,max_power;
int sign=1;
cout << "Welcome to The WORLD OF C++ PROGRAMING"<<endl;
cout << endl;
cout << "This Program will generate the Maclauren Series of sin(x)."<<endl;
cout << endl;
cout << "Which is sin(x)= x - x^3/3! + x^5/5! - x^7/7! +  - - - - - - - "<<endl;
cout << endl;
cout << "Enter the Value or X: ";
cin >> x_value;
cout << "Enter Maximum Power of series: ";
cin >> max_power;
for (int i=1;i<=max_power;i=i+2)
{
fact=1;
for(int j=1;j<=i;j=j+1)
{
fact=fact*j;
}
term=pow(x_value,i)/fact; // power fucntion
sum=sum+(term*sign);
sign*=-1;
}
cout << "Sum is: " << sum <<endl;
return 0;
}

Friday, 23 November 2012

Code to calculate the sum of series 1/2!+2/3!+3/4!+4/5!+5/6!+ - - - - - -



#include <iostream>
using namespace std;
int main()
{
double fact,term,sum=0;
int length;
cout << "This program will disply the sum of following series... \n"<<endl;
cout << "1/2!+2/3!+3/4!+4/5!+5/6!+ - - - - - - \n"<<endl;
cout << "Enter Length of the series: ";
cin >> length;
for (int i=1;i<=length;i++)
{
fact=1;
for (int j=1;j<=i+1;j++)
{
fact=fact*j;
}
term=i/fact;
sum=sum+term;
}
cout << "Sum is: "<< sum << endl;
cout <<endl;

return 0;
}

Sum of series 1/1!+2/2!+3/3!+4/4!+5/5! + - - - - - - -


Write a program to calculate the sum of the following series:
1/1!+2/2!+3/3!+4/4!+5/5!+6/6!+ - - - - - - -




#include <iostream>
using namespace std;
int main()
{
 double fact,term,sum=0;
 int length;
 cout << "This program will disply the sum of following series... \n"<<endl;
 cout << "1/1!+2/2!+3/3!+4/4!+5/5!+ - - - - - - \n"<<endl;
 cout << "Enter Length of the series: ";
 cin >> length;
 for (int i=1;i<=length;i++)
 {
  fact=1;
  for (int j=1;j<=i;j++)
  {
   fact=fact*j;
  }
  term=i/fact;
  sum=sum+term;
 }
  cout << "Sum is: "<< sum << endl;
  cout <<endl;

 return 0;
}

}



Pattern print With Nested for loop


Write a program that will display the following pattern using nested for loop
1
12
123
1234
12345

#include <iostream>
using namespace std;
int main()
{
for (int i=1;i<=5;i++)
{
     for (int j=1;j<=i;j++)
     cout << j;
cout <<endl;
}
return 0;
}

Pattern with Nested For Loop


Write a program that will display the following patter using nested for loop....
1
22
333
4444
55555
  
#include <iostream>
using namespace std;
int main()
{
for (int i=1;i<=5;i++)
{ for (int j=1;j<=i;j++)
cout << i;
cout <<endl;
}
return 0;
}

Tuesday, 20 November 2012

How to Generate a Fibonacci series in C++ Programming?


#include <iostream>
using namespace std;
int main()
{
int i, a=0,b=1,c, num;
cout << "Enter Number for how many times generate Series: ";
cin >>num;
cout << "Fibonaci Series is: \n";
for(i=0;i<num;i++)
{
c=a+b;
a=b;
   b=c;
cout << "\t" <<c<<endl;
}
return 0;
}

Square root upto given Number.


#include <iostream>
//#include <math.h>
using namespace std;
int main()
{
int num;
cout << "Enter Number upto which u want to take square: ";
cin >> num;
for (int q=1;q<=num;q++)

cout << "Square of " << q << " is " << q*q <<endl;

return 0;
}

Saturday, 17 November 2012

Pattern Print Code with Nested For Loop

        *
      **
    ***
  ****
*****



#include <iostream>
#include <math.h>
using namespace std;
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=5-i;j++)
cout <<" ";
for (int k=1;k<=i;k++)
cout <<"*";
cout<<endl;
}
return 0;
}

Pattern Print Code


*****
****
***
**
*

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
for (int i=5;i>=1;i--)
{
for(int j=1;j<=5-i;j++)
cout <<" ";
for (int k=1;k<=i;k++)
cout <<"*";
cout<<endl;
}
return 0;
}

Pattern Print code


*******
******
*****
****
***
**
*


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
for (int i=7;i>=1;i--)
{
for(int j=1;j<=i;j++)
cout <<"*";
cout<<endl;
}
return 0;
}

Pattern Print Code with Nested For loop


Write a program that will print the following code,
*
**
***
****
*****


#include <iostream>
#include <math.h>
using namespace std;
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
cout <<"*";
cout<<endl;
}
return 0;
}

Pattern Print Code with Nested For Loop


Write a program that will print the following pattern.
* * * * *
* * * * *

* * * * *
* * * * *

* * * * *



#include <iostream>
#include <math.h>
using namespace std;
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
cout <<" * ";
cout<<endl;
}
return 0;
}

What is Nested For Loop? Describe Flow Diagram and give an example?

Nested Loop:

           The loop within a loop is called Nested loop. In nested loops the inner loop is executed completely with each change in the value of counter variable of outer loop. The nesting can be done upto any level. The increase in level of nesting increases the complexity of nested loop.

Flow Diagram:


Example:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
for (int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
cout << i <<"\t"<<j<<endl;
}
        return 0;
}

Reverse the value of Entered Number


write a program that reverse the value of given number like 12345 and program should output 54321
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num,rev=0;
cout << "Enter an Integer: ";
cin >>num;
/*cout << "Enter The value of N: ";
cin >>n_value;*/
for (int i=num;i!=0;i=i/10)
{
rev=10*rev+i%10;
}
cout << "Entered Number is : " << num <<endl;
cout << "Reverse is: " << rev <<endl;
return 0;
}

Sum of Series

write a program that will calculate the sum of series:   x+x2+x3+...............+xn
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n_value,x_value,sum=0;
cout << "Enter The value of X: ";
cin >>x_value;
cout << "Enter The value of N: ";
cin >>n_value;
for (int i=1;i<=n_value;i++)
{
sum=sum + pow(x_value,i);
}
cout << "Sum is: " << sum <<endl;
return 0;
}

Sum of Squares of Integers

//Write a program to calculate the sum of squares of integers 1 to n. where n is the value entered by user. i.e(sum=12+22+32+42+52................n2).

#include <iostream>
using namespace std;
int main()
{
int num,sum=0;
cout << "Enter The value of n: ";
cin >>num;
for (int i=1;i<=num;i++)
{
sum=sum+(i*i);
}
cout << "Sum is: " << sum <<endl;
return 0;
}

Friday, 16 November 2012

Prime or Composite With "bool" data type using NOT operator.


//3rd logic with NOT Logical Operator and bool Data Type.
#include <iostream>
#include <process.h>
using namespace std;
int main()
{
int n;
bool check=false;
cout << "Enter Any +ve Integer: ";
cin >> n;
if (n < 0)
{
cout << "STOP !!!! Plz Enter Correct Number Value."<<endl;
exit(1);
}
for (int j=2;j<=n/2;j++)
{
if (n%j==0)
{
check=true;
break;
}
}
if (check!=true)
cout << n << " is a Prime Number.\n";
else
cout << n << " is a Composite Number.\n";
return 0;
}

Prime or Composite........


// 2nd Logic To check prime or composite
#include <iostream>
#include <process.h>
using namespace std;
int main()
{
int n,counter=0;
cout << "Enter Any +ve Integer: ";
cin >> n;
if (n < 0)
{
cout << "STOP !!!! Plz Enter Correct Number Value."<<endl;
exit(1);
}
for (int j=2;j<=n/2;j++)
{
if (n%j==0)
{
counter++;
break;
}
}
if (counter==0 && n!=0)
cout << n << " is a Prime Number.\n";
else
cout << n << " is a Composite Number.\n";
return 0;
}

Get Number And check it is Prime or Composite?


#include<iostream>
#include<process.h>
using namespace std;
int main()
{

int num,count=0;
cout<<"Enter a Number to Check prime or not:  ";
cin>>num;
if (num < 0)
{
cout << "Plz Enter +ve Integer Value"<<endl;
exit(1);
}
for(int a=1;a<=num;a++)
{
if(num%a==0)
{
count++;
}
}
if(count==2)
{
cout<<num<<" is a Prime Number. \n";
}
else
{
cout<<num<<" is Composit Number. \n";
}
return 0;
}