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

Tuesday, August 14, 2012

Reverse Diagonal Elements of a Square Matrix using C language

Genaerally 3x3 matrix is represented as
| a1 a2 a3|
| a4 a5 a6|
| a7 a8 a9|
We need reverse diagonal elements means a3,a5,a7. Now observe the indices of these elements they are (0,2),(1,1),(2,0). By observing the indices we can say that by adding the row and column indes we are getting the one less than the size of matrix. means if the matrix size is 4x4 then the sum of reverse diagonal indeces is 4-1=3. based on this logic program is written. The matrix can be represented by the two dimensional array so we need two dimensional array. to load and manipulate this array we need two for loops. one for loop deals with the row and second for loop deals with the columns.
This program takes the size of square matrix as the input and according to it the elements of the matrix are loaded into the 2Darray.
I will deal the For loop indetail after.
Progrm:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n Reverse Diagonal elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j==n-1)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

 

Lower Triangle elements of a Square Matrix using C language

Genaerally 3x3 matrix is represented as
| a1 a2 a3|
| a4 a5 a6|
| a7 a8 a9|
Lower triangle elements are the a2,a3,a6. we need to print theese elements. The indices of these elements are(0,1),(0,2),(1,2). By observing these elements we can say that the index of the row is less than the column. by using conditional statements i.e if statement we can print the required elements.two for loops one for row and one for column are used.

Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n lower triangle elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
printf("\n %d",a[i][j]);
}
}
}
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...