Thursday, December 25, 2014

Some very basic programs in C++

Here are few codes written in C++ (dev C) for practice

<-------- Program to calculate x raise to power y where x is base and y is exponent
------------>
#include <iostream>

using namespace std;

int main()
{
 int a,b,c,d=1;
 cout<<"Enter the Base number"<<endl;
 cin>>a;
 cout<<"Enter the exponent"<<endl;
 cin>>b;
 for(c=1; c<=b; c++)
 {
  d=a*d;
  
 }
 
 cout<<d<<endl;
 return 0;
 
 
}
<----------------------------------->

<------- Printing multiple tables----------->
#include <iostream>

using namespace std;

int main()
{
 int a,c,b; 
    for (b=1; b<=5; b++)
    {

 
 for (a=1; a<=10; a++) 
 {
  c=b*a;
  cout<<b<<"*"<<a<<"="<<c<<endl;
 }
    }

 return 0;
}
<----------------------------------->

<--------Simple Table of 5 (we can ask for input from user to make it generic)
-------------->

#include <iostream>

using namespace std;

int main()
{
 int a,c;
 int b=5;
 
 for (a=1; a<=10; a++) 
 {
  c=b*a;
  cout<<b<<"*"<<a<<"="<<c<<endl;
 }
 

 return 0;
}

<---------------------------------->

<------Factorial----------->

#include <iostream>

using namespace std;

int main()
{
 int a,i;
 int c=1;
 cout<<"Enter the number"<<endl;
 cin>>a;
  for(i=1; i<=a; i++)
  {
   c=i*c;
   
  }
  cout<<c;
}

<------------------------->
 
<!--------------Fibonacci Series----------------------->
 
#include<iostream>

using namespace std;

int main()
{
int a=1,b=1,c,e;
cout<<a<<endl<<b<<endl;
for (e=1; e<=10; e++)
{

c=a+b;
a=b;
b=c;
cout<<c<<endl;
}

return 0;
}
<!----------------------------------------------------------------------------> 




<-----------------------------------Right.angled.triangle.with.asterisks------------> 

#include <iostream>

using namespace std;

int main()
{
int i,a;

for(i=1; i<=3; i++)
{
cout<<endl;
for (a=1; a<=i; a++)
{
cout<<'*';
}

}

return 0;
}
<-------------------------------------------------->
 
<--------------------Reverse right angle triangle with asterisk--->
#include <iostream>

using namespace std;

int main()
{
 int a,b,c;
 cout<<"enter the number of loops"<<endl;
 cin>>c;
 for(a=c; a>=1;a--)
 {
  cout<<endl;
  for(b=a; b>=1; b--)
  {
   cout<<'*';
  }
 }
 return 0;
}
<---------------------------------------------------------> 

No comments:

Post a Comment