Custom Search

Saturday 16 April 2011

Program to read two matrices and print addition & subtraction of the matrices through array, Matrices Addition & Subtraction in single program


Program to read two matrices and print addition & subtraction of the matrices through array, Matrices Addition & Subtraction in single program 

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r1,c1,r2,c2,a[10][10], b[10][10], c[10][10];
clrscr();
printf("Enter the rows & column of 1st matrices:\n");
scanf("%d %d",&r1, &c1 );
printf("\nEnter the rows & column of 2nd matrices:\n");
scanf("%d %d",&r2, &c2);
if((r1==r2)&&(c1==c2))
{
printf("\nMatrices addition is possible\n");
}
else
{
printf("\nMatrices addition is not possible\n");
exit(0);
}
printf("\nEnter the first matrices:\n");
for(i=0;i<r1;i++)
{
 for(j=0;j<c1;j++)
 {
 scanf("%d",&a[i][j]);
 }
}
printf("\nFirst Matrices is:\n");
 for(i=0;i<r1;i++)
{
 printf("\n");
 for(j=0;j<c1;j++)
 {
 printf("%d\t",a[i][j]);
 }
}
printf("\n");
printf("\nEnter second Matrices:\n");
for(i=0;i<r2;i++)
{
 for(j=0;j<c2;j++)
 {
 scanf("%d",&b[i][j]);
 }
}
printf("\nSecond Matrices is:\n");
for(i=0;i<r2;i++)
{
 printf("\n");
 for(j=0;j<c2;j++)
 {
 printf("%d\t",b[i][j]);
 }
}
printf("\n");
printf("\nMatrices addition is:\n");
for(i=0;i<r1;i++)
{
 printf("\n");
 for(j=0;j<r2;j++)
 {
 c[i][j] = a[i][j] + b[i][j];
 printf("%d\t",c[i][j]);
 }
}
printf("\n");
printf("\nMatrices subtraction is:\n");
 for(i=0;i<r1;i++)
 {
 printf("\n");
 for(j=0;j<r2;j++)
 {
 c[i][j] = a[i][j] - b[i][j];
 printf("%d\t", c[i][j]);
 }
}
getch();
}

Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.

Click here to check the output

C/ C++ Program to print numbers in increasing order in pyramid form, C program to print 122333444455555.


C/ C++ Program to print numbers in increasing order in pyramid form, C program to print 122333444455555.



Program

#include<stdio.h>
#include<conio.h>
void main()
{ int i,j;
clrscr();
for(i=1;i<6;i++)
{ printf("\n");
for(j=1;j<=i;j++)
{ printf("%d",j);
}}
getch();
}

Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.


Click here to check the output

C/C++ Program to sort the integer array in ascending order, C/C++ Program for Bubble Sorting in Array, Program to enter 10 numbers & arrange them in ascending order.


C/C++ Program to sort the integer array in ascending order,  C/C++ Program for Bubble Sorting in Array, Program to enter 10 numbers & arrange them in ascending order.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10],temp=0;
clrscr();
printf("Enter the 10 Numbers:\n");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<10;i++)
{
 for(j=0;j<9;j++)
 {
 if(a[j] > a[j+1])
 { temp=a[j];
  a[j]=a[j+1];
  a[j+1]=temp;
 } } }
printf("Array in ascending order is:\n");
for(i=0;i<10;i++)
{
 printf("%d ",a[i]);
}
getch();
}


Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.

C /C++ Program for Multiplication of Two Matrices, C /C++ Program to enter elements of integer array & display its Multiplication, C or C++ Program to display numbers with the help of array & show its Multiplication.


C /C++ Program for Multiplication of Two Matrices, C /C++ Program to enter elements of integer array & display its Multiplication, C or C++ Program to display numbers with the help of array & show its Multiplication

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,c1,r2,c2,a[10][10], b[10][10], c[10][10];
clrscr();
printf("Enter the rows & column of 1st matrices:\n");
scanf("%d %d",&r1, &c1 );
printf("\nEnter the rows & column of 2nd matrices:\n");
scanf("%d %d",&r2, &c2);
if(c1==r2)
{
printf("\nMatrices multiplication is possible\n");
}
else
{
printf("\nMatrices multiplication is not possible\n");
exit(0);
}
printf("\nEnter the first matrices\n");
for(i=0;i<r1;i++)
{
 for(j=0;j<c1;j++)
 {
 scanf("%d",&a[i][j]);
 }
}
printf("\nFirst Matrices is:\n");
 for(i=0;i<r1;i++)
{
 printf("\n");
 for(j=0;j<c1;j++)
 {
 printf("%d\t",a[i][j]);
 }
}
printf("\n");
printf("\nEnter second matrices\n");
for(i=0;i<r2;i++)
{
 for(j=0;j<c2;j++)
 {
 scanf("%d",&b[i][j]);
 }
}
printf("\nSecond matrices is:\n");
for(i=0;i<r2;i++)
{
 printf("\n");
 for(j=0;j<c2;j++)
 {
 printf("%d\t",b[i][j]);
 }
}
printf("\n");
printf("\nMatrices multiplication is\n");
for(i=0;i<r1;i++)
{
 printf("\n");
 for(j=0;j<c2;j++)
 {
  c[i][j]=0;
  for(k=0;k<c1;k++)
  {
 c[i][j] = c[i][j]+(a[i][k] * b[k][j]);
  }
 }
}
for(i=0;i<r1;i++)
{
 printf("\n");
 for(j=0;j<c2;j++)
 {
printf("%d\t",c[i][j]);
 }
}
getch();
}


Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.


Click here to check the output

C/C++ Program to sort the integer array in descending order, Program to enter 10 numbers & arrange them in descending order.


C/C++ Program to sort the integer array in descending order,  Program to enter 10 numbers & arrange them in descending order.

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10],temp=0;
clrscr();
printf("Enter the 10 Numbers:\n");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<10;i++)
{
 for(j=0;j<9;j++)
 {
 if(a[j] < a[j+1])
 { temp=a[j];
  a[j]=a[j+1];
  a[j+1]=temp;
 } } }
printf("Array in descending order is:\n");
for(i=0;i<10;i++)
{
 printf("%d ",a[i]);
}
getch();
}


Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.


Click here to check the output

Tuesday 5 April 2011

C /C++ Program to print program to print m characters from string str1 to str2 from back, C /C++ Program to copy m characters from front

C /C++ Program to print program to print m characters from string str1 to str2 from back, C /C++ Program to copy m characters from front


Program
//program to print m characters from string str1 to str2 from front
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char str1[100], str2[100],n;
 int i,j=0,m;
 clrscr();
 printf("Enter the string\n");
 scanf("%s",str1);
 printf("\nAfter how many caracters you want to copy\n");
 scanf("%d",&m);
 for(i=0;i<m;i++)
 {
  str2[j]=str1[i];
  j++;
 }
 str2[j]='\0';
 printf("New string is:%s",str2);
 getch();
}

Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.

Sunday 3 April 2011

C/C++ Program to program to compare two strings without using function strcmp, C/C++ Program to program to find whether first string is greater, smaller or equal to second string


C/C++ Program to program to compare two strings without using function strcmp, C/C++ Program to program to find whether first string is greater, smaller or equal to second string

Program
//program to compare two strings without using function strcmp
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char str1[100], str2[100],m,n,i;
 clrscr();
 printf("Enter First String\n");
 scanf("%s",str1);
 printf("\nEnter Second String\n");
 scanf("%s",str2);
 m=strlen(str1);
 n=strlen(str2);
 if(m>n)
 {
  printf("\nString 1 is greater\n");
 }
 else if(m<n)
 {
  printf("\nstring 2 is greater\n");
 }
 else if(m==n)
 {
 for(i=0;i<m;i++)
 if(str1[i]>str2[i])
 {
 printf("\nString 1 is greater\n");
 break;
 }
 else if(str1[i]<str2[i])
 {
 printf("\nString 2 is greater\n");
 break;
 }
}
 if(i==m)
 printf("\nStrings are Equal\n");
 getch();
}

Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.

Laptops