Sunday, 18 March 2012
Tuesday, 24 January 2012
Program to print factorial upto 'n' number of terms where n is accepted by use(using function)
#include
#include
void fibo(int n);
void main()
{
int n;
clrscr();
printf("\Enter Limit : \n ");
scanf("%d", &n);
fibo(n);
getch();
}
void fibo(int n)
{
int i ,a=0, b=1 , fib=0;
for(i=0; i {
printf(" %d ", fib);
fib = a+b ;
a=b;
b=fib;
}
}
#include
void fibo(int n);
void main()
{
int n;
clrscr();
printf("\Enter Limit : \n ");
scanf("%d", &n);
fibo(n);
getch();
}
void fibo(int n)
{
int i ,a=0, b=1 , fib=0;
for(i=0; i
printf(" %d ", fib);
fib = a+b ;
a=b;
b=fib;
}
}
Saturday, 21 January 2012
Write a program to find out pythagorean triplets in raange of 1 to 100
#include
#include
#include
void main()
{
int s1,s2,hyp,ptriple=0;
clrscr();
printf(" Side1 ---- side2 ---- hypotenus\n");
for(s1=0;s1<=100;s1++)
{
for(s2=0;s2<=100;s2++)
{
for(hyp=1;hyp<=100;hyp++)
{
ptriple=(s1*s1)+(s2*s2);
hyp=hyp*hyp;
if(hyp==ptriple)
{
hyp=sqrt(hyp);
printf("\n%d ---- %d ---- %d ",s1,s2,hyp);
}
}
}
}
getch();
}
#include
#include
void main()
{
int s1,s2,hyp,ptriple=0;
clrscr();
printf(" Side1 ---- side2 ---- hypotenus\n");
for(s1=0;s1<=100;s1++)
{
for(s2=0;s2<=100;s2++)
{
for(hyp=1;hyp<=100;hyp++)
{
ptriple=(s1*s1)+(s2*s2);
hyp=hyp*hyp;
if(hyp==ptriple)
{
hyp=sqrt(hyp);
printf("\n%d ---- %d ---- %d ",s1,s2,hyp);
}
}
}
}
getch();
}
Thursday, 1 December 2011
Wednesday, 9 November 2011
Write a C program in C to carry out following operations on strings using library functions... 1) To concatenate a string s2 to string s1 2) To find the length of a given string 3) To compare two strings s1 nad s2 4) To copy a string s2 to another string s1
# include<stdio.h>
void main()
{
char a[20],b[20],c[20],d[20];
int z1=0,z2=0;
clrscr();
printf("\nEnter First String : ");
scanf("%s",&a);
printf("\nEnter Second String: ");
scanf("%s",&b);
printf("\n*** Length *** \n");
z1 = strlen(a);
printf("\nLength of String1 = %d\n",z1);
z2 = strlen(b);
printf("\nLength of String2 = %d\n",z2);
printf("\n*** Compare ***\n");
if (z1>z2)
{
printf("\nString1 is greater than String2\n");
}
else if(z1<z2)
{
printf("\nString2 is greater than String1\n");
}
else if (z1==z2)
{
printf("\nBoth strings are equal....\n");
}
printf("\n*** Copy ***\n");
printf("\nBefore Copy\n");
printf("\nString1 = %s\n",a);
printf("\nString2 = %s\n",b);
printf("\nAfter Copy\n");
z1 = strcpy(a,b);
printf("\nString1 = %s\n",z1);
printf("\nString2 = %s\n",b);
printf("\n*** Concatenate ***\n");
z1 = strcat(a,b);
printf("\nThe concatinated value is %s\n",z1);
getch();
}
Write a program in C to compute addition/subraction of two matrices.Use functions to read,display and add/subtract the matrices.
# include<stdio.h>
# include<conio.h>
void main()
{
void show(int c[2][2]);
void insert(int l[2][2]);
void add(int a[2][2],int b[2][2]);
void sub(int a[2][2],int b[2][2]);
int x[2][2],y[2][2],op;
clrscr();
printf("\n Enter First Matrix : \n");
insert(x);
printf("\n Eneter Second Matrix: \n");
insert(y);
printf("\n First Matrix is : \n");
show(x);
printf("\n");
printf("\n Second Matrix is : \n");
show(y);
printf(" \n\n");
printf("\n 1) Addition \n");
printf("\n 2) Subtraction \n");
printf("\nEnter your option: \n\n");
scanf("%d",&op);
switch(op)
{
case 1 : add(x,y);
break;
case 2 : sub(x,y);
break;
}
getch();
}
// insert function allow us to insert values of matrix
void insert(int c[2][2])
{
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d",&c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of insert function
// show function allow us to display the element of matrix
void show(int c[2][2])
{
int i,j;
printf(" \n ");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf(" %d ",c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of show function
// add function allow us to add two matrix
void add(int a[2][2],int b[2][2])
{
int i,j,c[2][2];
printf("\n\nAddition is \n\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j] = a[i][j] + b[i][j]; // Addition of two matrix is perform here
printf("\t %d ",c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of add function
// sub function allow us to Subtract two matrix
void sub(int a[2][2],int b[2][2])
{
int i,j,c[2][2];
printf("\n\n Sunstraction is \n\n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j] = a[i][j] - b[i][j]; // Substraction of two matrix is perform here
printf("\t %d ",c[i][j]);
} // end of inner for loop
} // end of outer for loop
} // end of sub function
// Write a program in C to find out prime numbers between 1 to "N"
#include<stdio.h>
void main()
{
int i,n=1,num;
clrscr();
printf("\nEnter your range to find out prime numbers: ");
scanf("%d",&num);
printf("\nPrime numbers between %d are: \n\n",num);
while(n<num) // Outer while loop
{
i=2;
while(i<num) // Inner while loop
{
if(n%i==0)
{
break;
} // end of if statement
else
{
i++;
} // end of else statement
} // End of inner while loop
if(i==n)
{
printf(" %d ",i);
}
n++;
} // end of outer while loop
getch();
}
Output:-
Enter your range to find out prime numbers: 100
Prime numbers between 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
79 83 89 97
Subscribe to:
Posts (Atom)