Anda di halaman 1dari 34

# Array sorting

#include <iostream.h>
#define maxsize 100
int main()
{
int temp, i, j, n, a[maxsize];
cin>>n;
for(i=0; i<n; i++)
{
cout<<"Enter a's element #"<<i<<"-->";
cin>>a[i];
}
// do the sorting...
for(i=0; i<n-1; i++)
for(j=i+1; j<n; j++)
if(a[i] > a[j])
{
// these three lines swap the elements list[i] and
list[j].
temp = a[i];
a[i] = a[j];
a[j] = temp;
}

## cout<<"\nSorted list, ascending: ";

for(i=0; i<n; i++)
cout<<" "<<a[i];
cout<<endl;
return 0;
}
ARROW PATTERN
#include<iostream.h>
int main()
{
int num, out = 0;
cout << "Give a number: ";
cin >> num;
for (int i = 1; i <= 2*num-1; i++)
{
cout << 1 << " ";
for (int j = 2; j <= i && ((j <= num && i <= num)
|| (i > num && j <= 2*num-i)); j++)
{
out = j;
cout << out;

## cout << " ";

}
cout << endl;
}
return 0;
}
CHECK PRIME NUMBER
#include<stdio.h>
#include<conio.h>
int prime();
void main()
{
clrscr();
int a;
a=prime();
if(a==0)
printf("prime number");
else
printf("not prime");
getch();
}

int prime()
{
int b,c,d=0;
printf("enter the number");
scanf("%d",&b);
for(c=2;c<=b/2,b++)
{
if(b%c==0)
d++;
}
if(b==0)
e=0;
else
e=1;
return(e);
}}
DEC TO BINARY
#include<iostream.h>
#include <stdio.h>
//using namespace std;
int main()
{
int n,x,a, c, k;

## cout<<"Enter an integer in decimal number

system";
cin>>x;
n=x;
cout<<"Binary Value OF Given Number Is: ";
for( a=1;n!=0;a++)
{
n=n/2;
}
a=a-2;
for (c = a; c >= 0; c--)
{
k = x >> c;
if (k & 1)
cout<<"1";
else
cout<<"0";
}

return 0;
}
EVEN ODD IN ARRAY
#include<stdio.h>
#include<conio.h>
#define size 15
void main()
{
clrscr();
int e[size],o[size],j=0,k=0;
for(int i=1;i<=30;i++)
{
if(i%2==0)
{
e[j]=i;
j++;
}
else
{
o[k]=i;

k++;
}
}
printf("\nThe Even numbers from 1 TO 30 are\n");
for(j=0;j<size;j++)
printf("\n%d",e[j]);
printf("\nThe ODD numbers from 1 TO 30 are\n");
for(k=0;k<size;k++)
printf("\n%d",o[k]);
getch();
}
FINDING PROPER FACTORS OF A
NUMBER
* factor1.c -- It prompts the user to enter an integer
N. It prints out
*
if it is a prime or not. If not, it prints out all
of its
*
proper factors.
*/
#include <stdio.h>
int main(void) {
int n,

lcv,
flag; /* flag initially is 1 and becomes 0 if we
determine that n
is not a prime */
printf("Enter value of N > ");
scanf("%d", &n);
for (lcv=2, flag=1; lcv <= (n / 2); lcv++) {
if ((n % lcv) == 0) {
if (flag)
printf("The non-trivial factors of %d are: \n",
n);
flag = 0;
printf("\t%d\n", lcv);
}
}
if (flag)
printf("%d is prime\n", n);
}
GETTING HIGH NUMBER AND
DIFFERENCE IN ARRAY
#include<iostream.h>
void main()
{

int i,score[5],max;
cout<<"enter 5 score"<<endl;
cin>>score[0];
max=score[0];
for(i=1;i<5;i++)
{
cin>>score[i];
if(score[i]>max)
max=score[i];
}
cout<<"the higest score is "<<max<<endl;
cout<<"the score and their diff from max
is"<<endl;
for(i=0;i<5;i++)
cout<<score[i]<<" diff "<<(max-score[i])<<endl;
}
GETTING HIGH NUMBER FROM ARRAY
include<iostream.h>
void main()
{
int i,score[5],max;
cout<<"enter 5 score"<<endl;
cin>>score[0];
max=score[0];

for(i=1;i<5;i++)
{
cin>>score[i];
if(score[i]>max)
max=score[i];
}
cout<<"the higest score is "<<max<<endl;
}
HOLLOW TRIANGLE
#include<iostream.h>
int main()
{
cout<<"\"Hollow Triangle Shape\"\n\n";
int z=1;
for (int i=0; i<7; i++)
{
for (int j=7; j>i; j--)
{
cout<<" "; // displaying space here
}
cout<<"*"; // displaying asterisk here
if (i!=0)

{
for (int k=1; k<=z; k++)
{
cout<<" ";
}
cout<<"*";
z+=2;
}
cout<<endl; // endl is for new line
}
for (i=0; i<=z+1; i++)
{
cout<<"*";
}
return 0;
}
MEAN MODE MEDIAN
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{

float data[100],sum;
float mean;
int i,size;
clrscr();
printf("please enter the size of array");
scanf("%d",&size);
for(i=0;i<=size-1;i++)
{
printf("\nenter value in element %d",i);
scanf("\t%f",&data[i]);
}
sum=0.0;
for(i=0;i<size;i++)
sum=sum+data[i];
mean=sum/size;
printf("\nthe sum is %f",sum);
printf("\nthe mean is %f",mean);
float x_bar,xi_x_bar,squar,xi,total_sum_xis=0.0;
float varience,stand_dev;
x_bar=sum;
printf("\nXi\t\tX_Bar\t\tXi-X_Bar\t\t(Xi-X_Bar)");
for(i=0;i<size;i++)
{
xi=data[i];

xi_x_bar=xi-sum;
squar=xi_x_bar*xi_x_bar;
printf("\n%f\t%f\t%f\t\t
%f",xi,x_bar,xi_x_bar,squar);
total_sum_xis=total_sum_xis+squar;
}
varience=total_sum_xis/size;
stand_dev=sqrt(varience);
printf("\nthe vairence is=%f",varience);
printf("\nthe standard deviation is=
%f",stand_dev);
getch();
}
PRIME NUMBER CHECK
#include<stdio.h>
#include<conio.h>
int prime();
void main()
{
clrscr();
int a;
a=prime();
if(a==0)

printf("prime number");
else
printf("not prime");
getch();
}
int prime()
{
int b,c,d=0;
printf("enter the number");
scanf("%d",&b);
for(c=2;c<=b/2,b++)
{
if(b%c==0)
d++;
}
if(b==0)
e=0;
else
e=1;
return(e);
}}

## To chech the input is char or not using if-else.

#include <iostream.h>
void main()
{
char c;
cout<<"Enter a character: ";
cin>>c;
if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
cout<<c<<" is an alphabet.";
else
cout<<c<<" is not an alphabet.";
}

## To chech the year is leap or not using if-else.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int year;
cout<<"enter year";
cin>>year;
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
cout<<"year is leap";
else
cout<<"year is not leap";
}
else
cout<<"year is leap";
}
else
cout<<"year is not leap";
getch();
}

## To find the roots of quadratic equation using ifelse.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,d,r1,r2,R,I;
cout<<"enter a,b,c"<<endl;
cin>>a;
cin>>b;
cin>>c;
d=b*b-4*a*c;
if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
cout<<"roots are"<<r1<<r2;
}
else
if(d==0)
{
r1=r2=-b/(2*a);
cout<<"roots are"<<r1<<r2;

}
else
{
R=-b/(2*a);
I=sqrt(-d)/(2*a);
cout<<"roots are"<<R<<I;
}
getch();
}

## make a calculater using if-else.

first take one integer type value from user then
operator and then 2nd value.
values and operator must be user defined.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char o;
float number1,number2,result;

## cout<<"enter two number and operator ";

cin>>number1;
cin>>o;
cin>>number2;
if(o=='*')
{
result=number1*number2;
cout<<result;
}
else
if(o=='/')
{
result=number1/number2;
cout<<result;
}
else
if(o=='+')
{
result=number1+number2;
cout<<result;
}
else
if(o=='-')
{

result=number1-number2;
cout<<result;
}
getch();
}

## Greatest number among three number,,

numbers must be user defined.
#include<iostream.h>
int main()
{
int num1,num2,num3;
cout<<" Enter value for first number";
cin>>num1;
cout<<" Enter value for second number";
cin>>num2;
cout<<" Enter value for third number";
cin>>num3;
if(num1>num2&&num1>num3)

{
cout<<" First number is
greatest:"<<endl<<"whick is= "<<num1;
}
else if(num2>num1&&num2>num3)
{
cout<<" Second number is
greatest"<<endl<<"whick is= "<<num2;
}
else
{
cout<<" Third number is
greatest"<<endl<<"whick is= "<<num3;
}
return 0;
}

## A Program to check number is positive and how

many digits number have
number must be user defined.

#include<iostream.h>
int main()
{
int num;
cout<<" Enter Number btween 1 to 999 ";
cin>>num;
if(num>0)
{
cout<<num<<" is a Positive Number "<<endl;
if(num<10)
{
cout<<num<<" is a ONE digit Number ";
}
else if(num<100)
{
cout<<num<<" is a TWO digit Number ";
}
else if(num<1000)
{
cout<<num<<" is a Three digit Number ";
}
}

return 0;
}

REVERSE NUMBERS
#include<iostream>
using namespace std;
int main() {
int number, reverse = 0;
cout<<"Input a Number to Reverse and press
Enter: ";
cin>> number; // Taking Input Number in
variable number
for( ; number!= 0 ; )
{
reverse = reverse * 10;
reverse = reverse + number%10;
number = number/10;
}
cout<<"New Reversed Number is: "<<reverse;
return 0;

}
RIGHT ANGLE TRIANGLE
#include<iostream.h>
int main()
{
int rows, out = 0, temp;
cout << "Give a number: ";
cin >> rows;
for (int i = 1; i <= rows; i++){
temp = i;
cout << i << " ";
for (int j = 1; j < i; j++)
{
out = temp + rows - j;
cout << out;
cout << " ";
temp = out;
}
cout << endl;
}

return 0;
}
SEARCHING IN ARRAY
#include <iostream.h>
int main ()
{
int t[5], i, V;
for (i = 0; i < 5; i++)
{
cout << "Type an integer: ";
cin >> t[i];
}
cout << "Type the value of V: ";
cin >> V;
for (i = 0; i < 5; i++)
{
if (t[i] == V)
{
cout << "V is in the array" << endl;
return 0;
}
}

## cout << "V is not in the array" << endl;

}
SWAP TWO INT
#include<iostream.h>
int main()
{
int var1, var2, swap;
cout<<"Enter value for first integer: ";
cin>>var1;
cout<<"Enter value for second integer: ";
cin>>var2;
cout<<" Values Before swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;

## cout<<"Second Interger ="<<var2<<endl;

swap=var1;
var1=var2;
var2=swap;
cout<<" Values After swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
return 0;
}

## SWAP TWO VARIABLES WITHOUT THIRD

VARIABLE
#include<iostream.h>

int main()
{
int var1, var2;
cout<<"Enter value for first integer: ";
cin>>var1;
cout<<"Enter value for second integer: ";
cin>>var2;
cout<<" Values Before swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
var1=var1+var2;
var2=var1-var2;

var1=var1-var2;
cout<<" Values After swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
return 0;
}

TEMPERATURE CONVERSION
#include<stdio.h>
#include<conio.h>
void main()
{ //start of main function
int ch=0,cc;
do
{ //start of do-while
float c,f,k;
clrscr();

printf("\t\t\t*****TEMPRATURE CONVERSION
SYSTEM*****");
printf("\n\t\t\t==========================
============");
printf("\n\n1. Celsius Conversion");
printf("\n\n2. Fahrenheit Conversion");
printf("\n\n3. Kelvin Conversion");
scanf("%d",&ch);
switch(ch)
{ //start of switch function
case 1:
{
printf("\n\nEnter Temprature In Celsius: ");
scanf("%f",&c);
c=c;
f=c*33.8;
k=c*274.15;
}
break;
case 2:
{

## printf("\n\nEnter Temprature In Fahrenheit: ");

scanf("%f",&f);
c=f*(-17.22222);
f=f;
k=f*255.92778;
}
break;
case 3:
{
printf("\n\nEnter Temprature In Kelvin: ");
scanf("%f",&k);
c=k*(-272.15);
f=k*(-457.87);
k=k;
}
break;
default:
{
printf("\n\nYou Enter A Wrong Value, You Enter
%d",ch);
}

## } //end of swith function

printf("\n\nTemprature In Celsius %f",c);
printf("\n\nTemprature In Fahrenheit %f",f);
printf("\n\nTemprature In Kelvin %f",k);
printf("\n\nRepeat ? (1.Yes/2.No)");
scanf("%d",&cc);
} //end of do-while
while(cc==1);
} //end of main function

## ENTER ARRAY ELEMENTS FROM USER

#include <iostream.h>
int main()
{
int t[5],i;
for(i=0;i<5;i++)
{
cout << "Type an integer: ";
cin >> t[i];
}

for(i=0;i<5;i++)
cout << "the elements entered are "<<t[i]
<<endl;
return 0;
}

## TO GET ARRAY ELEMENTS

#include<iostream.h>
void main()
{
int a[5]={1,2,3,4,5};
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
}
TRIANGLE
#include<iostream.h>
int main()
{

int e=1;
cout<<"\" Triangle Shape SHAPE \":\n\n";
for(int a=1;a<=5;a++)
{
for(int b=4;b>=a;b--)
{
cout<<" "; // Printing Space Here
}
for(int c=0;c<e;c++)
{
cout<<"*"; // Printing asterisk here
}
cout<<endl; // new line
e=e+2;
}
}