Thursday, 28 August 2014

,

C++ Program to Swap Two Numbers

C++ Program to Swap Two Numbers:Swapping means to interchange the value of two variables.For example if  variable a store 10 and another variable b store 20 then after swapping  b has 10 and a has 20. In C/C++ we can do the swapping in two way :- 
  • using extra variable
  • without using extra variable
C++ Program to Swap Two Numbers


C++ Program to Swap Two Numbers using extra variable

#include <iostream>
using namespace std;

int main() {
    
    int a = 25, b = 15, temp;
    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    temp = a;
    a = b;
    b = temp;

    cout << "\nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;


    return 0;
}

Output

Before swapping.a = 25, b = 15 
After swapping. a = 15, b = 25

C++ Program to Swap Two Numbers without using extra variable


#include <iostream>
using namespace std;

int main() {
    
    int a = 25, b = 15;

    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    a = a + b;
    b = a - b;
    a = a - b;

    cout << "\nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    return 0;
}


Output

Before swapping.a = 25, b = 15 
After swapping. a = 15, b = 25
Publisher: Anand - 12:23

Wednesday, 27 August 2014

,

C++ program to add two arrays

C++ program to add two arrays:This program first input some element of two array and perform the addition work correspondingly along with arrays element, that means first element of first array add with first element of second array and print the sum on output screen.

C++ program to add two arrays

C++ program to add two arrays



#include<iostream>
 
main()
{
   int first[20], second[20], c, n;
 
   cout << "Enter the number of elements in the array ";
   cin >> n;
 
   cout << "Enter elements of first array " << endl;
 
   for ( c = 0 ; c < n ; c++ )
      cin >> first[c];
 
   cout << "Enter elements of second array " << endl;
 
   for ( c = 0 ; c < n ; c++ )
      cin >> second[c];
 
   cout << "Sum of elements of two arrays " << endl;
 
   for ( c = 0 ; c < n ; c++ )
      cout << first[c] + second[c] << endl;
 
   return 0;
}

Output


Enter the number of elements in the array 5
Enter elements of first array 3 4 2 6 7
Enter elements of second array 5 3 7 5 2
Sum of elements of two arrays 8 7 9 11 9

Publisher: Anand - 05:37
,

C++ program to generate Fibonacci series

Fibonacci series is a sequence of numbers in which every number is equal to the sum of  previous two numbers.Here a program is given below which take the first two number and the number of terms as input  and print a series on output screen.
C++ program to generate Fibonacci series

C++ program to generate Fibonacci series

#include<iostream.h>
int main()
{
int first, second, sum, num, counter = 0;
cout<<"\nEnter the term : ";
cin>>num;
cout<<"\nEnter First Number : ";
cin>>first;
cout<<"\nEnter Second Number : ";
cin>>second;
cout<<"\nFibonacci Series :"<<first<<" "<<second;
while (counter < num)
{
sum = first + second;
cout<<sum;
first = second;
second = sum;
counter++;
}
return (0);
}


 Output:
Enter the term : 5
Enter First Number : 1
Enter Second Number :2


Fibonacci Series : 1 2 3 5 8

Publisher: Anand - 05:25
,

C++ program to check number is prime or not

The number which is only  divisible by one and itself is called prime number.For example 13 is a prime number which is only divisible by 1 and 13.There are various way to find prime number but here we provide an easy C++ program to check number is prime or not .


C++ program for prime numbers


Logic of the program

There are many methods to construct a program to check whether a number is prime or not in C++ , this methods uses for loop for the program.
  • First you should know the concept of prime number.
  • Now Prime is a number which is divisible only by 1 or itself.
  • So if the number to be checked is divided with certain number other than 1 or itself, and the remainder is zero, this means that the number is not prime as it has been fully divided by another number.
  • Every number is divisible by 1 so exclude the 1 and start the loop from 2 to the number to be checked,
  • Then divide the number with every number from 2 to less than that number and then if the number is not divisible then the number is prime number

C++ program to check number is prime or not

#include<iostream.h>
#include<conio.h>
void  main()
{
  int num,p=0;
  cout<<"\n Enter a number : ";
  cin>>num;
for(int i=2;i<=num;i++)
{
  if(num%i==0)
  {
    p++;break;
 }
}
if(p==0)
{
   cout<<"\n Number is prime";
}
else
{
    cout<<"\n Number is not prime";
}
getch();
}


Output 1

Enter a number : 13
Number is prime

Output 2

Enter a number : 15
Number is not prime

Publisher: Anand - 05:03
,

C++ program to add two complex numbers

C++ program to add two complex numbers:Complex number means the combination of real number and imaginary number, which is in the form of  a + ib, where a is the real number and ib is imaginary number.This program accept the real and imaginary parts of two complex number and add them.Sum of two complex number is also a complex number.


C++ program to add two complex numbers

C++ program to add two complex numbers

#include <iostream.h>
 

class complex
{
   public :
      int real, img;
};
 
int main()
{
   complex a, b, c;
 
   cout << "Enter a and b where a + ib is the first complex number.";
   cout << "\na = ";
   cin >> a.real;
   cout << "b = ";
   cin >> a.img;
   cout << "Enter c and d where c + id is the second complex number.";
   cout << "\nc = ";
   cin >> b.real;
   cout << "d = ";
   cin >> b.img;
 
   c.real = a.real + b.real;
   c.img = a.img + b.img;
 
   if ( c.img >= 0 )
      cout << "Sum of two complex numbers = " << c.real << " + " << c.img << "i";
   else
      cout << "Sum of two complex numbers = " << c.real << " " << c.img << "i";
 
   return 0;
}

Output

C++ program to add two complex numbers

Publisher: Anand - 03:06
,

C++ program to reverse a number

C++ program to reverse a number: Reverse or invert the input number. This can be useful to check if an integer is a palindrome or not.

C++ program to reverse a number


C++ program to reverse a number


#include <iostream>
 

class Operations
{
  long c;
 
public:
  void inputNumber()
  {
    cout << "Input a number\n";
    cin >> c;
  }
 
  long reverseNumber()
  {
    long invert = 0;
 
    while (c != 0)
    {
      invert = invert * 10;
      invert = invert + c%10;
      c = c/10;
    }
 
    return invert;
  }
 
};
 
int main()
{
  long result;
 
  Operations t;
  t.inputNumber();
  result = t.reverseNumber();
 
  cout << "Number obtained on reversal = " << result;
 
  return 0;
}


Output

Input a number 465
Number obtained on reversal = 564
Publisher: Anand - 02:40
,

C++ program to add two numbers

Here is simple C++ program to add two number is given below, which simply take two number and print the sum on output screen.

C++ program to add two numbers






C++ program to add two numbers


#include <iostream>
 

int main()
{
   int num1, num2, sum;
 
   cout << "Enter two numbers to add\n";
   cin >>num1>>num2;
 
   sum = num1 + num2;
   cout <<"Sum of entered numbers = " << sum << endl;
 
   return 0;
}

Output
Enter two numbers to add 6 8
Sum of entered numbers = 14

C++ program to add two numbers using class


#include <iostream>
 
class Mathematics 
{
  int num1,num2;
 
public:
  void input() {
    cout << "Input two integers\n";
    cin >>num1 >>num2;
  }
 
  void add() {
    cout << "Result = " << num1 + num2;
  }
 
};
 
int main()
{
   Mathematics m; // Creating object of class
 
   m.input();
   m.add();
 
   return 0;
}


Output
Input two integers  6 8
Result = 14


Explanation

We create Mathematics class with two functions input() and add(). First function is used to get two integers and add() performs addition and display the result. Similarly you can add more functions such as subtract, multiply, divide etc.
Publisher: Anand - 02:13
,

C++ hello world program

This is the first C++ program which simply prints Hello World on output screen.As you know that  in C++ the object cin and cout are used instead of scanf() and printf() function in C.

C++ hello world program

C++ hello world program

#include<iostream.h>
#include<conio.h>
 void main()
   {
       cout<<"\n Hello World ";
       getch();
   }

Output

Hello World


C++ hello world program using class

#include<iostream>
 
// Creating class
 
class Message
{
  public:
 
    void display() {
      cout << "Hello World\n";
    }
};
 
int main()
{
 Message c;    // Creating object
 c.display();  // Calling function
 
 return 0;
}


Output

Hello World

Publisher: Anand - 01:56

Tuesday, 26 August 2014

,

C program to Display Factors of a Number

C program to Display Factors of a Number : This program takes a positive integer from an user and displays all the factors of that number.


C program to Display Factors of a Number


C program to Display Factors of a Number



#include <stdio.h>
int main()
{
  int n,i;
  printf("Enter a positive integer: ");
  scanf("%d",&n);
  printf("Factors of %d are: ", n);
  for(i=1;i<=n;++i)
  {
      if(n%i==0)
         printf("%d ",i);
  }
  return 0;
}



Output

Enter a positive integer:50
Factors of 50 are: 2 5 5

Explanation

In this program, an integer entered by user is stored in variable n. Then, for loop is executed with initial condition i=1 and checked whether n is perfectly divisible by i or not. If n is perfectly divisible by i then, i will be the factor of n. In each iteration, the value of i is updated(increased by 1). This process goes not until test condition i<=n becomes false,i.e., this program checks whether number entered by user is perfectly divisible by all numbers from 1 to n and all displays factors of that number.

Publisher: Anand - 10:55
,

C Program to Calculate Difference Between Two Time Period

In this program, user is asked to enter two time periods and these two periods are stored in structure variables. This program calculates the difference between these two time period. To perform this task, a function is created which calculates the difference and the result is displayed in main( ) function without returning it (Using call by reference).
C Program to Calculate Difference Between Two Time Period

C Program to Calculate Difference Between Two Time Period


#include <stdio.h>
struct TIME{
  int seconds;
  int minutes;
  int hours;
};
void Difference(struct TIME t1, struct TIME t2, struct TIME *diff);
int main(){
    struct TIME t1,t2,diff;
    printf("Enter start time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d%d%d",&t1.hours,&t1.minutes,&t1.seconds);
    printf("Enter stop time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d%d%d",&t2.hours,&t2.minutes,&t2.seconds);
    Difference(t1,t2,&diff);
    printf("\nTIME DIFFERENCE: %d:%d:%d - ",t1.hours,t1.minutes,t1.seconds);
    printf("%d:%d:%d ",t2.hours,t2.minutes,t2.seconds);
    printf("= %d:%d:%d\n",diff.hours,diff.minutes,diff.seconds);
    return 0;
}
void Difference(struct TIME t1, struct TIME t2, struct TIME *differ){
    if(t2.seconds>t1.seconds){
        --t1.minutes;
        t1.seconds+=60;
    }
    differ->seconds=t1.seconds-t2.seconds;
    if(t2.minutes>t1.minutes){
        --t1.hours;
        t1.minutes+=60;
    }
    differ->minutes=t1.minutes-t2.minutes;
    differ->hours=t1.hours-t2.hours;
}

Output

C Program to Calculate Difference Between Two Time Period

Publisher: Anand - 10:38
,

C Program to Store Information of a Student Using Structure

In this program, a structure(studentinfo) is created which contains name, roll and marks as its data member. Then, a structure variable( s ) is created. Then, data (name, roll and marks) is taken from user and stored in data members of structure variable s. Finally, the data entered by user is displayed.


C Program to Store Information of a Student Using Structure

C Program to Store Information of a Student Using Structure



#include <stdio.h>
struct studentinfo
{
    char name[50];
    int roll;
    float marks;
};
int main(){
    struct studentinfo std;
    printf("Enter information of students:\n\n");
    printf("Enter name: ");
    scanf("%s",std.name);
    printf("Enter roll number: ");
    scanf("%d",&std.roll);
    printf("Enter marks: ");
    scanf("%f",&std.marks);
    printf("\nDisplaying Information\n");
    printf("Name: %s\n",std.name);
    printf("Roll: %d\n",std.roll);
    printf("Marks: %.2f\n",std.marks);
    return 0;
}



Output

C Program to Store Information of a Student Using Structure

Publisher: Anand - 09:05
,

C program to Calculate the Power of a Number

C program to Calculate the Power of a Number :This program below takes two integers from user, a base number and an exponent. For example: In case of 23, 2 is the base number and 3 is the exponent of that number. And this program calculates the value of baseexponent.

C program to Calculate the Power of a Number


C program to Calculate the Power of a Number


#include <stdio.h>
int main()
{
  int base, exp;
  long long int value=1;
  printf("Enter base number and exponent respectively: ");
  scanf("%d%d", &base, &exp);
  while (exp!=0)
  {
      value*=base;  /* value = value*base; */
      --exp;
  }
  printf("Answer = %d", value);
}

Output

Enter base number and exponent respectively:2 4
 
Answer = 16

Explanation
This program takes base number and exponent from user and stores in variable base and exp respectively. Let us suppose, user entered 2 and 4 respectively. Then,while loop is executed with test condition exp!=0. This test condition will be true and value becomes 2 after first iteration and value of exp will be decreased to 2. This process goes on and after fourth iteration, value will be equal to 16(2*2*2*2) and exp will be equal to 0 and while loop will be terminated. Here, we have declared variable value of type long long int because power of a number can be very large.
This program can only calculate the power if base power and exponent are integers. If you need to the calculate power of a floating point number then, you can use pow() function
Publisher: Anand - 06:46