Tuesday, 20 November 2012

Matrix addition and multiplication program

Program:
#include<stdio.h>
 void main()
 {
 int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
 clrscr();
 printf("************************************");
 printf("\n\t\tMENU");
 printf("\n**********************************");
 printf("\n[1]ADDITION OF TWO MATRICES");
 printf("\n[2]MULTIPLICATION OF TWO MATRICES");
 printf("\n[0]EXIT");
 printf("\n**********************************");
 printf("\n\tEnter your choice:\n");
 scanf("%d",&ch);
 if(ch<=2 & ch>0)
 {
 printf("Valid Choice\n");
 }
 switch(ch)
 {
 case 1:
 printf("Input rows and columns of A & B Matrix:");
 scanf("%d%d",&r1,&c1);
 printf("Enter elements of matrix A:\n");
 for(i=0;i<r1;i++)
 {
 for(j=0;j<c1;j++)
 scanf("%d",&a[i][j]);
 }
 printf("Enter elements of matrix B:\n");
 for(i=0;i<r1;i++)
 {
 for(j=0;j<c1;j++)
 scanf("%d",&b[i][j]);
 }
 printf("\n =====Matrix Addition=====\n");
 for(i=0;i<r1;i++)
 {
 for(j=0;j<c1;j++)
 printf("%5d",a[i][j]+b[i][j]);
 printf("\n");
 }
 break;
 case 2:
 printf("Input rows and columns of A matrix:");
 scanf("%d%d",&m,&n);
 printf("Input rows and columns of B matrix:");
 scanf("%d%d",&p,&q);
 if(n==p)
 {
 printf("matrices can be multiplied\n");
 printf("resultant matrix is %d*%d\n",m,q);
 printf("Input A matrix\n");
 read_matrix(a,m,n);
 printf("Input B matrix\n");
 /*Function call to read the matrix*/
 read_matrix(b,p,q);
 /*Function for Multiplication of two matrices*/
 printf("\n =====Matrix Multiplication=====\n");
 for(i=0;i<m;++i)
 for(j=0;j<q;++j)
 {
 c[i][j]=0;
 for(k=0;k<n;++k)
 c[i][j]=c[i][j]+a[i][k]*b[k][j];
 }
 printf("Resultant of two matrices:\n");
 write_matrix(c,m,q);
 }
 /*end if*/
 else
 {
 printf("Matrices cannot be multiplied.");
 }
 /*end else*/
 break;
 case 0:
 printf("\n Choice Terminated");
 exit();
 break;
 default:
 printf("\n Invalid Choice");
 }
 getch();
 }
 /*Function read matrix*/
 int read_matrix(int a[10][10],int m,int n)
 {
 int i,j;
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 scanf("%d",&a[i][j]);
 return 0;
 }
 /*Function to write the matrix*/
 int write_matrix(int a[10][10],int m,int n)
 {
 int i,j;
 for(i=0;i<m;i++)
 {
 for(j=0;j<n;j++)
 printf("%5d",a[i][j]);
 printf("\n");
 }
 return 0;
 }

Output:
 

 

Multiplication Table using C

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,p,i;
printf("\n enter the noof which u want to see the table");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
p=n*i;
printf("\n%d *%d=%d",n,i,p);
}
getch();
}

Output:


 

How to swap two numbers without using third variable

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

void main()
{
int a,b;
clrscr();
printf("Enter the values of a and b");
scanf("%d%d",&a,&b);
a=b+a;
b=a-b;
a=a-b;
printf("After Swapping a=%d b=%d,a,b);
getch();
}

Output:
Enter the values of a and b
30 40
After Swapping a=40 b=30

Friday, 16 November 2012

How to calculate Tamilnadu Electronic Bill in C Program


Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float r=0.0,a=1.1,b=1.8,c=3.5,d=5.75;
clrscr();
printf("Enter the readings\n");
scanf("%f",&r);
if(r<=100)
printf("Rupees=%f",r*a);
else if((r>100)&&(r<=200))
printf("Rupees=%f",r*b);
else if((r>200)&&(r<=500))
printf("Rupees=%f",r*c);
else if(r>500)
printf("Rupees=%f",r*d);
getch();
}


Tuesday, 23 October 2012

Abstract Class Vs Interface

FeatureInterfaceAbstract class
Multiple inheritanceclass may inherit several  interface .class may inherit only one  abstract class .
Default implementationAn interface cannot provide any code, just the signature.An abstract class  can provide complete, default code and/or just the details that have to be overridden.
Access ModfiersAn  interface  cannot have access modifiers for the subs, functions, properties etc everything is assumed as publicAn  abstract class  can contain access modifiers for the subs, functions, properties
Core VS Peripheralinterfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interfaceAn  abstract class  defines the core identity of a class and there it is used for objects of the same type.
HomogeneityIf various implementations only share method signatures then it is better to use interfaceIf various implementations are of the same kind and use common behaviour or status then abstract class  is better to use.
SpeedRequires more time to find the actual method in the corresponding classes.Fast
Adding functionality (Versioning)If we add a new method to an  interface  then we have to track down all the implementations of the interface  and define implementation for the new method.If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and ConstantsNo fields can be defined in interfacesAn  abstract class can have fields and constrants defined.


Monday, 22 October 2012

Pascal Triangle in c


Program:
#include<stdio.h>
 
long factorial(int);
 
main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");
 
      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
 
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for( c = 1 ; c <= n ; c++ )
         result = result*c;
 
   return ( result );
}

Output:

Monday, 15 October 2012

Program to find odd or even number using C

#include<stdio.h>
void main ()
{
int number;
clrscr ();
printf ("Enter the value of number: ");
scanf("%d",&number);
if (number%2==0)
{
printf ("\nNumber is Even");
}
else
{
printf("\nNumber is Odd");
}
getch ();
}

Output:








Related Posts Plugin for WordPress, Blogger...