Wednesday, November 28, 2012

Writing & Reading of FILE using W+ MODE in C language

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *f;
char c=' ';
clrscr();
f=fopen("kemp.txt","w+");
if(f==NULL)
{
printf("\n can not open file");
exit(1);
}
printf("write data and to stop process press '*':");
while(c!='*')
{
c=getche();
fputc(c,f);
}
rewind(f);
printf("\n contents read:");
while(!feof(f))
printf("%c",getc(f));
}

Tuesday, November 27, 2012

Appending DATA to a Pre-Existing FILE using C languge


#include<stdio.h>

#include<conio.h>

#include<process.h>

void main()

{

FILE *f;

char c;

clrscr();

printf("\n contents of file before appending");

f=fopen("balaram.txt","r");

while(!feof(f))

{
 
c=fgetc(f);

 printf("%c",c);

}

f=fopen("balaram.txt","a");

if(f==NULL)

{

 printf("File can not appending");

  exit(1);

}

printf("\n Enter string to append:");

while(c!='.')

{
 
c=getche();

  fputc(c,f);

}

fclose(f);

printf("\n contents of file after appending:\n");

f=fopen("balaram.txt","r");

while(!feof(f))

{

  c=fgetc(f);

  printf("%c",c);

}

}

Writing and reading Data from text FILE using C language


#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *f,*f1;
char c;
clrscr();
f=fopen("balaram.txt","w");
if (f==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("write data and to stop press '.':");
while(c!='.')
{
c=getche();
fputc(c,f);
}
fclose(f);
printf("\n contents read:");
f1=fopen("balaram.txt","r");
while(!feof(f1))
printf("%c",getc(f1));
}

Biggest Number in Array


/*Biggest Number in array:*/
#include<stdio.h>
 main()
{
  int a[50],size,i,big;

  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
           big=a[i];
  }
  printf("Largest element: %d",big);
 return 0;
}

Saturday, October 6, 2012

Message display program in clanguage using putc(),stdout(),stdin() Streams


#include<stdio.h>
main()
{
char ch[]="Programing is fun";
int k=0;
while(ch[k])
putc(ch[k++],stdout);
}

#include<stdio.h>
main(void)
{
char ch[11];
int i;
printf("Input a Text");
for(i=0;i<11;i++)
ch[i]=getc(stdin);
printf("\n The Text inputed was");
for(i=0;i<11;i++)
putc(ch[i],stdout);
}

Saturday, September 15, 2012

Program to check the number for Palindrome using C language

#include<stdio.h>
#include<conio.h>
main()
{
int n,t,s=0,r;
printf("\n Enter any number");
scanf("%d",&n);
t=n;
while(n>0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
if(t==s)
printf("%d is a palindrome number",t);
else
printf("%d is a not palindrome number",t);
getch();
}

Sunday, September 2, 2012

Guess The output of program

#include<stdio.h>
main()
{
int a=10;
printf("\n %d %d",++a,a++);
getch();
}

Observation: The printf statement get executed from the right side. so first the a will be printed as it is and then it gets incrimented and then again incrimented and the a value is printed means second a incrimented twice before it gets printed.

#include<stdio.h>
main()
{
int *p;
printf("sizeof(*p)=%d ,sizeof(p)=%d",sizeof(*p),sizeof(p));
getch();
}

Saturday, September 1, 2012

Armstrong number program using C language

Armstrong number: A number is called armstrong number when the sum of quebs of individual digits of a number is equal to the number itself. To write the program we need to seperate the individual digits we can do this by first modular division and division by 10. Consider the example 153 the individual digits are 1,5,3 sum of quebs of individual digits are 153 and the number itself is 153.

Program
#include<stdio.h>
main()
{
int n,r,t,s=0;
printf("\n Enter the number");
scanf("%d",&n);
t=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(s==t)
printf("\n %d is armstrong number",t);
else
printf("\n %d is not armstrong number",t);
getch();
}

Asterisks Graph program using C language

#include<stdio.h>
main()
{
int i,j,r;
printf("\n Enter number of rows");
scanf("%d",&r);
for(i=0;i<r;i++)
{
for(j=0;j<i+1;j++)
{
printf("* \t");
}
printf("\n");
}
getch();
}

Perfect number Program using C language


#include<stdio.h>
main()
{
int n,i=1,s=0;
printf("\n Enter the number");
scanf("%d",&n);
do
{
if(n%i==0)
{
s=s+i;
}
i++;
if(s>n)
break;
}while(i<n);
if(s==n)
printf("\n %d is perfect number",n);
else
printf("\n %d is not perfect number",n);
getch();
}

Friday, August 31, 2012

Swapping of two integers without another variable

#include<stdio.h>
main()
{
int a,b;
printf("\n Enter any two numbers to swap");
scanf("%d %d",&a,&b);
printf("\n Before swaping two numbers are a=%d and b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After swaping two numbers are a=%d and b=%d",a,b);
getch();
}

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...