Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Monday, February 16, 2015

Java Program for Addition of two Matrices

import java.util.Scanner; //import Scanner class in our program

class demo
{
public static void main(String...s)
{
int i,j,n,m;
Scanner sc=new Scanner(System.in); //used to read from keyboard

System.out.print("Enter number of rows:");
m=sc.nextInt();
System.out.print("Enter number of columns:");
n=sc.nextInt();

int a1[][]=new int[m][n];
int a2[][]=new int[m][n];
int a3[][]=new int[m][n];

System.out.print("
Enter elements of first matrix:
");

for(i=0;i<m;++i)
for(j=0;j<n;++j)
a1[i][j]=sc.nextInt();

System.out.print("
Enter elements of second matrix:
");

for(i=0;i<m;++i)
for(j=0;j<n;++j)
a2[i][j]=sc.nextInt();

System.out.print("
Addition of Matrices:
");


for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
a3[i][j]=a1[i][j]+a2[i][j];
System.out.print(a3[i][j]+" ");
}
System.out.print("
");

}


}
}

Java Program for Addition of two Matrices

C program to swap two numbers without using temp variable

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b;
cout<<"Enter value of a:";
cin>>a;
cout<<"Enter value of b:";
cin>>b;

a=a+b;
b=a-b;
a=a-b;

cout<<"
a="<<a;

cout<<"
b="<<b;

getch();
}

Thursday, February 12, 2015

C program to calculate roots of quadratic equation ax 2 bx c 0


C program to calculate roots of quadratic equation ax^2+bx+c=0

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


void main()
{
float root1,root2,a,b,c,d;
clrscr();
printf("Quadratic Equation is ax^2=bx+c=0");
printf("
Enter values of a,b and c:");

  scanf("%f%f%f",&a,&b,&c);


d=(b*b)-(4*a*c);
if(d>0)
{
printf("
Two real and distinct roots");

root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("
Roots are %f and %f",root1,root2);

}


else
if(d==0)
{
printf("
Two real and equal roots");

root1=root2=-b/(2*a);
printf("
Roots are %f and %f",root1,root2);

}
else
printf("
Roots are COMPLEX and IMAGINARY....!!!");

getch();
}

Wednesday, February 11, 2015

C program to print a message on the screen

C program to print a message on the screen

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

void main()
{
clrscr(); //to clear the screen
printf("



***** Welcome to C Programming *****");

getch(); //to stop the screen
}

C Program to print given series 1 2 4 8 16 32 64 128


#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int i;

for(i=1;i<=128;i*=2)
cout<<i<<" ";
getch();
}

Tuesday, February 10, 2015

Java program to find sum of digits of a number

Java program to find sum of digits of a number

class sum
{
public static void main(String...s)
{
int n,i,sum=0;
n=Integer.parseInt(s[0]);

while(n!=0)
{
i=n%10;
sum+=i;
n/=10;
}

System.out.println(sum);
}
}