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