Pages

Showing posts with label computer programs. Show all posts
Showing posts with label computer programs. Show all posts

Thursday, November 19, 2015

C++ Program to Find Largest and Smallest Number in Array


 
The c++  program below generates a random number and stores it in an array, then  passes an array to a function which calculates the maximum and minimum number in the array and displays it.

Also Read: C++ Program to Find All The Roots Of A Quadratic Equation

How the program works:

  • Declare array of size 10
  • Using for loop assign array indexes with random values between 1 and thousand
  • Call the function and pass array and its size as argument
  • Function declares two integers max and min and assign both integers with arrays first index value
  • Then with in for loop there are two if condition first check is for minimum number and second check is for maximum number
  • Finally program display the output values of both integers min and max

Also Read: C++  Program For Binary Equivalent Of a Decimal Number

#include <iostream.h>
#include <stdlib.h>

void FindMaxMin(int *array, int size)
{
int min,max;
min=max=array[0];
for(int i=1;i<size;i++)
{
if(array[i]<min)
min=array[i];
else if(array[i]>max)
max=array[i];
}
cout<<"Minimum Number = "<<min<<endl;
cout<<"Maximum Number = "<<max<<endl;
}
int main()
{
int array[10];
for(int i=0;i<=9;i++)
{
array[i]=rand()%1000+1;
cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
}
FindMaxMin(array,10);
return 0;
}
 
Also Read: C++ Program to Check If a Number Is A Perfect Number or Not 
 
Source



Read more »

Sunday, November 15, 2015

Pascal Arithmetic Program Using IF THEN statement

Pascal program using control statements. Each section performs a different basic mathematical arithmetic.

YOU MIGHT ALSO LIKE: Pascal Program To Create And Save Records In A Textfile
Program Basic_Arithmetic; 
Uses crt;
Label 20;
Var A, B, Sum, Subtract, Divide, Multiply, Average: Real;
Choice : Integer;
YN : Char;


Begin
20 : Clrscr;
Writeln (' For addition, press 1');
Writeln (' For subtraction, press 2');
Writeln (' For Divison, press 3');
Writeln (' For multiplication, press 4');
Writeln ('To calculate Average, press 5');
Writeln ('To exit, Press 6');
Writeln ('Enter a number of your choice');
Choice := Readkey;


If choice = '1' then
Begin
Writeln (' Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Sum := A + B;
Writeln (' The answer is', Sum);
Goto 20;
End;


If choice = '2' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Subtract := A - B ;
Writeln ('The answer is', Subtract);
Readln;
Goto 20;
End;


If choice = '3' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Divide := A / B;
Writeln ('The answer is', divide);
Readln;
Goto 20;
End;


If choice = '4' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Multiply := A * B
Writeln ('The answer is', multiply);
Readln;
Goto 20;
End;

If choice = '5' then
Writeln ('Enter your first number');
Readln (A);
Writeln ('Enter your second number');
Readln (B);
Sum := A + B;
Average := Sum / 2 ;
Writeln ('The answer is:', Average);
Readln;
Goto 20;
End;


If choice = '6' then
Writeln (' You are about exiting this program, to continue press Y/N');
YN := Readkey;
If YN = 'Y' then
Writeln (' Goodbye');
Halt;
If YN = 'N' then
goto 20;
end;
End.



If you have more numbers you can use a looping statement which will save time instead of the If then statement.
Read more »

Wednesday, November 11, 2015

Find The Perfect Number In C++ Program

C++ program to find if a number is a perfect number or not. A Perfect number is one which the sum of all its divisors equals to the number.
For example 28 a Perfect Number since the sum of its divisors which are 1+ 2 + 4 + 7 + 14 = 28

Also Read: C++ Program To Find Roots Of a Quadratic Equation

#include<iostream.h>
#include<math.h>
int main()
{
int i,n,sum=0;
cout<<"enter a number";
cin>>n;
for (i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
cout<<i<<"is a perfect number";
else
cout<<i<<"is not a perfect number";
return 0;
}

Also Read: C++ Program To Find Prime Numbers
Read more »

C++ Program to Find All The Roots Of A Quadratic Equation


A quadratic equation has the form ax^2+bx+c=0 where x is an unknown, and a, b, c represent values which in this c++ program will be supplied by the user.



Also Read: C++ Program To compute least common multiple

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


void main()
{
float a,b,c,d,x1,x2;

cout<<"Enter the value of a: ";
cin>>a;
cout<<"Enter the value of b: ";
cin>>b;
cout<<"Enter value of c: ";
cin>>c;

d=b*b-4*a*c;

if(d==0)
{
x1=(-b)/(2*a);
x2=root1;
cout<<"The Roots are real & equal";
}
else if(d>0)
{
x1=-(b+sqrt(d))/(2*a);
x2=-(b-sqrt(d))/(2*a);
cout<<"the Roots are real & distinct";
}
else
{
x1=(-b)/(2*a);
x2=sqrt(-d)/(2*a);
cout<<"Roots are imaginary";
}

cout<<x1<<" "<<x2;
}
Read more »

C++ Program to Compute The Least Common Multiple of Integers

C++ program to compute the LCM of two numbers. Least Common Multiple can be gotten by dividing the numbers by their common factors.Using the logic below, we can also find write a program to calculate the LCM for more numbers.

//C++ program that computes the least common multiple of two integers. These are represented as a and b below.

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,i,j; //Variable declarations
clrscr;
cout<<"enter a number: ";
cin>>a;
cout<<"enter a number: ";
cin>>b;
i=a ;
j=b;
if(a==0||b==0) //Checks if either a OR b is 0
cout<<" The LCM is 0"; //If 0 from the condition above, a display message is output to the screen
else
{
while(a!=b)//While the value of a is not equal to b execute the statements below
{
if(a>b)
b=b+j;
else
if(a<b)
a=a+i;
}
cout<<"LCM:"<<" "<<a;
}
}
Read more »

C++ Program to Compare The Greatest Of Three Integers

C++ program to compare which is the greatest among three numbers. An IF statement is used to set the condition to know which is less or greater than which.

#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"enter the first value";
cin>>a;
cout<<"enter the second value";
cin>>b;
cout<<"enter the third value";
cin>>c;
if (a>b&&b>c)
cout<<"The greatest is: "<<a;
else
if(a<b&&b>c)
cout<<"The greatest is: "<<b;
else
if(a<b&&b<c)
cout<<"The greatest is: "<<c;
else
if(a==b&&b==c)
cout<<"Non is greater!!";
}
Read more »

Sunday, November 1, 2015

C++ Program to Check If a Character is Small, Capital, Number or Special Character

The C++ Program checks if the value entered by a user is a capital, small, number or a special character. The output also returns the Ascii equivalent of the character. IF statement is used for flow of control in the program.

Also Read: C++ Program to Check If a Number Is A Perfect Number or NOt

C++ Program to Find All The Roots Of A Quadratic Equation
C++ Program to Compute The Least Common Multiple of Integers


#include <iostream.h>
int main()
{
char alphabet;

cout<<"enter an character: ";
cin>>alphabet;
int Ascii=alphabet;
if (Ascii>=97 && Ascii<=122)
{
cout<<"the Ascii equivalent of "<<alphabet<<" is: "<<Ascii<<endl;
cout<<"alpabet is a small letter";
}
else if (Ascii>=65 && Ascii<=90)
{
cout<<"the Ascii equivalent of "<<alphabet<<" is:" <<Ascii<<endl;
cout<<"The alphabet is a capital letter";

else if (Ascii>=47 && Ascii<=57)
{
cout<<"the Ascii equivalent of "<<alphabet<<"is: "<<Ascii<<endl;
cout<<"You entered a number";
}
else if (Ascii>=0 && Ascii>=47 || Ascii>=54 && Ascii<=64 || Ascii>=91 && Ascii<=96 || Ascii >=123 && Ascii<=127)
{
cout<<"the Ascii equivalent of "<<alphabet<<"is:"<<Ascii<<endl;
cout<<"you entered a special character";
}
return 0;
}
Read more »