Tuesday, December 6, 2011

Write a Cprogram to "Addition of matrix row and column are user given"

#include<stdio.h>
#include<conio.h>

void main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
   clrscr();

   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of first matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&first[c][d]);

   printf("Enter the elements of second matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d",&second[c][d]);

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d]+ second[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t",sum[c][d]);

      printf("\n");
   }

   getch();
}

Write a C Program to swapping value until user stop

#include <stdio.h>
#include <conio.h>
void main()
{
 int i,a,b;
 char s[10];
 clrscr();
 for(i=0;;i++)
 {
  printf("A= ");
  scanf("%d",&a);
  printf("B= ");
  scanf("%d",b);
  if(a==b)
  {
   printf("Try again with different value");
   printf("If you continue type Y other wise N: ");
   scanf("%s",&s);
   if(s=="y")
   {
    continue;
   }
   else
   {
    break;
   }
  }
  else
  {
   printf("\n\n\nAfter Swappint...\nA= %d\nB= %d",b,a);
   printf("If you continue type Y other wise N: ");
   scanf("%s",&s);
   if(s=="y")
   {
    continue;
   }
   else
   {
    break;
   }
  }
 }
 getch();
}

Write a C Program to find Factorial of all element of user given limit

#include <stdio.h>
#include <conio.h>
void main()
{
 int n,i,r,j,l;
 clrscr();
 printf("Enter Number of limit for calculating factorial: ");
 scanf("%d",&l);
 for(;l!=0;l--)
 {
  n=l;
  printf("\n!%d= ",n);
  switch(n)
  {
   case 1:
   {
    printf("1= ");
    r=1;
    break;
   }
   case 2:
   {
    printf("2*1= ");
    r=2;
    break;
   }
   default:
   {

    r=n*(n-1);
    for(j=n;j!=1;j--)
    {
     printf("%d*",j);
    }
    printf("1= ");
    for(i=n-2;i!=1;i--)
    {
     r=r*i;
    }
   }
  }
   printf("%d",r);
 }

 getch();
}

Write a C Program to fild string length without library function

#include <stdio.h>
#include <string.h>
void main()
{
 int c;
 char a[50];
 printf("Enter A String: ");
 gets(a);
 for(c=0;a[c]!='\0';c++);
 printf("\nYour entertd string is \"%s\" and its Length is \"%d\"",a,c);

}

Write a Cprogram to create a Employee data structure and save them into a file

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
 FILE *fp;
 char another='y';
 struct emp
 {
  char name[40];
  int age;
  float bs;
 };
 struct emp e;
 fp=fopen("EMPLOYEE.doc","w");
 if(fp==NULL)
 {
  puts("Cannot open file");
  exit(1);
 }
 while(another=='y')
 {
  printf("\nEnter Name: ");
  scanf("%s",&e.name);
  printf("Enter age: ");
  scanf("%d",&e.age);
  printf("Enter salary: ");
  scanf("%f",&e.bs);
  fprintf(fp,"%s\t%d\t%f\n",e.name,e.age,e.bs);
  printf("Add Another Record (y/n): ");
  fflush(stdin);
  another=getche();
 }
 fclose(fp);
 return 0;
}

Write a C Program to copy a file to another file

#include <stdio.h>
#include <conio.h>
void main()
{
  FILE *p,*q;
  char file1[20],file2[20],ch;
  clrscr();
  printf("\nEnter the source file name to be copied: ");
  gets(file1);
  p=fopen(file1,"r");
  if(p==NULL)
  {
      printf("cannot open %s",file1);
  }
  printf("\nEnter the destination file name:");
  gets(file2);
  q=fopen(file2,"w");
  if(q==NULL)
  {
      printf("cannot open %s",file2);
  }
  while((ch=getc(p))!=EOF)
      putc(ch,q);
  printf("\nCOMPLETED");
  fclose(p);
  fclose(q);
 getch();
}

Write a C Program to create a File and write some text line and read thisfile

#include <stdio.h>
#include <conio.h>
void main()
{
 FILE * fp;
 char s[800];
 clrscr();
 fp=fopen ("Salary.txt","w");
 if(fp==NULL)
 {
  puts("\nFile creating unsuccessful");
  exit(1);
 }
 printf("\nEnter a few line:\n ");
 while(strlen(gets(s))>0)
 {
  fputs(s,fp);
  fputs("\n",fp);
 }
 fp=fopen("NONAME00.C","r");
 if(fgetc(fp)==NULL)
 {
  return(EOF);
 }
 while(fgets(s,79,fp)!=NULL)
 {
  printf("%s",s);
 }
 fclose(fp);
 getch();
}

Write a C program to find Factorial value by switch - case

#include <stdio.h>
#include <conio.h>
void main()
{
 int n,i,r,j;
 clrscr();
 printf("Enter Number for calculating factorial: ");
 scanf("%d",&n);
 printf("!%d= ",n);
 switch(n)
 {
  case 0:
  {
   printf("0= ");
   r=0;
   break;
  }
  case 1:
  {
   printf("1= ");
   r=1;
   break;
  }
  default:
  {

   r=n*(n-1);
   for(j=n;j!=1;j--)
   {
    printf("%d*",j);
   }
   printf("1= ");
   for(i=n-2;i!=1;i--)
   {
    r=r*i;
   }
  }
 }
   printf("%d",r);

 getch();
}

Write a C program to create pyramid number of user given limit

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,x=40,y=10;
clrscr();
printf(“Enter n (between 2 & 9)\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
gotoxy(x,y); /* To take the cursor to the co-ordinates x & y */
for(j=1;j<=i;j++)
{
 printf(“%d “,i);
}
x=x-1;
y++;
}
getch();
}

Write a C Program to Create a student Data Structure

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
 char another='y';
 struct student
 {
  char name[40];
  int roll;
  float marks;
 };
 struct student e;
 clrscr();
 while(another=='y')
 {
  printf("\nEnter Name: ");
  scanf("%s",&e.name);
  printf("Enter Roll: ");
  scanf("%d",&e.roll);
  printf("Enter Marks: ");
  scanf("%f",&e.marks);
  printf("Add Another Record (y/n): ");
  fflush(stdin);
  another=getche();
 }
 getch();
}

Write a C program to copy a String to one variable to another variable by library function

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
 char a[25],b[25];
 clrscr();
 printf("Please Enter A String: ");
 gets(a);
 strcpy(b,a);
 printf("Your Entered string \"%s\"\nAfter copying string is \"%s\"",a,b);
 getch();
}

Write a C program to find Root of X as equation ax^2+bx+c=0 where value of a,b,c given by User (Complex Number)

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
 int a,b,c,x1,x2,d;
 clrscr();
 printf("Enter three value: ");
 scanf("%d %d %d",&a,&b,&c);
 d=sqrt((b*b)-(4*a*c));
 if(d>0)
 {
  x1=(-b+d)/(2*a);
  x2=(-b-d)/(2*a);
  printf("x= %d, %d",x1,x2);
 }
 else
 {
  printf("Your entered value is incorrect %d",d);
 }
 getch();
}

Write a C Program to example of CALL - BY - VALUE and CALL - BY - ADDRESS (REFERENCE)

#include <stdio.h>
#include <conio.h>
int "call-by-value"(int, int);
void "call-by-address"(int*,int*,int*,int*);
void main()
{
 int a=6,b=10,sum,mul;
 clrscr();
 printf("\n&a= %d\n&B= %d\n&SUM= %d\n&MUL= %d",&a,&b,&sum,&mul);
 sum= "call-by-value" (a,b);
 printf("\n\n%d+%d=%d",a,b,sum);
 printf("\nA= %d, B= %d",a,b);
 "call-by-address"(&a,&b,&sum,&mul);
 printf("\n After swapping:\nA= %d, B= %d,\nSUM =%d,\nMUL= %d",sum,mul,a,b);
}
int "call-by-value" (int x, int y)
{
 int z;
 x=65;
 z=x+y;
 return(z);
}
void "call-by-adderss" (int*m,int*n,int*q,int*r)
{
 int p;
 *m=65;
 p=*m;
 *m=*n;
 *n=*p;
 *q=*m+*n;
 *r=*m**n;
}

Write a C Program to create CALCULATOR by SWITCH - CASE Operation

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
 int a,b,ch,d,e,i,n;
 float c;
 clrscr();
 printf("\tCalculator Made by SURAJIT SADHUKHAN\n\n\t\t\t\t+-*/%WELCOME%/*-+");
 printf("\n\t 1. Addition\n\t 2. Subtraction\n\t 3. Multiplication\n\t 4. Division\n\t 5. Reminder operation\n\t 6. Squre root\n\t 7. Power operation\n\t 8. Memory Operation.\n\tNow Enter Your Choice: ");
 scanf("%d",&ch);
 printf("\n\tEnter First Number\n\tfor addition, subtraction, multiplication , Division ,reminder operation and squreroot \n\t Number for power operation: ");
 scanf("%d",&a);
 printf("\n\tEnter second number for addition\n\tsubtraction, multiplication, division and reminder operation\n\tpower: ");
 scanf("%d",&b);
 printf("\n\tEntered Choice No=%d",ch);
 switch (ch)
 {
  case 1:
  {
  d=(a+b);
  printf("\n\t Your choice is Addition \n\t your result is: %d",d);
  break;
  }
  case 2:
  {
   d=(a-b);
   printf("\n\t Your choice is Subtraction\n\t Your result is: %",d);
   break;
  }
  case 3:
  {
   d=a*b;
   printf("\n\t Your choice is Multiplication\n\t Your result is: %d",d);
   break;
  }
  case 4:
  {
   c=a/b;
   printf("\n\t Your choice is Divition\n\t Your Result is: %.2f",c);
   break;
  }
  case 5:
  {
   d=a%b;
   printf("\n\t Your Choice is Reminder Operation\n\t Your result is: %d",d);
   break;
  }
  case 6:
  {
   c=sqrt(a);
   printf("\n\tYour choice is root of First Number\n\t Your result is: %.2f",c);
   break;
  }
  case 7:
  {
   d=a^b;
   printf("\n\tYour choice is power calculation \n\t Your result is: %d",d);
   break;
  }
  case 8:
  {
   d=a;
   e=-b;
   printf("\n\tYour choice is Memory operation\n\t Your stored positive number is %d and stored negative number is %d",d,e);
   break;
  }
  default:
  {
   printf("\n\t Your choice is not listed");
  }
 }
 getch();
}

Write a C program to "multiply of 5 of a range of number"

//If limit is 2 then output will 5*1=5 \n 5*2=10

#include <stdio.h>
#include <conio.h>
void main()
{
 int n,i,m;
 clrscr();
 printf("Enter limitation of this calculation: ");
 scanf("%d",&n);
 for(i=0;n>=i;i++)
 {
  m=5*i;
  printf("5*%d=%d\n",i,m);
 }
 getch();
}

Write a C program to find Factorial value without any function

#include <stdio.h>
#include <conio.h>
void main()
{
 int n,i,r=0,j;
 clrscr();
 printf("Enter Number for calculating factorial: ");
 scanf("%d",&n);
 printf("!%d= ",n);
 r=n*(n-1);
 for(j=n;j!=1;j--)
 {
  printf("%d*",j);
 }
 printf("1= ");
 for(i=n-2;i!=1;i--)
 {
  r=r*i;
 }
 printf("%d",r);
 getch();
}

Write a C Program to convert a Decimal number to Binary Number

#include <stdio.h>
#include <conio.h>
void main()
{
 int a,b,no[100],i,c,n=0;
 clrscr();
 printf("\nEnter Decimal Number: ");
 scanf("%d",&b);
 if(b>0)
 {
  a=b;
  printf("\nThe binary equivalant of decimal number is: 0");
 }
 else
 {
  a=-b;
  printf("\nThe binary equivalant of decimal number is: 1") ;
 }
 for(i=0;a!=0;++i)
 {
  c=a%2;
  no[i]=c;
  a=a/2;
  ++n;
 }

 for(i=n-1;i>=0;--i)
 {
  printf("%d",no[i]);
 }
 getch();
}

Write a C program to perform of Interchange charecter by ASCII code

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
 char s;
 int a,n;
 clrscr();
 printf("This a program to convert upper case lower case and find ASCII value and IF You know ASCII Value , it find charecter and change case");
 printf("\n\n\n\tChange ASCII to charecter press 1 \n\tchange charecter to ASCII code press 2\n\tASCII Code to charecter Press 3\n\t know ASCII code to charecter Press 4::\n\t Enter your choice: ");
 scanf("%d",&n);
 switch(n)
 {
  case 1:
  {
   printf("\nEnter ASCII code: ");
   scanf("%d",&a);
   printf("Charecter is %c",a);
   if(a>=65&&a<=90)
   {
    printf("\nYou entered a upper case letter\nYour lower case letter is %c",(a+32));
   }
   if(a>=97&&a<=122)
   {
    printf("\nYou entered a lower case letter\nYour upper case letter is %c",(a-32));
   }
   break;
   case 2:
   {
    printf("\nEnter Charecter: ");
    scanf("%s",&s);
    a=s;
    printf("ASCII Code is %d",a);
    if(a>=65&&a<=90)
    {
     printf("You entered a upper case letter\nYour lower case letter is %c",(s+32));
    }
    if(a>=97&&a<=122)
    {
     printf("\nYou entered a lower case letter\nYour upper case letter is %c",(s-32));
    }
    break;
   }
   case 3:
   {
    printf("\nEnter Charecter: ");
    scanf("%s",&s);
    printf("ASCII Code is %d",s);
    break;
   }
   case 4:
   {
    printf("\nEnter ASCII: ");
    scanf("%d",&a);
    printf("Charecter is %c",a);
    break;
   }
   default:
   {
   printf("\n\tInvalid choice!!!");
   }
  }
 }
 getch();
}

Write a C Program to find Area or Triangle

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
 int a,b,c,p;
 float s,temp,area;
 clrscr();
 printf("Sides: ");
 scanf("%d %d %d",&a,&b,&c);
 if(a>0&&b>0&&c>0)
 {
  p=(a+b+c);
  s=p/2;
  temp=s*(s-a)*(s-b)*(s-c);
  if(temp>0)
  {
   area=sqrt(temp);
   printf("\nThe Area of Triangular is %.2f",area);
  }
  else
  {
   printf("\nInvalid Value!!");
  }
 }
 else
 {
 printf("\nCheck sides Value!!");
 }
 getch();
 }

Write a C program to find PRIME NUMBER and FACTOR

#include <stdio.h>
#include <conio.h>
void main()
{
 int flag,i,n,r,a[100],b,s;
 clrscr();
 printf("\n Enter Number: ");
 scanf("%d",&n);
 printf(" Facrtors are: 1 %d ",n);
 for(i=2;i<=n/2;i++)
 {
  r=n%i;
  if(r==0)
  {
   flag=s;
   a[b]=i;
   b++;
   printf("%d ",i);
  }
 }
 if(flag==s)
 {
  printf("\n So The Number %d is not prime",n);
 }
 else
 {
  printf("\n So The Number %d is prime",n);
 }
 getch();
}