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

Upper Triangle of Square Matrix using C language

#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 Upper triangle elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j<i)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

Reverse Diagonal elements of a Square Matrix using single for loop in C language

#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,j=0;i<n,j<n;i++,j++)
{
printf("\n %d",a[i][n-1-i]);
}
getch();
}

Forward Diagonal Elements of a Square Matrix using C language

#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 Forward Diagonal elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

Monday, August 13, 2012

Write a program to find Nth prime number in a given range, inclusive of the first and the last element in that range

#include<stdio.h>
main()
{
 long int i=0,j=0;
 long int n1,n2,t=0,v;
 int k;
 int flag=0;
 printf("\n FROM the number n1 To the number n2 enter the nth prime no you want ");
 scanf("%ld %ld %ld",&n1,&n2,&v);
 if(v>0&&n2>n1&&n1<1500000&&n2<1500000)
 {
 for(i=n1;i<=n2;i++)
 {
  for(j=1,k=0;j<=i;j++)
  {
   if(i%j==0)
   k++;
  }
 if(k==2)
 {
 t++;
 if(t==v)
 {
 printf("\n %ld",i);
 flag=1;
 break;
 }
 }
 }
 if(flag==0)
 {
 printf("\n no prime number is present at this index");
 }
 }
 else
 {
 printf("Invalid Input");
 }
 getch();
 }


Sunday, August 12, 2012

Strong Number program using C language


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

Saturday, August 11, 2012

Program to find Prime number from 0 to given range C language



#include<conio.h>
#include<stdio.h>
void main()
{
int n,i,k;
clrscr();
printf("\n Enter any number");
scanf("%d",&n);
i=1;
k=0;
while(i<=n)
{
if(n%i==0)
{
k++;
if(k>2)
{
printf("\n Enterd number %d is not prime number",n);
break;
}
}
i++;
}
if(k<=2)
{
printf("\n Enterd number %d is prime number",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...