#include <stdio.h>
int main()
{
double firstNumber, secondNumber, temporaryVariable;
printf("Enter first number: ");
scanf("%lf", &firstNumber);
printf("Enter second number: ");
scanf("%lf",&secondNumber);
temporaryVariable = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryVariable;
printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
printf("After swapping, secondNumber = %.2lf", secondNumber);
return 0;
}
COMPUTER PROGRAMMING
Wednesday, 7 September 2016
Program to Swap Numbers Using Temporary Variable
Wednesday, 1 October 2014
TRANSPOSE MATRIX
#include<stdio.h>
void main()
{
int a[50][50];
int r,c,i,j;
printf("\n Enter the Limit of Row & Coloumn : ");
scanf("%d%d",&r,&c);
printf("\n Enter the Matrix Elements : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The Given Matrix......\n\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n The Transpose of the Matrix......\n\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
}
SYMMETRIC MATRIX
#include<stdio.h>
void main()
{
int a[50][50];
int r,c,i,j,flag=1;
printf("\n Enter the Limit of Row & Coloumn : ");
scanf("%d%d",&r,&c);
printf("\n Enter the Matrix Elements : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The Given Matrix......\n\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
Friday, 6 September 2013
BINARY SEARCH
#include<stdio.h>
void main()
{
int a[100];
int i,j,n,temp,s,top,bottom,mid;
printf("\n Enter the Limit : ");
scanf("%d",&n);
printf("\n Enter the %d Elemets......\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the Searching Element : ");
scanf("%d",&s);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
if(n%2==0)
{
top=n+1;
bottom=0;
}
else
{
top=n;
bottom=0;
}
do
{
mid=(bottom+top);
if(s<a[mid])
{
top=mid-1;
}
else
{
bottom=mid+1;
}
}while((s!=a[mid])&&(top>=bottom));
if(s==a[mid])
printf("\n Se Searching element is found \n");
else
printf("\n Searching Element is Not found \n");
}
Thursday, 5 September 2013
TEMPERATURE CONVERSION
import java.awt.*;
import java.awt.event.*;
import jpb.*;
// Driver class
public class ConvertTemp {
public static void main(String[] args) {
Frame frame =
new ConvertTempFrame("Temperature Conversion");
frame.setSize(150, 75);
frame.setVisible(true);
}
}
// Frame class
class ConvertTempFrame extends Frame {
private TextField fahrenField = new TextField();
private TextField celsiusField = new TextField();
// Constructor
public ConvertTempFrame(String title) {
// Set title for frame and choose layout
super(title);
setLayout(new GridLayout(2, 2));
// Add Fahrenheit label and text field to frame; attach
// listener to text field
add(new Label("Fahrenheit"));
add(fahrenField);
fahrenField.addActionListener(new FahrenheitListener());
// Add Celsius label and text field to frame; attach
// listener to text field
add(new Label("Celsius"));
add(celsiusField);
celsiusField.addActionListener(new CelsiusListener());
// Attach window listener
addWindowListener(new WindowCloser());
}
// Listener for fahrenField
class FahrenheitListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String fahrenheitString = fahrenField.getText();
double fahrenheit = Convert.toDouble(fahrenheitString);
double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
celsius = Math.rint(celsius * 100.0) / 100.0;
celsiusField.setText(celsius + "");
}
}
// Listener for celsiusField
class CelsiusListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String celsiusString = celsiusField.getText();
double celsius = Convert.toDouble(celsiusString);
double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
fahrenheit = Math.rint(fahrenheit * 100.0) / 100.0;
fahrenField.setText(fahrenheit + "");
}
}
// Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
AVERAGE OF A SERIES
// Computes the average of a series of scores
import jpb.*;
public class AverageScores {
public static void main(String[] args) {
// Prompt user to enter number of scores
SimpleIO.prompt("Enter number of scores: ");
String userInput = SimpleIO.readLine().trim();
int numberOfScores = Integer.parseInt(userInput);
System.out.println();
// Create array to hold scores
int[] scores = new int[numberOfScores];
// Prompt user to enter scores and store them in an array
int i = 0;
while (i < scores.length) {
SimpleIO.prompt("Enter score #" + (i + 1) + ": ");
userInput = SimpleIO.readLine().trim();
scores[i] = Integer.parseInt(userInput);
i++;
}
// Compute sum of scores
int sum = 0;
i = 0;
while (i < scores.length) {
sum += scores[i];
i++;
}
// Display average score
System.out.println("\nAverage score: " +
sum / scores.length);
}
}
Thursday, 25 July 2013
SEQUENTIAL SEARCH
#include<stdio.h>
void main()
{
int a[100];
int i,n,num,flag=0;
printf("\n Enter the Limit : ");
scanf("%d",&n);
printf("\n Enter the %d Elements.......\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the Number which you want to search : ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
flag=1;
break;
}
}
if(flag==0)
printf("\n The searching Element %d is not found in the Array \n",num);
else
printf("\nThe Searching Element is in %d th position ",i+1);
}
Subscribe to:
Posts (Atom)