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

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

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

Monday, March 31, 2014

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

Saturday, March 29, 2014

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

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