Sunday, 26 February 2012

Pascal triangle program in C language


#include<stdio.h>
#include<conio.h>
void main()
{
int p[10][10],i,j,k;
clrscr();
printf("\n Pascal Triangle: ");
for(i=0;i<10;i++)
{
j=1;
p[i][0]=1;
p[i][i]=1;
while(j<i)
{
p[i][j]=p[i-1][j-1]+p[i-1][j];
j++;
}
}
for(i=0;i<10;i++)
{
j=10;
while(j>i)
{
printf(" ");
j--;
}
for(k=0;k<=i;k++)
{
printf("  %d ",p[i][k]);
}
printf("\n");
}
getch();
}



Saturday, 25 February 2012

No Argument No Returning type function program


#include<stdio.h>
#include<conio.h>
void fact();
void main()
{
clrscr();
fact();
getch();
}


void fact()
{
int i=0,f=1,n;
printf("Enter the number: ");
scanf("%d",&n);
for(i=n;i>0;i--)
f=f*i;
printf("\nThe factorial of the number is: %d",f);
}

Thursday, 16 February 2012

Program to change the string from upper case to lower case


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char n[100];
int i=0,p=0;
clrscr();
printf("Enter the string  ");
gets(n);
while(n[i]!='\0')
{
i++;
}
printf("Length of string is:  %d",i);


p=i;
while(p>=0)
{
if(n[p]>=65 && n[p]<=90)
n[p]=n[p]+32;
p--;
}
printf("The lower case string is: ");
puts(n);
getch();
}

Program to check whether two strings are same or not


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,k=0;
char n[100],m[100];
clrscr();
printf("Input the string:");
gets(n);
gets(m);
while(n[i]!='\0')
{
i++;
}
k=i;
i=0;
while(n[i]!='\0')
{
if(n[i]==m[i])
{
i++;
if(k==i)
printf("The two strings are same.");
continue;
}
else
{
printf("String are not same.");
goto p;
}
}
p:
getch();
}

Sum of two integers using bit wise operators


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum,carry;
clrscr();
printf("Enter the two number: ");
scanf("%d  %d",&a,&b);
sum=a^b;
carry=a&b;
while(carry!=0)
{
carry<<=1;
a=sum;
b=carry;
sum=a^b;
carry=a&b;
}
printf("\n Sum=  %d",sum);
getch();
}

Addition of two integers using bitwise operators


/* Bit wise operator use less space in the memory */




#include<stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
int m,n,sum;
clrscr();
printf("Enter the values of a and b:");
scanf("%d %d",&m,&n);
sum=add(m,n);
printf("The sum of the numbers: %d",sum);
getch();
}
int add(int m,int n)
{
int a,k;
do
{
a=m^n;
k=m&n;
m=a;
n=k<<1;
}while(k!=0);
return(a);
}