Google Ads
Saturday, May 30, 2009
C Programs - INDEX
Two Rightmost Digits
The Rightmost Digit
Array Function
Divisible By 7
Finding Square Root
Sum Of Square 10 Numbers
Tables Using For Loop
Tables
Table From 1 To 10
Displaying Integral Part
Harmonic Value
Reversing of 2 Digit Number
Leap Year
SUM OF 5 VALUES
To Calculate 1/1+1/2+1/3+1/4+1/5
Stars2 - program
Stars1 - program
INDEX - PROGRAMS
Pascal's Triangle
Armstrong Number
Prime Number
Concatenation of String
Reversing a String
Length of String
Replacing String
Substring
Sine Series
Cosine Series
Exponential Series
Bank Operations
Sales Report
Fibonacci Series
Fibonacci - Recursion
Tower of Hanoi
Factorial
Mark Sheet
Searching Sorting
Data Structures
Floyd's Triangle
#include”conio.h”
void main()
{
int i,j,n,c=1;
clrscr();
printf("Enter the Limit :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",c);
c++;
}
printf("\n");
}
getch();
}
Output will be
Enter the Limit :6
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Two Rightmost Digits
#include”conio.h”
void main()
{
float a;
int b,c;
//clrscr();
printf("\n\n");
printf("enter a value");
scanf("%f",&a);
b=(int)a;
c=b%100;
printf("THE 2 RIGHTMOST DIGITS OF THE GIVEN NO ARE %d",c);
getch();
}
OUTPUT WILL BE
enter a value27654
THE 2 RIGHTMOST DIGITS OF THE GIVEN NO ARE 54
The Rightmost Digit
#include”conio.h”
void main()
{
float a;
int b,c;
clrscr();
printf(“enter a value:”);
scanf(“%f”,&a);
b=(int)a;
c=b%10;
printf(“THE RIGHTMOST DIGIT OF THE GIVEN NO IS %d”,c);
getch();
}
OUTPUT WILL BE
enter a value:4562
THE RIGHTMOST DIGIT OF THE GIVEN NO IS 2
Array Function
#include”conio.h”
void main()
{
int x[5],i,tot=0;
clrscr();
for(i=0;i<5;i++)
{
printf("\nenter %d element:",i+1);
scanf("%d",&x[i]);
tot=tot+x[i];
}
for(i=0;i<5;i++)
{
printf("x[%d]=%d\n",i,x[i]);
}
printf("array total=%d",tot);
getch();
}
OUTPUT WILL BE
enter 1 element:34
enter 2 element:56
enter 3 element:5
enter 4 element:12
enter 5 element:999
x[0]=34
x[1]=56
x[2]=5
x[3]=12
x[4]=999
array total=1106
Divisible By 7
#include”conio.h”
void main()
{
int i,n;
clrscr();
printf("Enter a value\n");
scanf("%d",&n);
printf("The number divisible by 7 b/w 0 to %d is",n);
for(i=0;i<=n;i++)
{
if(i%7==0)
printf("%d\n",i);
}
getch();
}
OUTPUT WILL BE
Enter a value
50
The number divisible by 7 b/w 0 to 50 is
7
14
21
28
35
42
49
Finding Square Root
#include”conio.h”
#include"math.h"
void main()
{
int count=1;
float x,y;
clrscr();
printf("Enter 5 value: ");
read:
scanf("%f",&x);
if(x<0)
printf("value %d is negative\n",count);
else
{
y=sqrt(x);
printf("Sqrt of %f is %f\n",x,y);
}
count++;
if(count<=5)
goto read;
getch();
}
OUTPUT WILL BE
Enter 5 value:
4 6 9 -10 16
Sqrt of 4.000000 is 2.000000
Sqrt of 6.000000 is 2.449490
Sqrt of 9.000000 is 3.000000
value 4 is negative
Sqrt of 16.000000 is 4.000000
Sum Of Square 10 Numbers
#include”conio.h”
void main()
{
int sum=0,n=1;
clrscr();
printf("\t\t\sum of 10 square numbers\n\n");
loop:
sum=sum+(n*n);
if(n==10)
goto print;
else
{
n=n++;
goto loop;
}
print("%d",sum);
getch();
}
OUTPUT WILL BE
Sum of 10 square numbers
385
Tables Using For Loop
#include "conio.h"
void main()
{
int row,col,y;
clrscr();
for(row=1;row<=10;row++)
{
for(col=1;col<=10;col++)
{
y=row*col;
printf("%d\t",y);
}
printf("\n");
}
getch();
}
OUTPUT WILL BE
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Tables
#include "conio.h"
void main()
{
int y,row=1,col;
clrscr();
do
{
col=1;
do
{
y=row*col;
printf("%d\t",y);
col=col+1;
}
while(col<=10);
printf("\n");
row=row+1;
}
while(row<=10);
getch();
}
OUTPUT WILL BE
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Table From 1 To 10
#include”conio.h”
void main()
{
int y,row=1,col;
clrscr();
do
{
col=1;
do
{
y=row*col;
printf("%d*%d=%d\t",row,col,y);
col=col+1;
}while(col<=10);
printf("\n");
row=row+1;
}while(row<=10);
getch();
}
OUTPUT WILL BE
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 6*10=60
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 7*10=70
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 8*10=80
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 9*10=90
10*1=10 10*2=20 10*3=30 10*4=40 10*5=50 10*6=60 10*7=70 10*8=80 10*9=90 10*10=100
Friday, May 29, 2009
Displaying Integral Part
#include”conio.h”
void main()
{
float a;
int c;
clrscr();
printf("Enter any float no.\n");
scanf("%f",&a);
c=(int)a%100;
printf("The right most digit of integral part is %d",c);
getch();
}
OUTPUT WILL BE
Enter any float no.
546.255
The right most digit of integral part is 46
Harmonic Value
#include”conio.h”
void main()
{
int n;
float i,sum=0;
clrscr();
printf("\t\t\tHarmonic series\n\n");
printf("Enter a numeric value\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1/i);
}
printf("The Harmonic value of %d is %f",n,sum);
getch();
}
OUTPUT WILL BE
Harmonic series
Enter a numeric value
2
The Harmonic value of 2 is 1.500000
Reversing of 2 Digit Number
#include”conio.h”
void main()
{
int n,x,d1,d2;
clrscr();
printf("Enter a 2-digit value\n");
scanf("%d",&n);
d1=n%10;
d2=n/10;
x=d1*10+d2;
printf("The Reverse is %d",x);
getch();
}
OUTPUT WILL BE
Enter a 2-digit value
85
The Reverse is 58
Leap Year
#include”conio.h”
void main()
{
int i;
clrscr();
for(i=1999;i<=2008;i++)
{
if(i%4==0)
printf("%d is leap year\n",i);
else
printf("%d is not a leap year\n",i);
}
getch();
}
OUTPUT WILL BE
1999 is not a leap year
2000 is leap year
2001 is not a leap year
2002 is not a leap year
2003 is not a leap year
2004 is leap year
2005 is not a leap year
2006 is not a leap year
2007 is not a leap year
2008 is leap year
SUM OF 5 VALUES
#include"conio.h"
void main()
{
int i,x[10],tot=0;
clrscr();
printf("Enter 5 values\n");
for(i=1;i<=5;i++)
{
scanf("%d",&x[i]);
tot=tot+x[i];
printf("x[%d]=%d\n",i,x[i]);
}
printf("Total=%d",tot);
getch();
}
OUTPUT WILL BE
Enter 5 values
5 7 8 9 10
x[1]=5
x[2]=7
x[3]=8
x[4]=9
x[5]=10
Total=39
To Calculate 1/1+1/2+1/3+1/4+1/5
#include
#include
void main()
{
float i,n,res,sum;
clrscr();
printf("Enter the Value for N: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
res=1/i;
sum=sum+res;
printf("i = %f\t",res);
printf("\tSum = %f\n",sum);
}
getch();
}
Output will be
Enter the Value for N: 5
i = 1.000000 Sum = 1.000000
i = 0.500000 Sum = 1.500000
i = 0.333333 Sum = 1.833333
i = 0.250000 Sum = 2.083333
i = 0.200000 Sum = 2.283334
Stars2 - program
#include"stdio.h"
#include"conio.h"
void main()
{
int i,j;
clrscr();
for(i=5;i<6;i--)
{
for(j=0;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}
Output will be
*****
****
***
**
*
Stars1 - program
#include"stdio.h"
#include"conio.h"
void main()
{
int i,j,n;
clrscr();
printf("Enter the Value for N: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}
Output will be
*
**
***
****
*****
Wednesday, May 27, 2009
Pascal's Triangle
PASCAL’S TRIANGLE
#include”stdio.h”
#include “conio.h”
void main()
{
int i,a,b,n;
clrscr();
printf("\n\n\n\t\t\tPASCAL’S TRIANGLE\n");
printf("\t\t\t***************\n\n\n");
printf("\nEnter the Limit : ");
scanf("%d",&n);
for(i=0;i
{
a=0;
b=1;
printf("\n");
while(a<=i)
{
if((a==0)||(i==0))
{
printf("\t%d",b);
}
else
{
b=b*(i-a+1)/a;
printf("\t%d",b);
}
a++;
}
}
getch();
}
Output will be
Enter the Limit : 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Armstrong Number
ARMSTRONG NUMBER
#include”stdio.h”
#include “conio.h”
void main()
{
int f,r,s,n;
clrscr();
printf("\nEnter the Number : ");
scanf("%d",&n);
s=0;
f=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(f==s)
{
printf("\n\nThe given no %d is an armstrong number",f);
}
else
{
printf("\n\nThe given no %d is not an armstrong number",f);
}
getch();
}
Output will be
Enter the Number : 153
The given no 153 is an armstrong number
Enter the Number : 156
The given no 156 is not an armstrong number
Prime Number
#include”stdio.h”
#include “conio.h”
void main()
{
int i,j,c,n;
clrscr();
printf("Enter the limit,to generate prime no's for given value : ");
scanf("%d",&n);
printf("\n\nThe Prime number upto %d : ",n);
for(i=2;i<=n;i++) { c=0; for(j=2;j
{
if(i%j==0)
{
c=c+1;
}
}
if(c==0)
{
printf(" %d",i);
}
}
getch();
}
Output will be
Enter the limit, to generate prime no's for given value : 6
The Prime number’s upto 6 : 2 3 5
Concatenation of String
#include”stdio.h”
#include “conio.h”
#include
void main()
{
char str1[10],str2[20],d[20]=" ",ch;
int i=0,j=0;
clrscr();
printf("\nEnter the First String : ");
scanf("%s",str1);
printf("\nEnter the Second String : ");
scanf("%s",str2);
ch=str1[i];
while(ch!='\0')
{
d[j]=str1[i];
i++;
j++;
ch=str1[i];
}
i=0;
ch=str2[i];
while(ch!='\0')
{
d[j]=str2[i];
i++;
j++;
ch=str2[i];
}
printf("\nThe Concatenated String : %s",d);
getch();
}
Enter the First String : hello
Enter the Second String : world
The Concatenated String : helloworld
Reversing a String
#include”stdio.h”
#include “conio.h”
#include
void main()
{
char str1[10],str2[20];
int len,i,j;
clrscr();
printf("\nEnter the String : ");
gets(str1);
len=strlen(str1);
for(i=len-1,j=0;i>=0;i--,j++)
str2[j]=str1[i];
str2[j]=’\0’;
printf("\nThe Reversed String : %s",str2);
getch();
}
Output will be
Enter the String : good
The Reversed String : doog
Length of String
#include”stdio.h”
#include “conio.h”
#include
void main()
{
char str[50],ch;
int i=0,c=0;
clrscr();
printf("\n\nEnter the String : ");
gets(str);
ch=str[i];
while(ch!='\0')
{
i=i+1;
ch=str[i];
c++;
}
printf("\nThe Length of the String : %d",c);
getch();
}
Output will be
Enter the String : India is my country
The Length of the String : 9
Replacing String
#include “conio.h”
#include
void main()
{
char a[50],b[50],c[50],d[50];
int i,j,e,l,k,f=0;
clrscr();
printf("Enter the String : ");
gets(a);
printf("Enter the Substring : ");
gets(b);
printf("Enter the string to be replace : ");
gets(c);
e=0;
i=0;
while(a[i]!='\0')
{
j=0;
k=i;
while(b[j]==a[k]&&b[j]!='\0')
{
j++;
k++;
}
if(a[k]=='\0'||a[k]==' ')
{
if(b[j]=='\0')
{
l=0;
i=k;
while(c[l]!='\0')
{
d[e]=c[l];
e++;
l++;
f=l;
}
}
else
{
d[e]=a[i];
e++;
i++;
}
}
else
{
d[e]=a[i];
e++;
i++;
}
}
d[e]='\0';
if(f==0)
printf("\nThe string is not found");
else
{
printf("Replaced string: ");
printf("%s",d);
}
getch();
}
Output will be
Enter the String : this is a hotel
Enter the Substring : hotel
Enter the string to be replace : college
Replaced string: this is a college
Enter the String : this is a wall
Enter the Substring : hai
Enter the string to be replace : hi
The string is not found
Substring
#include”stdio.h”
#include “conio.h”
#include
void main()
{
char str[10],sstr[10];
int i,j,len,slen,n,f=0,k=0;
clrscr();
printf("Enter the String : ");
gets(str);
printf("\nEnter the Substring : ");
gets(sstr);
len=strlen(str);
slen=strlen(sstr);
for(i=0;i
printf("\nSubstring occured %d times\n",k);
else
printf("\nString not found ");
getch();
}
Output will be
Enter the String : Hello world
Enter the Substring : l
The Given Substring : l
Substring occured 3 times
Sine Series
#include “conio.h”
#include
int fact(int);
void main()
{
int i,x;
float sum=0,p,f,x1,n,c=1;
clrscr();
printf("\n Enter the values of x and n : ");
scanf("%d %f",&x,&n);
x1=(x*3.14)/180;
for(i=1;i<=n;i+=2)
{
p=pow(x1,i);
f=fact(i);
c=c*(p/f);
sum=sum+c;
c=c*(-1);
}
printf("\n Summation of Sine series : %f",sum);
printf("\n\n Sin(%d) : %f",x,sin(x1));
getch();
}
int fact(int a)
{
int f=1,i;
for(i=1;i<=a;i++)
f=f*i;
return(f);
}
Output will be
Enter the values of x and n : 56 3
Summation of Sine series : 0.825104
Sin(56) : 0.828760
Cosine Series
#include”stdio.h”
#include “conio.h”
#include
int fact(int);
void main()
{
int i,j,x;
float sum=1,x1,n,c=1,p,f;
clrscr();
printf("\n Enter the values of x and n : ");
scanf("%d %f",&x,&n);
x1=(x*3.14)/180;
for(i=2;i<=n;i=i+2)
{
p=pow(x1,i);
f=fact(i);
c=c*(-1);
c=c*(p/f);
sum=sum+c;
}
printf("\n Summation of Cosine Series : %.4f",sum);
printf("\n\n Cos(%d) : %.4f",x,cos(x1));
getch();
}
int fact(int a)
{
int f=1,i;
for(i=1;i<=a;i++)
f=f*i;
return(f);
}
Output will be
Enter the values of x and n : 30 6
Summation of Cosine Series : 0.8635
Exponential Series
#include”stdio.h”
#include “conio.h”
#include
int fact(int);
void main()
{
int i;
float x,sum=1,p,f,n,c;
clrscr();
printf("\n Enter the values of x and n : ");
scanf("%f %f",&x,&n);
for(i=1;i<=n;i++)
{
p=pow(x,i);
f=fact(i);
c=(p/f);
sum=sum+c;
}
printf("\n Summation of Exponential series : %f",sum);
printf("\n\n Exp(%.0f) : %f",x,exp(x));
getch();
}
int fact(int a)
{
int f=1,i;
for(i=1;i<=a;i++)
f=f*i;
return(f);
}
Output will be
Enter the values of x and n : 2 3
Summation of Exponential series : 6.333333
Exp(2) : 7.389056
Bank Operations
#include”stdio.h”
#include “conio.h”
void deposit();
void balance();
void withdraw();
int n;
float bal=0,wit,dep;
char nam[50];
void main()
{
int ch;
clrscr();
printf("\nEnter the Name of the Customer : ");
scanf("%s",nam);
printf("\nEnter the Account Number : ");
scanf("%d",&n);
do
{
printf("\n\n1.Deposit\n2.Balance\n3.Withdraw\n4.Exit\n");
printf("\nEnter the Operation to perform : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
deposit();
break;
case 2:
balance();
break;
case 3:
withdraw();
break;
case 4:
exit(0);
break;
}
}while(ch<4);
getch();
}
void deposit()
{ printf("\nEnter the Amount to be Deposited : Rs.");
scanf("%f",&dep);
bal=bal+dep;
balance();
}
void balance()
{ printf("\n Account Number : %d",n);
printf("\n Name : %s",nam);
printf("\n Balance : Rs.%.2f",bal);
}
void withdraw()
{ printf("\nEnter the Amount to be withdrawn : Rs.");
scanf("%f",&wit);
if(wit>bal)
printf("\nWithdraw is not sucess");
else
bal=bal-wit;
balance();
}
Sales Report
SALES REPORT - Using Two Dimentional Array
#include “conio.h”
#include”stdio.h”
void main()
{
int a[20][20],i,j,b,n,m;
clrscr();
printf("\n\n\t\t\tSALES REPORT");
printf("\n\t\t\t************\n");
printf("\nEnter the no.of Salesgirl : ");
scanf("%d",&n);
printf("\nEnter the no.of Items : ");
scanf("%d",&m);
printf("\nEnter the items ");
for(i=0;i
{
for(j=0;j
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i
{
b=0;
for(j=0;j
{
b=a[i][j]+b;
}
a[i][j]=b;
}
for(j=0;j
{
b=0;
for(i=0;i
{
b=a[i][j]+b;
}
a[i][j]=b;
}
b=0;
for(i=0;i
{
b=a[i][m]+b;
}
a[n][m]=b;
printf("\n\tSALES GIRL");
for(i=0;i
{
printf("\tITEM %d",i+1);
}
printf("\tTOTAL");
for(i=0;i<=n;i++)
{
if(i
printf("\n\t\t%d",i+1);
else
printf("\n\t\tTOTAL");
for(j=0;j<=m;j++)
{
printf("\t %d",a[i][j]);
}
}
getch();
}
Fibonacci Series
#include”stdio.h”
void main()
{
int a=0,b=1,y=0,i,n;
clrscr();
printf("\nEnter the limit : ");
scanf("%d",&n);
printf("\nThe Fibonacci Series of %d numbers :",n);
for(i=0;i
{
printf(" %d",y);
y=a+b;
b=a;
a=y;
}
getch();
}
Output will be
Enter the limit : 6
The Fibonacci Series of 6 numbers : 0 1 1 2 3 5 8 13 21 34
Fibonacci - Recursion
FIBONACCI SERIES USING RECURSION
#include “conio.h”
#include”stdio.h”
int fib(int,int,int);
void main()
{
int a=0,b=1,n;
clrscr();
printf("\nEnter the limit : ");
scanf("%d",&n);
printf("\nThe Fibonacci Series of %d numbers :",n);
fib(a,b,n);
getch();
}
int fib(int a,int b,int n)
{
int c;
if(n==0)
return(1);
else
{
printf(" %d",a);
c=a+b;
b=a;
a=c;
}
n--;
return(fib(a,b,n));
}
Output will be
Enter the limit : 10
The Fibonacci Series of 8 numbers : 0 1 1 2 3 5 8 13 21 34
Tower of Hanoi
#include”stdio.h”
#include “conio.h”
void tower(int n,char*l,char*c,char*r);
void main()
{
int n;
clrscr();
printf("\n\nEnter the no. of disks : ");
scanf("%d",&n);
tower(n,"TOWER 1","TOWER 2","TOWER 3");
getch();
}
void tower(int n,char*l,char*c,char*r)
{
if(n<=0)
return;
tower(n-1,l,r,c);
printf("\nMove Disk %d from %s to %s",n,l,r);
tower(n-1,c,l,r);
}
Output will be
Enter the no. of disks : 4
Move Disk 1 from TOWER 1 to TOWER 2
Move Disk 2 from TOWER 1 to TOWER 3
Move Disk 1 from TOWER 2 to TOWER 3
Move Disk 3 from TOWER 1 to TOWER 2
Move Disk 1 from TOWER 3 to TOWER 1
Move Disk 2 from TOWER 3 to TOWER 2
Move Disk 1 from TOWER 1 to TOWER 2
Move Disk 4 from TOWER 1 to TOWER 3
Move Disk 1 from TOWER 2 to TOWER 3
Move Disk 2 from TOWER 2 to TOWER 1
Move Disk 1 from TOWER 3 to TOWER 1
Move Disk 3 from TOWER 2 to TOWER 3
Move Disk 1 from TOWER 1 to TOWER 2
Move Disk 2 from TOWER 1 to TOWER 3
Move Disk 1 from TOWER 2 to TOWER 3
Factorial
FACTORIAL OF A NUMBER
#include”stdio.h”
#include “conio.h”
long int fact(int);
void main()
{
int n;
clrscr();
printf("\nEnter the value : ");
scanf("%d",&n);
printf("\nThe Factorial of %d : %ld",n,fact(n));
getch();
}
long int fact(int n)
{
if((n==0)||(n==1))
return(1);
else
return(n*fact(n-1));
}
Enter the value : 5
The Factorial of 5 : 120