Monday, March 31, 2014

C++ Program to find whether the number is Armstrong or not

C++ Program to find whether the number is Armstrong or not

#include<iostream.h>
#include<conio.h>
class amstrong
{
int n,nd,n2,n3,an,re,re1;
public:
void inputnumb()
{
cout<<"Program to find Amstrong Number\n";
cout<<"Enter the number\n";
cin>>n;
}
void no_digit()
{
n2=n;
nd=0;
do
{
n2=n2/10;
nd++;
}
while(n2!=0);
}
void calculation()
{
an=0;
n3=n;
do
{
int i;
re=n3%10;
re1=re;
for(i=0;i<nd-1;i++)
re=re*re1;
an=an+re;
n3=n3/10;
}
while(n3!=0);
}
void output()
{
cout<<"Entered number is of "<<nd<<" digits\n";
if(n==an)
cout<<"Number is Amstrong number";
else
cout<<"Number is not Amstrong number";
}
};
void main()
{
clrscr();
amstrong a;
a.inputnumb();
a.no_digit();
a.calculation();
a.output();
getch();
}

C Program to print call of day (habits)

C Program to print call of day (habits)

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter integer time\n");
scanf("%d",&a);
switch(a)
{
case 6:
printf("waking up");
break;
case 7:
printf("taking bath");
break;
case 8:
printf("Reading news paper");
break;
case 9:
printf("Having Break fast");
break;
case 10:
printf("Going Class Room");
break;
default:
printf("Undergoing class");
}
getch();
}

Sunday, March 30, 2014

CPP Program to demonstrate how to call function by reference

CPP Program : how to call function by reference


Calling by reference is new concept which is not available in C language. It is not passing address just like in C. but it is giving another name to an existing variable, just like an alias. Don't mistake this as call by reference in C. In C call by reference and call by address is same thing. But in CPP both are different, having some similarities. Call by reference is simpler than call by address. But you cannot use reference all the place you are using address. This concept is clear from the following example

#include<conio.h>
#include<iostream>
using namespace std;
int addition (int &a, int &b)// declaration of called function using & sign
{
int y;
y=a+b;
return (y);
}
int main()
{
int x,y,sum;
//clrscr();
cout<<"Enter two integers to be added\n";
cin>>x>>y;
sum=addition(x,y);// calling function just like calling function by value
cout<<"Sum of the integers="<<sum;
getch();
return 0;
}

Saturday, March 29, 2014

C program to implement stack using array

C program to implement stack using array

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 10
void push();
int pop();
void traverse();
int stack[MAXSIZE];
int Top=-1;
void main()
{
int choice;
char ch;
do
{
clrscr();
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.TRAVERSE");
printf("\nEnter your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:printf("\n The deleted element is %d",pop());
break;
case 3:traverse();
break;
default: printf("\n You entered wrong choice\n");
}
printf("\n Do you want to continue (Y/N\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='Y'||ch=='y');
}
void push()
{
int i;
int item[5];
if(Top==MAXSIZE-1)
 {
printf("The stack is full\n");
getch();
exit(0);
 }
else
 {
printf("Enter the five elements to be inserted");
for (i=0;i<5;i++)
{
scanf("%d",&item[i]);
Top=Top+1;
stack[Top]=item[i];
 }
 }
 }
int pop()
{
int item;
if(Top==-1)
{
printf("The stack is empty");
getch();
exit(0);
}
else
{
item=stack[Top];
Top=Top-1;
}
return(item);
}
void traverse()
{
int i;
if (Top==-1)
{
printf("The stack is Empty");
getch();
exit(0);
}
else
{
for (i=Top;i>=0;i--)
{
//printf("traverse the element");
printf("\n%d",stack[i]);
}
}
}

C Program to print calender of a perticular month

C Program to print calender of a perticular month

Use microsoft visual Studio or Code::Blocks Ides to run this subject. In Turb C++ It faces some Problems

#include<stdio.h>
#include<conio.h>
int main()
{
enum day
{
mon,tue,wed,thu,fri,sat,sun
};
enum month
{
January,February,March,April,May,June,july,Augest,September,October,November,December
};
int m,y,i,re=0,nm,ny,nd;
long unsigned int sum;
//clrscr();
printf("Enter month & year (Ex.2 2000) \n");
scanf("%d%d",&m,&y);
printf("\t\t\t");
switch(m)
{
case 1:
printf("January");
break;
case 2:
printf("February");
break;
case 3:
printf("March");
break;
case 4:
printf("April");
break;
case 5:
printf("May");
break;
case 6:
printf("june");
break;
case 7:
printf("July");
break;
case 8:
printf("Augest");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
}
printf(" %d\n\n",y);
printf("Mon\tTue\tWed\tThu\tFri\tSat\tSun\n");
ny=y-1900;
if(m==1||m==2)
nm=(m-1)*31;
if(m==3)
{
if(y%4!=0)
nm=31+28;
else
nm=31+29;
}
if (m==4)
{
if(y%4!=0)
nm=(m-1)*30;
else
nm=(m-1)*30+1;
}
if(m==5)
{
if(y%4!=0)
nm=(m-1)*30;
else
nm=(m-1)*30+1;
}

C program to Find out Odd and Even numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a Number\n");
scanf("%d",&a);
b=a%2;
if (b==0)
{
printf("Number is odd");
}
else
{
printf("Number is even");
}
getch();
}

C++ program to sort the numbers with selection sort method

This is done by code::blocks Ide, You can run it in Turbo C++ also, but with changing header into iostream.h and removing using namespace std;

#include<iostream>
#include<conio.h>
using namespace std;
class bubble
{
    int num[50],n,i,j,temp;
public:
    void input()
    {
        cout<<"How many values you want to enter\n";
cin>>n;
cout<<"Enter values"<<endl;
for (i=0;i<n;i++)
  {
cin>>num[i];
  }
    }
    void display()
    {
for(i=0;i<n;i++)
cout<<num[i]<<"\n";
    }
    void calculation()
    {
        for (i=0;i<n;i++)
{
for(j=i;j<n;j++)
  {
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
     }

    }
    }
};
int main()
{
    bubble b;
    b.input();
    cout<<"Unlisted values are as follows:\n";
    b.display();
    b.calculation();
    cout<<"Listed values are as follows:\n";
    b.display();
getch();
return 0;
    }

Friday, March 28, 2014

Let us start C programming

Let us start C programming

This is done using Turbo C++ Ide. you can do the same with code blocks, microsoft visual studio ides also 

#include<stdio.h> //to include standard input output header file in which some keywords are defined
#include<conio.h>
void main()           //compiling starts with main() program
{
clrscr();     // all statement of c should be terminated with ;(semi colon)
 printf("Hallo World");   //used to send the data into output devices
 getch();   // to display the messages in screen
 }


If you are using Turbo C++, copy this program and open a notepad in your system(windows key+r, then type notepad in run window) and give a name with .c extension and save it in turbo c home folder

C Program to print Address


C Program to print Your Address


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char a,b,c;
printf("Name:\t");
scanf("%s",a);
printf("Street:\t");
scanf("%s",b);
printf("Pin Code: \t");
scanf("%s",c);
printf("%s%s%s",a,b,c);
getch();
}

C Program to find out greatest of two numbers

C Program to find out greatest of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf ("Enter any two number\n");
scanf("%d\n%d",&a,&b);
if (a>b)
printf("%d is greater than %d",a,b);
else
printf("%d is greater than %d",b,a);
getch();
}