#include
#include
void main()
{
int i,f,n;
f=1;
cout <<"enter the number \n";
cin > > n;
for(i=1;i<=n;i++)
f=f*i;
cout <<"Factorial of number is "<<f;
getch();
}
factorial of a number in C++
C Program to find twin prime numbers, twin Prime Numbers by using C language
C Program to find twin prime numbers, twin Prime Numbers by using C language
void main()
{
int n,i,k,r,ary[50],p;
clrscr();
printf(" Enter Maximum Value For Numbers: ");
scanf("%d",&r);
i=1;
p=0;
while(i<=r)
{
k=0;n=1;
while(n<=i)
{
if(i%n==0)
k++;
n++;
}
if(k==2)
{
ary[p]=i;
p++;
}
i++;
}
for(n=0;n<p;n++)
{
if(ary[n+1]-ary[n]==2)
printf("\n %d and %d are TWIN PRIME Numbers ",ary[n],ary[n+1]);
}
getch();
}
c program which restricts the movement of pointer
void main()
{
union REGS k,o;
//show mouse pointer
k.x.ax=1;
int86(0x33,&k,&o);
//x coordinate restriction
k.x.ax=7;
k.x.cx=20;
k.x.dx=300;
int86(0x33,&k,&o);
//y coordinate restriction
k.x.ax=8;
k.x.cx=50;
k.x.dx=250;
int86(0x33,&k,&o);
getch();
}
Most c compilers Most widely used
Most c compilers Most widely used
There are various c compilers are variables. Some of these are:
S.N. | Name | Microprocessor | OS |
1 | Turbo c 3.0 | 8086 | MS DOS |
2 | ANSIC C | 80386 | LINUX |
3 | Borland C 4.0 | 80386 | WINDOW |
4 | Microsoft C | 8086 | MS DOS |
5 | Visual C++ | 80386 | WINDOW |
c program to create dos command DIR, Program to create a DIR Dos Command
c program to create dos command DIR, Program to create a DIR Dos Command
#include “stdio.h”
#include “dos.h”
void main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else{
printf("File not found");
}
}
c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)
c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)
#include”dos.h”
#include”stdio.h”
void main()
{
union REGS i,o;
int x,y,k;
//show mouse pointer
i.x.ax=1;
int86(0x33,&i,&o);
while(!kbhit()) //its value will false when we hit key in the key board
{
i.x.ax=3; //get mouse position
x=o.x.cx;
y=o.x.dx;
clrscr();
printf("(%d , %d)",x,y);
delay(250);
int86(0x33,&i,&o);
}
getch();
}
C code to print or display lower triangular matrix
C code to print or display lower triangular matrix
#include
int main(){
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements For the matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\n Setting zero in upper triangular matrix\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i<=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}
return 0;
}
Strassen algorithm C Program to Multiply matrix
C Program of two 2 by 2 matrix multiplication using Strassen algorithm (It is faster than the standard matrix multiplication algorithm ), Strassen algorithm C Program to Multiply matrix
#include
int main(){
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
printf("Enter the 4 elements For first matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("Enter the 4 elements For second matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\nThe first matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe second matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
printf("\nAfter multiplication using Strassen's matrix\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Sum of diagonal elements of a matrix in c
Sum of diagonal elements of a matrix in c
#include
int main(){
int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);
return 0;
}
C Program to print or display lower triangular matrix
C Program to print or display lower triangular matrix
#include
int main(){
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of the matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nSetting zero in upper triangular matrix\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i<=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}
return 0;
}
C program to find inverse of a matrix , inverse of a matrix in C
C program to find inverse of a matrix , inverse of a matrix in C
#include
int main(){
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
printf("\nInverse of matrix is: \n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
printf("\n");
}
return 0;
}
Write a c program to find out transport of a matrix
#include
int main(){
int a[10][10],b[10][10],i,j,k=0,m,n;
printf("\n Enter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;ifor(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
printf("\n%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",b[i][j]);
}
}
return 0;
}
KeyWords : transport of a matrix in C, transport of a matrix Programming in C Language, C Assignment to transport of a matrix,Matrix Program in C
c program to find out NCR factor of given number
c program to find out NCR factor of given number
#include
int main(){
int n,r,ncr;
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is %d",n,r,ncr);
return 0;
}
int fact(int n){
int i=1;
while(n!=0){
i=i*n;
n--;
}
return i;
}
C program to print Pascal triangle using for loop
C program to print Pascal triangle using for loop
#include
int main(){
int line,i,j,k;
printf("Enter the no. of lines");
scanf("%d",&line);
for(i=1;i<=line;i++){
for(j=1;j<=line-i;j++)
printf(" ");
for(k=1;k printf("%d",k);
for(k=i;k>=1;k--)
printf("%d",k);
printf("\n");
}
return 0;
}
c program to print Floyd’s triangle
c program to print Floyd’s triangle
OUTPUT :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
#include
int main(){
int i,j,r,k=1;
printf("Enter the range: ");
scanf("%d",&r);
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
return 0;
}
OUTPUT :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
C Program To FIND POWER OF A NUMBER USING C PROGRAM
C Program To FIND POWER OF A NUMBER USING C PROGRAM
#include
int main(){
int pow,num,i=1;
long int sum=1;
printf("\n Enter a number: ");
scanf("%d",&num);
printf("\n Enter Value for power: ");
scanf("%d",&pow);
while(i<=pow){
sum=sum*num;
i++;
}
printf("\n%d to the power %d is : %ld",num,pow,sum);
return 0;
}
C program to print Armstrong numbers from 1 to 500
C program to print Armstrong numbers from 1 to 500
#include
int main(){
int num,r,sum,temp;
for(num=1;num<=500;num++){
temp=num;
sum = 0;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
Check the given number is Armstrong number or not using c program
Check the given number is Armstrong number or not using c program
#include
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
C Program to Check Whether Number is Perfect Or Not
C Program to Check Whether Number is Perfect Or Not
#include
int main()
{
int n,i=1,sum=0;
printf("\n Please Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("\n%d is a Perfect Number",i);
else
printf("\n%d is Non Perfect Number",i);
return 0;
}
C Program to insert and delete a node from the binary search tree
C Program to insert and delete a node from the binary search tree
#include
#include
#include
#define TRUE 1
#define FALSE 0
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert(struct btreenode **,int);
void del(struct btreenode **,int);
void search(struct btreenode **,int,struct btreenode **,struct btreenode **,int *);
void inorder(struct btreenode *);
void main()
{
struct btreenode *bt;
int req;
int i=0,num,a[]={10,7,11,5,8,12,16,15,6};
bt=NULL; /* Empty Tree */
clrscr();
while(i<=8) { insert(&bt,a[i]); i++; } clrscr(); printf(" Binary tree before deletion :\n"); inorder(bt); del(&bt,11); printf("\n Binary tree after deletion :\n"); inorder(bt); del(&bt,10); printf("\n Binary tree after deletion :\n"); inorder(bt); del(&bt,6); printf("\n Binary tree after deletion :\n"); inorder(bt); del(&bt,16); printf("\n Binary tree after deletion :\n"); inorder(bt); getch(); } /* inserts a new node in a binary search tree */ void insert(struct btreenode **sr,int num) { if(*sr==NULL) { (*sr)=malloc(sizeof(struct btreenode)); (*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /* Search the node to whilch new node will be attatched */
{
/* If new data is less, traverse to left*/
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
/* Else traverse to right */
insert(&((*sr)->rightchild),num);
}
}
/* Deletes a node from the binary search tree */
void del(struct btreenode **root,int num)
{
int found;
struct btreenode *parent,*x,*xsucc;
/* If tree is empty */
if(*root == NULL)
{
printf("\n Tree is Empty ");
return;
}
parent=x=NULL;
/* Call to search function to find the node to be deleted */
search(root,num,&parent,&x,&found);
/* If the node to be deleted is not found */
if(found == FALSE)
{
printf("\n Data to be deleted , not found ");
return;
}
/* If the node to be deleted has two children */
if(x->leftchild !=NULL && x->rightchild!=NULL)
{
parent=x;
xsucc=x->rightchild;
while(xsucc->leftchild != NULL)
{
parent=xsucc;
xsucc=xsucc->leftchild;
}
x->data=xsucc->data;
x=xsucc;
}
/* If the node to be deleted has no child */
if(x->leftchild==NULL && x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->rightchild=NULL;
free(x);
return;
}
/* If the node to be deleted has only right child */
if(x->leftchild==NULL && x->rightchild !=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
/* If the node to be deleted has only left child */
if(x->leftchild != NULL && x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
}
/* Returns the address of the node to be deleted ,address of its parent and whether the node is found or not */
void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{
struct btreenode *q;
q=*root;
*found=FALSE;
*par=NULL;
while(q!=NULL)
{
/* If the node to be deleted is found */
if(q->data == num)
{
*found=TRUE;
*x=q;
return;
}
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data > num)
q=q->leftchild;
else
q=q->rightchild;
}
}
/* Traverse a binary search tree in an LDR(Left-Data-Right) fashion */
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
/* Print the data of the node whose leftchild is NULL or the path has already been traversed */
printf("%d\t",sr->data);
inorder(sr->rightchild);
}
}