Strong Number
void strong_number()
{
int num,i,p,r,sum=0,save_num;
printf("\n Enter a number");
scanf("%d",&num);
save_num=num;
while(num)
{
i=1,p=1;
{
int num,i,p,r,sum=0,save_num;
printf("\n Enter a number");
scanf("%d",&num);
save_num=num;
while(num)
{
i=1,p=1;
r=num%10;
while(i<=r)
{
p=p*i;
i++;
} //while
sum=sum+p;
num=num/10;
} //while
if(sum==save_num)
printf("%d is a Strong number", save_num);
else
printf("%d is not a Strong number", save_num);
}
Subtract without subtract operator
#include<stdio.h>
int main()
{
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
return 0;
}
Sum of Digit
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,num,x,sum=0;
printf("Enter a number=");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("Sum of digits of a number=%d",sum);
getch();
}
#include<conio.h>
void main()
{
clrscr();
int n,num,x,sum=0;
printf("Enter a number=");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("Sum of digits of a number=%d",sum);
getch();
}
Swap two numbers using bitwise operators
#include <stdio.h>
int main()
{
int i = 65;
int k = 120;
printf("\n value of i=%d k=%d before swapping", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\n value of i=%d k=%d after swapping", i, k);
return 0;
}
int main()
{
int i = 65;
int k = 120;
printf("\n value of i=%d k=%d before swapping", i, k);
i = i ^ k;
k = i ^ k;
i = i ^ k;
printf("\n value of i=%d k=%d after swapping", i, k);
return 0;
}
Swap String
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
main()
{
char first[100], second[100], *temp;
printf("Enter the first string ");
gets(first);
printf("Enter the second string ");
gets(second);
printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);
temp = (char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);
getch();
return 0;
}
#include<string.h>
#include<malloc.h>
#include<conio.h>
main()
{
char first[100], second[100], *temp;
printf("Enter the first string ");
gets(first);
printf("Enter the second string ");
gets(second);
printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);
temp = (char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);
getch();
return 0;
}
Swap Two Number
#include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d", &x, &y);
printf("Before Swapping\n x = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\n x = %d\ny = %d\n",x,y);
return 0;
}
Swap Without using third variable
#include <stdio.h>
void main()
{
int a,b;
printf("Enter number1: ie a");
scanf("%d",&a);
printf("Enter number2:ie b ");
scanf("%d",&b);
printf(value of a and b before swapping is a=%d,b=%d"a,b);
a=a+b;
b=a-b;
a=a-b;
printf("value of a and b after swapping is a=%d,b=%d"a,b);
}
void main()
{
int a,b;
printf("Enter number1: ie a");
scanf("%d",&a);
printf("Enter number2:ie b ");
scanf("%d",&b);
printf(value of a and b before swapping is a=%d,b=%d"a,b);
a=a+b;
b=a-b;
a=a-b;
printf("value of a and b after swapping is a=%d,b=%d"a,b);
}
Swap Without using third variable
#include <stdio.h>
void main()
{
int a,b;
printf("Enter number1: ie a");
scanf("%d",&a);
printf("Enter number2:ie b ");
scanf("%d",&b);
printf(value of a and b before swapping is a=%d,b=%d"a,b);
a=a+b;
b=a-b;
a=a-b;
printf("value of a and b after swapping is a=%d,b=%d"a,b);
}
void main()
{
int a,b;
printf("Enter number1: ie a");
scanf("%d",&a);
printf("Enter number2:ie b ");
scanf("%d",&b);
printf(value of a and b before swapping is a=%d,b=%d"a,b);
a=a+b;
b=a-b;
a=a-b;
printf("value of a and b after swapping is a=%d,b=%d"a,b);
}
Topological Sort
#include<stdio.h>
#define MAX 200
int n,adj[MAX][MAX];
int front = -1,rear = -1,queue[MAX];
void main()
{
int i,j = 0,k;
int topsort[MAX],indeg[MAX];
create_graph();
printf(“The adjacency matrix is:\n”);
display();
for(i=1;i<+n;i++)
{
indeg[i]=indegree(i);
if(indeg[i]==0)
insert_queue(i);
}
while(front<=rear)
{
k=delete_queue();
topsort[j++]=k;
for(i=1;ilt;=n;i++)
{
if(adj[k][i]==1)
{
adj[k][i]=0;
indeg[i]=indeg[i]-1;
if(indeg[i]==0)
insert_queue(i);
}
}
}
printf("Nodes after topological sorting are:\n");
for(i=0;i<=n;i++)
printf("%d",topsort[i]);
printf("\n");
}
create_graph()
{
int i,max_edges,origin,destin;
printf("\n Enter number of vertices:");
scamf("%d",&n);
max_edges = n * (n - 1);
for(i = 1;i <= max_edges;i++)
{
printf("\n Enter edge %d (00 to quit):",i);
scanf("%d %d",&origin,&destin);
if((origin == 0) && (destin == 0))
{
printf("Invalid edge!!\n");
i–;
}
else
adj[origin][destin] = 1;
}
return;
}
display()
{
int i,j;
for(i = 0;i <= n;i++)
{
for(j = 1;jrear)
{
printf(“Queue Underflow”);
return;
}
else
{
del_item = queue[front];
front = front + 1;
return del_item;
}
}
int indegree(int node)
{
int i,in_deg = 0;
for(i = 1;i <= n;i++)
if(adj[i][node] == 1)
in_deg++;
return in_deg;
}
}
#define MAX 200
int n,adj[MAX][MAX];
int front = -1,rear = -1,queue[MAX];
void main()
{
int i,j = 0,k;
int topsort[MAX],indeg[MAX];
create_graph();
printf(“The adjacency matrix is:\n”);
display();
for(i=1;i<+n;i++)
{
indeg[i]=indegree(i);
if(indeg[i]==0)
insert_queue(i);
}
while(front<=rear)
{
k=delete_queue();
topsort[j++]=k;
for(i=1;ilt;=n;i++)
{
if(adj[k][i]==1)
{
adj[k][i]=0;
indeg[i]=indeg[i]-1;
if(indeg[i]==0)
insert_queue(i);
}
}
}
printf("Nodes after topological sorting are:\n");
for(i=0;i<=n;i++)
printf("%d",topsort[i]);
printf("\n");
}
create_graph()
{
int i,max_edges,origin,destin;
printf("\n Enter number of vertices:");
scamf("%d",&n);
max_edges = n * (n - 1);
for(i = 1;i <= max_edges;i++)
{
printf("\n Enter edge %d (00 to quit):",i);
scanf("%d %d",&origin,&destin);
if((origin == 0) && (destin == 0))
{
printf("Invalid edge!!\n");
i–;
}
else
adj[origin][destin] = 1;
}
return;
}
display()
{
int i,j;
for(i = 0;i <= n;i++)
{
for(j = 1;jrear)
{
printf(“Queue Underflow”);
return;
}
else
{
del_item = queue[front];
front = front + 1;
return del_item;
}
}
int indegree(int node)
{
int i,in_deg = 0;
for(i = 1;i <= n;i++)
if(adj[i][node] == 1)
in_deg++;
return in_deg;
}
}
Transpose A Matrix
#include <stdio.h>
#include <conio.h>
int main()
{
int m, n, i, j;
int mat[10][10], trans[10][10];
printf(" Enter the number of rows and columns of matrix ");
scanf(" %d %d ", &m, &n);
printf(" Enter the elements of matrix \n ");
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
scanf(" %d ", &mat[i][j] );
}
}
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
trans[j][i] = mat[i][j];
}
}
printf(" Transpose of entered matrix :-\n ");
for( i = 0 ; i < n ; i++ )
{
for( j = 0 ; j < m ; j++ )
{
printf(" %d\t ", trans[i][j] );
}
printf(" \n ");
}
getch();
return 0;
}
#include <conio.h>
int main()
{
int m, n, i, j;
int mat[10][10], trans[10][10];
printf(" Enter the number of rows and columns of matrix ");
scanf(" %d %d ", &m, &n);
printf(" Enter the elements of matrix \n ");
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
scanf(" %d ", &mat[i][j] );
}
}
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
trans[j][i] = mat[i][j];
}
}
printf(" Transpose of entered matrix :-\n ");
for( i = 0 ; i < n ; i++ )
{
for( j = 0 ; j < m ; j++ )
{
printf(" %d\t ", trans[i][j] );
}
printf(" \n ");
}
getch();
return 0;
}