Wednesday, April 16, 2014

C Program to check the number is palindrome or not

C Program to check the number is palindrome or not
 #include<stdio.h>
#include<conio.h>
void main()
{
int i,temp,sum=0,remainder,number;
clrscr();
printf("Enter the number to check Palindrome: ");
scanf("%d",&number);
temp=number;
while(number>0)
{
remainder=number%10;
number=number/10;
sum=sum*10+remainder;
}
printf("\nreverse of given number is= %d\n",sum);
if(sum==temp)
printf("number is palindrome");
else
printf("number is not palindrome");
getch();
}

Friday, April 11, 2014

C Program to open an existing file and read it

C Program to open an existing file and read it

You have to save the file which you want to read in the home folder (ie in the folder where your c program exists with ). Give the name of file with its extension (like name.txt) when it is asked.

#include <stdlib.h>
#include<stdio.h>
int main()
{
    FILE *fptr;
    char filename[15];
    char ch;
//    clrscr();

    printf("Enter the filename to be opened \n");
    scanf("%s", filename);
    /*  open the file for reading */
    fptr = fopen(filename, "r");
    if (fptr == NULL)
    {
printf("Cannot open file \n");
exit(0);
    }
    ch = fgetc(fptr);
    while (ch != EOF)
    {
printf ("%c", ch);
ch = fgetc(fptr);
    }
    fclose(fptr);
}

Wednesday, April 9, 2014

C++ Program to count lines,words and characters of a string

C++ Program to count lines,words and characters of a string




#include <iostream>
using namespace std;

class countli
{
    char c;
    int lines,words,chars;
public:
    void input()
    {
       cout<<"Enter string termanate by # : ";
    }
    void operation()
    {
        lines=1;
        words=1;
        chars=1;
        while(c != '#'){
        cin.get(c);
        chars++;
        if(c==' ' || c=='\n')
            words++;
        if(c=='\n')
            lines++;
    }
    }
    void output()
    {
        cout<<"No. of lines"<<lines<<endl;
        cout<<"No. of words"<<words<<endl;
        cout<<"No. of characters"<<chars<<endl;
    }
    };
int main()
{
countli a;
a.input();
a.operation();
a.output();
return 0;
}

Tuesday, April 8, 2014

C Program to sort n number of integers by using bubble sort technique

C Program to sort n number of integers by using bubble sort technique 

#include<stdio.h>
#include<conio.h>
void swap(int[],int,int);
void dis(int[],int);
void main ()
{
int a[100];
int n,i,temp,j;
clrscr();
printf("Enter how many numbers do you want");
scanf("%d",&n);
printf("Enter the %d numbers",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Unsorted values\n");
dis(a,n);
for (i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
  {
if(a[i]<a[j])
    {
swap(a,i,j);
    }
    }
}
printf("Sorted values are as follow\n");
dis(a,n);
getch();
}

void dis (int b[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\n",b[i]);
}
void swap(int b[],int k,int l)
{
int temp;
temp=b[k];
b[k]=b[l];
b[l]=temp;
}

Monday, April 7, 2014

C Program to create a menu based program using switch case

C Program to create a menu based program using switch case

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int s,n1,rem,rev=0,num,n,f=1,i,sum=0;
clrscr();
printf("Choose the option\n");
printf("1. Cheque wheather number is palindrome\t2. Factorial value\n");
printf("3. Sum of n natural number\t\t4. Exit\n");
scanf("%d",&s);
switch(s)
{
case 1:
printf("Enter the number\n");
scanf("%d",&n1);
num=n1;
while (n1>0)
{
rem=n1%10;
n1=n1/10;
rev=rev*10+rem;
}
printf("reverse number is %d\n",rev);
if(num==rev)
printf("Number is palindrome");
else
printf("Number is not palindrome\n");
break;
case 2:
printf("Enter the number\n");
scanf("%d",&n);
for(i=n;i>0;i--)
f=f*i;
printf("factorial is %d",f);
break;
case 3:
printf("How many numbers do you want to enter\n");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&n1);
sum=sum+n1;
}
printf("sum of %d numbers is %d",n,sum);
break;
case 4:
exit();
}
getch(0);
}

Sunday, April 6, 2014

C++ Program to swap two integers using friend function

C++ Program to swap two integers using friend function

In object-oriented programming, a friend function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public. Normally, a function that is defined outside of a class cannot access such information. Declaring a function a friend of a class allows this, in languages where the concept is supported.
                                                                                         wiki

#include<iostream.h>
class b;
class a
{
int x1;
public:
void reada()
{
cout<<"Enter x1 value\n";
cin>>x1;
}
friend void swap (a &,b &);
};
class b
{
int x2;
public:
void readb()
{
cout<<"Enter an x2 value\n";
cin>>x2;
}
friend void swap (a &,b &);
};
void swap (a &o1, b &o2)
{
int t;
cout<<"Before swapping\nx1="<<o1.x1<<"\n"<<"x2="<<o2.x2<<"\n";
t=o1.x1;
o1.x1=o2.x2;
o2.x2=t;
cout<<"After swapping\nx1="<<o1.x1<<"\n"<<"x2="<<o2.x2<<"\n";
}
void main()
{
clrscr();
a a;
a.reada();
b b;
b.readb();
swap(a,b);
}

Saturday, April 5, 2014

C++ Program to find out the sum of digits of a number

C++ Program to find out the sum of digits of a number

#include<iostream.h>
class sumdig
{
int n,rem,rem1;
public:
void input()
{
cout<<"Enter the number\n";
cin>>n;
}
void calculation()
{
rem1=0;
while (n>0)
{
rem=n%10;
n=n/10;
rem1=rem1+rem;
}
}
void output()
{
cout<<"Sum of single digits is "<<rem1;
}
};
void main()
{
clrscr();
sumdig a;
a.input();
a.calculation();
a.output();
}

Friday, April 4, 2014

C++ Program to generate n number of Fibonacci series

C++ Program to generate n number of Fibonacci series

In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:
1,1,2,3,5,8,13,21,34,55,89,144….
or (often, in modern usage):

0,1,1,2,3,5,8,13,21,34,55,89,144……(sequence A000045 in OEIS)

By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.                                                wikipedia



#include<iostream.h>
class febin
{
int f,s,ne,n,i;
public:
void input()
{
cout<<"How many elements do you want in febinocci series\n";
cin>>n;
}
void calculation()
{
f=0;
s=1;
cout<<f<<" ";
for (i=1;i<n;i++)
{
ne=f+s;
cout<<ne<<" ";
s=f;
f=ne;
}
}
};
void main()
{
clrscr();
febin a;
a.input();
a.calculation();
}

Thursday, April 3, 2014

C++ Program to check whether the number is palindrome or not

C++ Program to check whether the number is palindrome or not
A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction.[1] Famous examples include "Amore, Roma", "A man, a plan, a canal: Panama" and "No 'x' in 'Nixon'". --- wiki

#include<iostream.h>
#include<conio.h>
class palindrome
{
int n1,n2,rem,rev;
public:
void input()
{
cout<<"Enter the number\n";
cin>>n1;
}
void calculation()
{
rev=0;
n2=n1;
while (n1>0)
{
rem=n1%10;
n1=n1/10;
rev=rev*10+rem;
}
}
void output()
{
if(n2==rev)
cout<<"Number is palindrome";
else
cout<<"Number is not palindrome";
}
};
void main()
{
clrscr();
palindrome a;
a.input();
a.calculation();
a.output();
getch();
}

Wednesday, April 2, 2014

C++ program to check whether the number is prime or not

C++ program to check whether the number is prime or not

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

Tuesday, April 1, 2014

C++ Program to do operation on matrices

C++ Program to do operation on matrices

This is done in Code::Blocks Ide. Some modification are needed to run in Turbo C++ ide, like changing header into iostream.h, adding one more header conio.h and hence getch at the end of the program, removing using namespace std;, and all

#include<iostream>
#include<stdlib.h>
using namespace std;
class matrix
{
    int fraw,fcol,sraw,scol,i,j,k;
    int f[50][50],s[50][50],l[50][50];
public:
    void input()
    {
        cout<<"Enter number of raws and columns do you want for fisrt matrix\n";
cin>>fraw>>fcol;
cout<<"Enter number of raws(same as number of columns in first) and columns of second matrix\n";
cin>>sraw>>scol;
cout<<"Enter "<<fraw*fcol<<"elements of first matrix\n";
for(i=0;i<fraw;i++)
{
for(j=0;j<fcol;j++)
{
cin>>f[i][j];
}
}
cout<<"Enter "<<sraw*scol<<"elements of second matrix\n";
for(i=0;i<sraw;i++)
{
for(j=0;j<scol;j++)
{
cin>>s[i][j];
}
    }
    }
    void addition()
    {
        cout<<"Sum of two matrices\n";
for(i=0;i<fraw;i++)
{
for(j=0;j<fcol;j++)
{
l[i][j]=f[i][j]+s[i][j];
cout<<"\t"<<l[i][j];
}
cout<<"\n";
}
    }
void substraction()
{
    cout<<"Difference of two matrices\n";
for(i=0;i<fraw;i++)
{
for(j=0;j<fcol;j++)
{
l[i][j]=f[i][j]-s[i][j];
cout<<"\t"<<l[i][j];
}
cout<<"\n";
}
}
void multiplication()
{
    if(fcol!=sraw)
        cout<<"No of columns of First matrix and raws of second matrix is not same, multiplication is not allowed";
    else
    {
    for(i=0;i<fraw;i++)
for (j=0;j<scol;j++)
{
l[i][j]=0;
}
for(i=0;i<fraw;i++)
for (j=0;j<scol;j++)
for (k=0;k<fcol;k++)
{
l[i][j]=l[i][j]+f[i][k]*s[k][j];
}
for(i=0;i<fraw;i++)
{
for (j=0;j<scol;j++)
{
cout<<l[i][j]<<"\t";
}
cout<<"\n";
}
}
}
void read()
{
    cout<<"Elements of first matrix\n";
for(i=0;i<fraw;i++)
{
for(j=0;j<fcol;j++)
{
cout<<f[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"Elements of second matrix\n";
for(i=0;i<sraw;i++)
{
for(j=0;j<scol;j++)
{
cout<<s[i][j]<<"\t";
}
cout<<"\n";
}
};
};
int main()
{
int c;
char an;
matrix a;
a.input();
do
{
    cout<<"Choose from the following options:\n";
    cout<<"1 Addition\t\t2 Subtraction\n";
    cout<<"3 Multiplication\t4 Reading\n";
    cout<<"5 Exit";
    cin>>c;
    switch (c)
    {
    case 1:
        a.addition();
        break;
    case 2:
        a.substraction();
        break;
    case 3:
        a.multiplication();
        break;
    case 4:
        a.read();
        break;
    default:
        exit(0);
    }
    cout<<"Do you want to continue: y/n";
    cin>>an;
}
while(an=='y'||an=='Y');
return 0;
}


C++ Program to demonstrate operator overloading

C++ Program to demonstrate operator overloading function (Unary Operator) Using friend function
#include<iostream>
class A
{
int x,y;
public:
void input ()
{
cout<<"Enter X and Y values\n";
cin>>x>>y;
}
void disp()
{
cout<<"x is "<<x<<endl<<"y= "<<y<<endl;
}
friend void operator --(A &o);
friend void operator ++(A &o);
};
void operator --(A &o)
{
o.x=--o.x;
o.y=--o.y;
}
void operator ++(A &o)
{
o.x++;
o.x=o.x;
o.y++;
o.y=o.y;
}
int main()
{
A a;
a.input();
a.disp();
cout<<"After decrimenting\n";
//accessing unary minus operator
--a;//calling operator -()
a.disp();
++a;
cout<<"After incrimenting\n";
a.disp();
return 0;
}


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();
}