Friday, 21 October 2016

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 !