Saturday, May 21, 2016

A Program in C using DO-WHILE Loop to find square of the number

Program code:

#include<stdio.h>
#include<conio.h>
int main()
{
int num,op;
//clrscr();
do
{
printf("enter a number:\n");
scanf("%d",&num);
printf("square root of %d is %d\n",num,num*num);
printf("want to try another number? \n(enter 1 for yes /0 for no)\n");
scanf("%d",&op);
}
while(op==1);
//getch();
return 0;
}

output:

enter a number:
2
square root of 2 is 4
want to try another number?
(enter 1 for yes/0 for no)
n

Friday, February 13, 2015

C Program for Strassen Matrix Multiplication

#include<conio.h>
 #include<stdio.h>
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 of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of 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 \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",c[i][j]);
   }

getch();
   return 0;
}

Output:


C Program for LRU (Last Recently Used) Page replacement technique implementation

#include<stdio.h> #include<conio.h> int n,ref[100],fs,frame[100],count=0; void input(); void show(); void cal(); void main() { printf("****************** LRU Page Replacement Algo *********************\n"); input(); cal(); show(); getch(); } void input() { int i; printf("Enter no of pages in Refrence String\t"); scanf("%d",&n); printf("Enter the reference string:"); for(i=0;i<n;i++) scanf("%d",&ref[i]); printf("Enter the Frame Size\t"); scanf("%d",&fs); } void cal() { int i,j,k=0,c1,c2[100],r,temp[100],t; frame[k]=ref[k]; count++; k++; for(i=1;i<n;i++) { c1=0; for(j=0;j<fs;j++) { if(ref[i]!=frame[j]) c1++; } if(c1==fs) { count++; if(k<fs) { frame[k]=ref[i]; k++; } else { for(r=0;r<fs;r++) { c2[r]=0; for(j=i-1;j<n;j--) { if(frame[r]!=ref[j]) c2[r]++; else break; } } for(r=0;r<fs;r++) temp[r]=c2[r]; for(r=0;r<fs;r++) { for(j=r;j<fs;j++) { if(temp[r]<temp[j]) { t=temp[r]; temp[r]=temp[j]; temp[j]=t; } } } for(r=0;r<fs;r++) { if(c2[r]==temp[0]) frame[r]=ref[i]; } } } } } void show() { printf("Page Faults = %d",count); }


Output:




Thursday, February 12, 2015

C program for number pattern

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,rng;
clrscr();
printf("enter the range");
scanf("%d",&rng);
for(i=1;i<=rng;i++)
{
printf("\n");
for(j=1;j<=i;j++)
printf("%2d",j);
}
getch();
}

output:


C program to create diamond pattern in C

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,l,rng;
char c='*';
clrscr();
printf("enter the range");
scanf("%d",&rng);
for(i=rng;i>=0;i--)
{
printf("\n");
for(j=0;j<=i;j++)
printf("%c",c);
for(k=i;k<=2*rng-i;k++)
printf(" ");
for(l=0;l<=i;l++)
printf("%c",c);
}
for(i=0;i<=rng;i++)
{
printf("\n");
for(j=0;j<=i;j++)
printf("%c",c);
for(k=i;k<=2*rng-i;k++)
printf(" ");
for(l=0;l<=i;l++)
printf("%c",c);
}
getch();
}

output:

Program to print pyramid pattern in c

 #include <stdio.h>
 #include <conio.h>
int main()
{
   int row, c, p, temp;

   printf("Enter the number of rows in pyramid ");
   scanf("%d",&p);

   temp = p;

   for ( row = 1 ; row <= p ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");

      temp--;

      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");

      printf("\n");
   }

   return 0;
}

output



Bitstuffing Program in C

#include<stdio.h> #include<conio.h> #include<string.h> int main() { int i, j,count=0,nl; char str[100]; printf("enter the bit string: "); gets(str); for (i=0;i<strlen(str);i++) { count=0; //the following code search the six ones in given string for (j=i;j<=(i+5);j++) { if(str[j]=='1') { count++; } } //if there is six ones then folling code execute to bit stuffing after five ones if(count==6) { nl=strlen(str)+2; for (;nl>=(i+5);nl--) { str[nl]=str[nl-1]; } str[i+5]='0'; } } puts(str); getch(); }