Showing posts with label Simple Programs. Show all posts
Showing posts with label Simple Programs. Show all posts

Sunday, 31 August 2014

,

C++ program to perform insertion and deletion operation in array

C++ program to perform insertion and deletion operation in array:A simple C+ + program related to array which perform insertion and deletion operation.It is a menu driven program which ask the choice for insertion or deletion and according to user choice it perform operation.


C++ program to perform insertion and deletion operation in array

C++ program to perform insertion and deletion operation in array

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

void main()
{
clrscr();
int n,x,c=0,e,ch,i,a[50],j;
cout<<"Enter Array lim"<<endl;
cin>>n;
cout<<"Enter array"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter Choice  1:Deletion  2Insertion"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter to be deleted"<<endl;
cin>>x;
for(i=0;i<n;i++)
{

if(a[i]==x)
{c++;
for(j=i;j<n;j++)
{
a[j]=a[j+1];
}
}
}
if(c==0)
cout<<"ENP"<<endl;
for(i=0;i<n-c;i++)
cout<<a[i]<<endl;
break;

case 2:
cout<<"Enter What & Where to be inserted"<<endl;
cin>>e>>x;
for(i=n;i>=x;i--)
a[i]=a[i-1];
a[x-1]=e;
for(i=0;i<=n;i++)
cout<<a[i]<<endl;
break;
}
getch();
}
Publisher: Anand - 04:56
,

C++ program to find the sum of diagonal elements

C++ program to find the sum of diagonal elements : A simple C++ program which find the sum of diagonal element of matrices.There are two diagonal in a matrix primary diagonal and secondary diagonal, so this program first ask the choice about diagonal from user and then print the total.

C++ program to find the sum of diagonal elements



C++ program to find the sum of diagonal elements 


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

void main()
{
clrscr();
int i,s=0,j,r,c,ch,a[50][50];
cout<<"Entr Array Limit"<<endl;
cin>>r>>c;
if(r!=c)
cout<<"Wrong Choice"<<endl;
else
{
cout<<"Enter Array"<<endl;
  for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
   cin>>a[i][j];
   }
  }
cout<<"Enter Choice 1:main  2:Secondary"<<endl;
cin>>ch;
switch(ch)
{
case 1:
  for(i=0;i<r;i++)
  {
  s=s+a[i][i];
  }
cout<<"Sum = "<<s;
break;

case 2 :
j=c-1;
  for(i=0;i<r;i++)
  {
  s=s+a[i][j--];
  }
  cout<<"Sum = "<<s;
break;

default :
cout<<"Wrong Choice"<<endl;
break;

}
}
getch();
}
Publisher: Anand - 03:57
,

C++ program to convert decimal number to binary and its vice verse

C++ program to convert decimal number to binary and its vice verse:A simple C++ program which accept decimal number and convert it into binary number and it also convert binary number to decimal number.

C++ program to convert decimal number to binary and its vice verse


C++ program to convert decimal number to binary and its vice verse

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

void main()
{
clrscr();
int a,g,h,s=0,i=0,d,b,ch;
cout<<endl<<"Enter the choice - "<<endl<<"1:Binary to Decimal"<<endl<<"2:Decimal to Binary"<<endl;
cin>>ch;
switch(ch)
{
case 1:

cout<<"Enter the Binary Digit"<<endl;
cin>>b;
while(b>0)
{
a=b%10;
s=s+a*(pow(2,i));
i=i+1;
b=b/10;
}
cout<<"Answer ="<<s<<endl;
break;

case 2:
cout<<"Enter the Decimal"<<endl;
cin>>d;
while(d>0)
{
a=d%2;
a=a*pow(10,i);
s=s+a;
i=i+1;
d=d/2;
}
cout<<"Answer ="<<s<<endl;
break;

default:
cout<<"Wrong choice"<<endl;
break;
}
getch();

}




Output 

Enter the choice -
1:Binary to Decimal
2:Decimal to Binary 1
Enter the Binary Digit 1000001
Answer = 65
Publisher: Anand - 03:39
,

C++ program to print the default value of variable

C++ program to print the default value of an integer variable: A simple C++ program which print some values on output screen, which are already assigned into those variable.
C++ program to print the default value of  variable

C++ program to print the default value of an integer variable

#include <iostream.h>#include <conio.h> void main(){ clrscr(); int x = 10; float y = 10.1; char z = 'a'; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; getch();}
OUTPUT: x = 10 y = 10.1 z = a
Publisher: Anand - 03:09
,

C++ program to convert binary number to octal and its vice verse

C++ program to convert binary number to octal and its vice verse:Here is a C++ program which convert the binary number into octal manually.

C++ program to convert binary number to octal and its vice verse


C++ program to convert binary number to octal and its vice verse

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

void main()
{
clrscr();
int oc,p,r,ch,n,a,i=0,x=0,d=0,j,o;
long int b,s=0,sum=0;
cout<<"Enter Choice"<<endl;
cout<<"1.B-O"<<endl<<"2.O-B"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"ENTER THE BINARY DIGIT  :"<<endl;
cin>>b;
while(b>0)
{
a=b%1000;
j=0;
d=0;
while(a>0)
{
n=(a%10);
d=d+(n*pow(2,j));
j=j+1;
a=a/10;
}
x=x+(d*pow(10,i));
i=i+1;
b=b/1000;
}
cout<<"Answer ="<<x;
break;

case 2:
cout<<"Enter the Octel"<<endl;
cin>>oc;
i=0;
j=0;
sum=0;
s=0;

while(oc>0)
{
p=oc%1000;
while(p>0)
{
r=p%2;
s=s+r*pow(10,j);
j++;
p=p/2;
}
sum=sum+(s*pow(1000,i));
i++;
oc=oc/10;
j=0;
}
cout<<"Binary ="<<sum;
break;

default:
cout<<"????????????????????????"<<endl;
break;
}
getch();
}

Publisher: Anand - 03:01
,

C++ program to perform basic arithmetic operations

C++ program to perform basic arithmetic operations:A simple c++ program which perform various arithmetic operation


C++ program to perform basic arithmetic operations

C++ program to perform basic arithmetic operations


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

void main()
{
  clrscr();
  int x = 10;
  int y = 2;
  int sum, difference, product, quotient;
  sum = x + y;
  difference = x - y;
  product = x * y;
  quotient = x / y;
  cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl;
  cout << "The difference of " << x << " & " << "y <<  is " << difference << "." << endl;
  cout << "The product of " << x << " & " << y << " is " << product << "." << endl;
  cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl;
  getch();
}

OUTPUT :


The sum of 10 & 2 is 12.
The difference of 10 & 2 is 8.
The product of 10 & 2 is 20.
The quotient of 10 & 2 is 5.
Publisher: Anand - 02:50

Thursday, 28 August 2014

,

C++ program to check whether number is armstrong or not

A number in which the sum of cube of its individual digits is equal to the number itself is called Armstrong number
For Example: 1^3 + 5^3 + 3^3 = 153
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number.

C++ program which takes input a number and check whether it is Armstrong Number or not.

C++ program to check whether number is armstrong or not

C++ program to check whether number is armstrong or not



 #include<iostream.h>
  using namespace std;
  int main()
  {
  int armstrong=0,num=0,result=0,check;
  cout<<"Enter Number to find it is an Armstrong number ";
       cin>>num;
       check=num;
       for(int i=1;num!=0;i++){
           armstrong=num%10;
           num=num/10;
           armstrong=armstrong*armstrong*armstrong;
           result=result+armstrong;
       }
       if(result==check){
       cout<<check<<"  is an Armstrong Number";
       }
       else{
       cout<<check<<"  is NOT an Armstrong Number";
       }
       return 0;
    }


Output

Enter Number to find it is an Armstrong number 153
153 is an Armstrong Number
Publisher: Anand - 14:43
,

C++ program to find factorial of any number

The factorial of a number 'n' is the product of all number from 1 upto the number 'n'
it is denoted by n!.  For example n=5 then factorial of 5 will be 1*2*3*4*5= 120. 5!= 120

C++ program to find factorial of any number

Factorial program C++ Logic:
  • First think what is the factorial of a number? How mathematically it can be calculated.
  •  If you got this info then it will be very easier to make a C++ Program logic to find the factorial.
  •  User enters a number and we have to multiply all numbers upto entered number.
  • Like if user enters 6  then Factorial should be equal to factorial= 1*2*3*4*5*6.
  • In this case a for Loop will be very helpful. It will start from one and multiply all numbers upto entered number after it loop will be terminated.
  • Take a variable and initialized it to 1 and in loop store multiplication result into it like in below program a variable 
  • Factorial is used for this purpose.what is we does not initialized it to 1 and initialized it to zero or remain it uninitialized. In case of 0 our result will be zero in case of any number entered
  • In case of not initializing it our answer will correct mostly but if variable contains garbage value then we will not be able to get correct result. 
  • It is recommended that to initialize it to one.

C++ program to find factorial of any number


#include<iostream.h>

using namespace std;

int main()

{

    int num,factorial=1;

    cout<<" Enter Number To Find Its Factorial:  ";

    cin>>num;

    for(int i=1;i<=num;i++)

    {

        factorial=factorial*i;

    }

cout<<"Factorial of Given Number is ="<<factorial<<endl;

    return 0;

}



Output

Enter Number To Find Its Factorial: 6
Factorial of Given Number is = 720
Publisher: Anand - 13:30
,

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 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

Sunday, 24 August 2014

,

C program to swap two string

Here is simple C program to swap two string is given below.It takes two string using gets function and using a build in  function strcpy() ,which is use to copy the string in variable.
C program to swap two string


C program to swap two string


#include <stdio.h>
#include <string.h>
#include <malloc.h>
 
int main()
{
   char String1[100], String2[100], *temp;
 
   printf("Enter the first string\n");
   gets(String1);
 
   printf("Enter the second string\n");
   gets(String2);
 
   printf("\nBefore Swapping\n");
   printf("First string: %s\n",String1);
   printf("Second string: %s\n\n",String2);
 
   temp = (char*)malloc(100);
 
   strcpy(temp,String1);
   strcpy(String1,String2);
   strcpy(String2,temp);
 
   printf("After Swapping\n");
   printf("First string: %s\n",String1);
   printf("Second string: %s\n",String2);
 
   return 0;
}


Output

C program to swap two string

Publisher: Anand - 03:51