Tuesday, November 27, 2012

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

Fibonacci Series using C language

The fibonacci series is like 0,1,1,2,3,5,8,13,24,55,89......We can get this like first two are 0 and 1 from third number on wards we have to add the previous two numbers for example to get the 5th number in the series we need to add 3rd and 4th numbers in the series.


#include<stdio.h>
int main()
{
int i,a=0,b=1,s=0,r;
printf("\n Enter range:");
scanf("%d",&r);
printf("%d \n",a);
printf("%d \n",b);
while(s<=r)
{
s=a+b;
if(s<=r)
{
printf("%d \n",s);
}
a=b;
b=s;
}
getch();
}

 

Floyd's Triangle using C language

#include<stdio.h>
main()
{
int r,p,a=1,i,j;
printf("\n Enter number of rows");
scanf("%d",&r);
for(i=0;i<r;i++)
{
for(j=0;j<i+1;j++)
{
printf("%d \t",a);
a++;
}
printf("\n");
}
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...