Friday, 21 October 2016

Alternate Elements of 2-D Array

C++ PROGRAMS


In this program the number of rows, columns and the elements of the 2-D Array are entered by the user. The output is the arranged Array (just like the output of the previous program 2-D Array) and the Alternate Elements of the Array starting from the first element.

The Code consists a user-defined function in which the Array, number of rows and number of columns are passed as arguments.

Code:

#include<iostream.h>
#include<conio.h>

void alternate(int A[10][10], int r, int c)
{
int i,j;
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  if((i+j)%2==0)
  cout<<A[i][j]<<" ";
 }
}

void main()
{
clrscr();
int i,j,r,c,A[10][10];
cout<<"Enter the number of Rows: ";
cin>>r;
cout<<"Enter the number of columns: ";
cin>>c;
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  cin>>A[i][j];
 }
}
clrscr();
cout<<"Your Array is: \n";
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  cout<<A[i][j]<<" ";
 }
 cout<<"\n";
}

cout<<"\nAlternate Elemnts of Array are: ";

alternate(A,r,c);

getch();
}

Output:









For any queries or suggestions please comment.

2-D Array

C++ PROGRAMS


The following program takes the values for the elements of a 2-D Array and arranges and output is the arranged 2-D Array. Initially the program also asks the user to enter the number of rows and columns, so that it can arrange the elements accordingly.

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,r,c,A[10][10];
cout<<"Enter the number of Rows: ";
cin>>r;
cout<<"Enter the number of columns: ";
cin>>c;

for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  cin>>A[i][j];
 }
}

clrscr();
cout<<"Your Array is: \n";
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  cout<<A[i][j]<<" ";
 }
 cout<<"\n";
}

getch();
}

Output:





For any suggestions or queries please comment !

Monday, 26 September 2016

Fibonacci Series

C++ PROGRAMS


Fibonacci Series a series of numbers in which each number, Fibonacci number, is the sum of the two preceding numbers. 
The simplest is the series 0,1, 1, 2, 3, 5, 8, etc.

The following program asks the user that how many Fibonacci numbers are required (minimum 3) and the output is the series till that number.

The program uses simple for-loop.

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,n,i,sum;
cin>>n;
cout<<"Fibonacci Series: "<<"\n";
a=0;
b=1;
sum=0;
cout<<a<<"\n"<<b<<"\n";

for(i=1;i<=n-2;i++)
{
 sum=a+b;
 cout<<sum<<"\n";
 a=b;
 b=sum;
}
getch();

}

Output:

First Run:


Second Run:







LCM

C++ PROGRAMS


This program will help the user get the Least Common Multiple(LCM) of any two numbers entered.

This program also uses simple for-loop.

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,i;
cout<<"Enter any two numbers: ";
cin>>a>>b;

for(i=a;i<=a*b;i+=a)
{
 if(i%b==0)
 break;
}
cout<<"LCM: "<<i;

getch();
}

Output:

First Run:


Second Run:









For any queries or suggestions please comment.

HCF/GCF

C++Programs


The following program is to find the Highest Common Factor (HCF) of any two numbers entered by the user.

The program uses simple for loop.

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,i;
cout<<"Enter any two numbers: ";
cin>>a>>b;

for(i=a;i>=1;i--)
{
 if(a%i==0&&b%i==0)
 break;
}
cout<<"HCF: "<<i;

getch();
}

Output:

First Run:

Second Run:






For any queries or suggestions please comment.

Swapping Without Third Variable

C++ PROGRAMS


The previous program (Swapping) used a third variable t which helped in swapping. However swapping can also be done without a third variable. 

In this we use simple logic of addition and subtraction.

Code:

#include<iostream.h>
#include<conio.h>

void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;

cout<<a<<" "<<b;

}

void main()
{
int a,b;
cin>>a>>b;
swap(a,b);

getch();

}


Output:

First Run:

Second Run:







For any queries or suggestions please comment !

Friday, 23 September 2016

Swapping

C++ PROGRAMS


Swapping is a very important and useful program that is frequently used in advanced programming. In this, any two numbers are entered by the user and they get swapped and output is the swapped arrangement of the numbers.

For example:
User enters: 12 13
Output:         13 12

In this I will use an user-defined-function for swapping and use three variables - two for the numbers entered and the third one will help in swapping the values.

Code:

#include<iostream.h>
#include<conio.h>

void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
cout<<a<<" "<<b;

}

void main()
{
int a,b;
cin>>a>>b;
swap(a,b);

getch();

}

Output:


First Run:

Second Run:




It's interesting that Swapping can also be done without using a third variable. To know how click on Swapping Without Third Variable





For any queries or suggestions please comment !



Saturday, 17 September 2016

Prime Numbers Between Two Numbers

C++ Programs


In the previous program (Prime and Composite Numbers) the user had entered any number and the output stated whether it was a prime or a composite number. 
In this program, the user will enter any two different numbers and the output will be all the prime numbers between those two numbers.

For this, I am using user-defined-function in which I will be using for-loops and if-statements.


Code:

#include<iostream.h>
#include<conio.h>

void prime(int a,int b)
{
int i,j,k;
for(i=a;i<=b;i++)
 {
  k=0;
  for(j=2;j<=i/2;j++)
   {
    if(i%j==0)
    k=1;
   }
  if(k==0)
  cout<<i<<"\n";
 }
}

void main()
{
clrscr();
int a,b;
cout<<"\t"<<"\t"<<"Prime numbers between two numbers"<<"\n";
cout<<"From: ";
cin>>a;
cout<<"To: ";
cin>>b;

prime(a,b);

getch();
}

Output:

First Run:

Second Run:



Note: For finding composite numbers between any two numbers apply the same code but with just a slight change. In the prime function, in the outermost loop, the condition for the if statement has to be changed, i.e.,:

void prime(int a,int b)
{
int i,j,k;
for(i=a;i<=b;i++)
 {
  k=0;
  for(j=2;j<=i/2;j++)
   {
    if(i%j==0)
    k=1;
   }
  if(k==1)
  cout<<i<<"\n";
 }
}

It gives all the composite numbers between any two numbers.








For any queries or suggestions please comment !

Thursday, 15 September 2016

Prime and Composite Numbers

C++ PROGRAMS


Prime Numbers are those natural numbers whose factors (which must be natural numbers too) are 1 and the number itself only.
For example: 2, 3, 5, 7, 11, 13, 17, 19, .....

Composite Numbers are those natural numbers which have more than two factors, which include 1 and the number itself also.
For example: 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, .....

Note: 1 is neither prime nor composite number.

In the following program, user will enter any natural number and output will be whether the number is a prime or a composite number. However if user enters any number less than 2 the output will be neither prime nor composite.

In this program I will use for-loop and if statement.


Code:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,i;
cout<<"Prime and Composite Numbers"<<"\n";
cout<<"Enter any natural number: ";
cin>>a;

if(a<2)
cout<<"Neither Prime nor Composite";

if(a>=2)
{
 b=1;
 for(i=2;i<=a/2;i++)
 {
  if(a%i==0)
  b=0;
 }

 if(b==0)
 cout<<"A composite number";

 if(b==1)
 cout<<"A prime number";
}

getch();
}

Output:

First Run:

Second Run:

Third Run:





For finding all the prime numbers between any two number, please go through Prime Numbers Between Two Numbers.



For any queries or suggestions please comment !


Tuesday, 13 September 2016

Powers II

C++ PROGRAM


POWERS (using in-built function)


The code of this program will be shorter than the previous one (Powers I) because in this I will use the in built function, pow(a,b), to calculate powers.
This function is available in the header file: <math.h>.

This is even more useful because we can also find fractional powers of numbers such as 
4^0.5, which is it's square root, i.e, 2.

Code:

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
double a,b;
cout<<"Powers"<<"\n";
cout<<"Enter any number: ";
cin>>a;
cout<<"Raised to power: ";
cin>>b;

cout<<"Ans. "<<pow(a,b);

getch();
}

Output:

First Run:


Second Run:

  Third Run:








For any queries or suggestions please comment !

Powers I

C++ PROGRAM


POWERS (without using in-built function)


In this program the user will enter any number 'a' and the power 'b' (which can be any whole number), and the output will be a^b, that is, 'a' raised to the power 'b'.

In this program I will not use the in-built function for calculating powers. I will use only 
for-loop.

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
double a,b,i,p=1;
cout<<"Powers"<<\n";
cout<<"Enter any number: "
cin>>a;
cout<<"Raised to the power: ";
cin>>b;

for(i=1;i<=b;i++)
{
 p=p*a;
}

cout<<"Ans. "<<p;

getch();

}

Output:

First Run:

Second Run:





In the next program (Powers II), I will use in-built function for calculation of powers.



For any queries or suggestions please comment !

Factorial

C++ PROGRAMS


Factorial of any natural number is the product  of that number and all the natural numbers less than that. It is indicated with the help of the sign: ! . 
For example:
4!= 4 X 3 X 2 X 1 = 24
8!= 8 X 7 X 6 X 5 X 4 X 3 X 2 X 1 = 40320

Note: 0! = 1

In the following program the user enters any natural number and the output is the factorial of that number.

In this I will use for-loop.

Code:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long a,i,fact=1;
cout<<"Factorial"<<"\n";
cout<<"Enter any natural number: ";
cin>>a;

for(i=1;i<=a;i++)
{
 fact=fact*i;
}

cout<<"Ans. "<<fact;

getch();
}


Output: 

First Run:

Second Run:




Note: if you enter a number more than 10, then sometimes the output may be a junk value(random value). This is because the factorials of those numbers are very large.





For any queries or suggestions please comment !