You are on page 1of 49

Computer Science File

Udbhav Gupta S6G

INDEX
Acknowledgements

Programs:
1. Addition of Two Integers
2. Count Till
3. Average Marks of 5 Students
3. Area of Circle
4. Difference of Two Integers
5. All Even Numbers from 2 to 40
6. Roots of a Quadratic equation
7. All Odd Numbers less than 50
8. Highest Common Factor of Two Integers
9. Lowest Common Multiple of Two Integers
10. Even Number or Odd Number
11. Prime Number
12. Multiples of any Natural Number up to 100
13. Factors of a Number
14. Multiplication Table of a Number
15. Number Pyramid
16. Star Pyramid
17. Check Whether a Triangle is Equilateral,
Isosceles or Scalene
18. Addition, Subtraction, Multiplication, Division of

Two integers (Switch Case)


19. Reverse Order
20. Sum and Average of 10 Integers
21. Largest and Smallest Number in an Array
22. Even Numbers in an Array
23. Fibonaccis Sequence
24. Ascending Order (Linear Sorting)
26. Palindrome
27. Factorial of a Number
28. Length of a String
29. Descending Order (Bubble Sorting)
30. Number of Spaces and Words
31. Display Initials of the first two words in a string

ACKNOWLEDGMENT

I would like to express my deep gratitude to Gautam Sarkar Sir


for his expert advice and brilliance in the subject. Because of
your immense knowledge of the subject the concepts are easily
understandable and the classes are always fun.

I would also like to thank Yogita maam for her patient


guidance. The completion of this file could not have been
accomplished without your valuable support.

1. Write a Program for addition of Two Integers


#include<iostream.h>
void main()
{
int a, b, sum;

//iostream.h is for cin and cout

cout<<"::: Addition of Two Integers :::"<<endl;


cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
sum=a+b;
cout<<"Answer: "<<a<<"+"<<b<<"="<<sum;
}
Output:

2. Write a Program to Find out the Marks and


Average of 5 Students
#include<iostream.h>
void main()
{
float m1, m2, m3, m4, m5, avg;

//float is used for decimals

cout<<"Marks of First Student: ";


cin>>m1;
cout<<endl;
cout<<"Marks of Second Student: ";
cin>>m2;
cout<<endl;
cout<<"Marks of Third Student: ";
cin>>m3;
cout<<endl;
cout<<"Marks of Fourth Student: ";
cin>>m4;
cout<<endl;
cout<<"Marks of Fifth Student: ";
cin>>m5;
cout<<endl;
avg=(m1+m2+m3+m4+m5)/5;
cout<<"Average Marks of Five Students: "<<avg;
}

Output:

3. Write a Program to Calculate the Area of a


Circle
#include<iostream.h>
void main()
{
float area, radius;
cout<<":::Calculate Area of Circle:::"<<endl;
cout<<"Enter Radius of Circle(in metres): ";
cin>>radius;
area = 3.14285*radius*radius;

cout<<"Area of Circle = "<<area<< sq. metres;

Output:

4. Write a Program to Check Whether the Difference


of 2 integers is Greater Than or Less Than 0
#include<iostream.h>
void main()
{
int a, b, diff;
cout<<":::Subtraction of Two Integers:::"<<endl;
cout<<endl;
cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
diff=a-b;
cout<<"Answer: "<<a<<"-"<<b<<"= "<<diff<<endl;
if(diff>0)
cout<<"Greater Than 0";
else if(diff==0)
cout<<"Equal to Zero";
else
}

cout<<"Less Than 0";

Output:

5. Write a Program to Print all Even Numbers from 2


to 40
#include<iostream.h>
void main()
{
int i;
i=2;
cout<<"***Series of Even Numbers upto 40***"<<endl;
cout<<endl;
while(i<=40)

cout<<i;
i=i+2;

cout<<endl;

Output:

10

6. Write a Program to Find The Real Roots of a


Quadratic Equation
#include<iostream.h>
#include<math.h>
void main()
{
float a, b, c, D, r1, r2;
cout<<":::Find The Roots of a Quadratic Equation:::"<<endl;
cout<<endl;
cout<<"Enter the coefficient of x^2: ";
cin>>a;
cout<<"Enter the coefficient of x: ";
cin>>b;
cout<<"Enter constant value: ";
cin>>c;
cout<<endl;
D = pow(b,2)-4*a*c ;

//'pow' is used for power, header file: math.h

if(D<0)
cout<<"No real roots"<<endl;
else if(D==0)
{
cout<<"Equal and real roots"<<endl;
r1= -b/(2*a);
cout<<"Roots of Given Equation: "<<r1<<endl;
}
else
{

cout<<"Real and Distinct Roots"<<endl;


r1= (-b+sqrt(D))/2*a;
r2= (-b-sqrt(D))/2*a;
cout<<"Roots of Given Equation: "<<r1<<", "<<r2;

11

Output:

12

7. Write a Program to Display all Odd Numbers Less


Than 50
#include<iostream.h>
void main()
{
int odd;
odd=1;

while(odd<50)
{
cout<<odd<<endl;
odd=odd+2;
}

Output:

13

8. Write a Program to Find the HCF of Two Integers


#include<iostream.h>
void main()
{
int no1, no2, M, N, HCF;
cout<<"Find the Highest Common Factor of any Two Real Numbers!"<<endl;
cout<<endl;
cout<<"Enter First Number: ";
cin>>no1;
cout<<"Enter Second Number: ";
cin>>no2;
if(no1>no2)
M=no2;
else
M=no1;
N=1;
while(N<=M)
{
if(no1%N==0 && no2%N==0)
HCF=N;
N++;

}
cout<<"HCF of "<<no1<<" and "<<no2<<" is: "<<HCF;

14

Output:

15

9. Write a Program to Find the LCM of Two Integers


#include<iostream.h>
void main()
{
int no1, no2, M, LCM;
cout<<":::Find the Lowest Common Multiple of any Two Integers:::"<<endl;
cout<<endl;
cout<<"Enter First Number: ";
cin>>no1;
cout<<"Enter Second Number: ";
cin>>no2;
M=no1*no2;

while(M>=no1 && M>=no2)


{
if(M%no1==0 && M%no2==0)
{
LCM=M;
}
M--;
//Because the final value of M should be lowest
}
cout<<"LCM of "<<no1<<" and "<<no2<<" is: "<<LCM;

16

Output:

17

10. Write a Program to Find if a Number is Even


or Odd
#include<iostream.h>
void main()
{
int no;
cout<<":::Find if a Number is Even or Odd:::"<<endl;
cout<<endl;
cout<<"Enter Number: ";
cin>>no;
cout<<endl;
if(no%2==0)
cout<<no<<" is an Even Number"<<endl;
else

cout<<no<<" is an Odd Number";

}
Output:

18

11. Write a Program to Check Whether a Given


Natural Number is Prime or Not
#include<iostream.h>
void main()
{
int x, prime, A;
cout<<":::Find if a Number is Prime or Not:::"<<endl;
cout<<endl;
cout<<"Enter Number: ";
cin>>x;
A=2;
prime=0;
while(A>=2 && A<x && prime==0)
{
if(x%A==0)
//A will go till one less the value of x
prime=1;
else
prime=0;
A++;

}
if (prime==1)
cout<<x<<" is not a Prime Number.";
else
cout<<x<<" is a Prime Number.";

19

Output:

20

12. Write a Program to print the Multiples of any


Natural Number upto 100
#include<iostream.h>
void main()
{
int x, y;
cout<<"Multiples of any Real Number upto 100"<<endl;
cout<<"Enter Number: ";
cin>>x;
cout<<endl;
y=x*1;
while(y>=x && y<=100)
{
if(y%x==0)
cout<<y<<endl;

y++;

Output:

21

13. Write a Program to Find the Factors of a Number


#include<iostream.h>
void main()
{
int x, y;
cout<<"Find the Factors of any Natural Number!"<<endl;
cout<<endl;
cout<<"Enter Number: ";
cin>>x;
y=x;
while(y<=x && y>=1)
{
if(x%y==0)
cout<<y<<endl;

y--;

}
Output:

22

14. Write a Program for the Multiplication Table of


a Natural Number
#include<iostream.h>
void main()
{
int no, divisor, product;
cout<<":::Multiplication Table of any Natural Number:::"<<endl;
cout<<endl;
cout<<"Enter Number: ";
cin>>no;
cout<<endl;
divisor=0;
while(divisor<=10)
{
product=no*divisor;
cout<<no<<"x"<<divisor<<"= "<<product<<endl;

divisor++;

}
Output:

23

15. Write a Program to Print a Number Pyramid


#include<iostream.h>
void main()
{
cout<<":::NUMBER PYRAMID:::"<<endl;
cout<<endl;

for(int n=1 ; n<=10 ; n++)


{
for(int x=1 ; x<n ; x++)
{
cout<<x;
}
cout<<endl;
}

Output:

24

16. Write a Program to Print a Star Pyramid


#include<iostream.h>
void main()
{
cout<<":::STAR PYRAMID:::"<<endl;
cout<<endl;
for(int m=1 ; m<=11 ; m++)
{
for(int x=1 ; x<=m ; x++)
{
cout<<"*";
}
cout<<endl;
}
for(int n=12 ; n>=1 ; n--)
{
for(int y=1; y<=n ; y++)
{
cout<<"*";
}
cout<<endl;
}

}
Output:

25

17. Write a Program to Check Whether a Triangle


is Equilateral, Isosceles or Scalene
#include<iostream.h>
void main()
{
float side1, side2, side3;
cout<<"Check Wether a Triangle is Equilateral, Isosceles or Scalene"<<endl;
cout<<endl;
cout<<"Enter The Length of the First side: ";
cin>>side1;
cout<<"Enter the length of the Second side: ";
cin>>side2;
cout<<"Enter the length of the Third side: ";
cin>>side3;
cout<<endl;
if(side1==side2 && side2==side3) //'&&' signifies that both conditions should be met
{
cout<<"All side are equal."<<endl;
cout<<"It is an Equilateral Triangle!";

}
else if(side1==side2 || side2==side3 || side1==side3) //'||' stands for 'or'
{
cout<<"Two sides are Equal."<<endl;
cout<<"It is an Isosceles Triangle!";
}
else if(side1!=side3 && side2!=side3 && side1!=side2)
{
cout<<"No sides are equal."<<endl;
cout<<"It is a Scalene Triangle!";
}

26

Output:

27

18. Write a Program to Perform Addition,


Subtraction, Multiplication and Division of Two
Integers
#include<iostream.h>
void main()
{
int a, b, sum, diff, product, div;
char op;
cout<<"Perform Addition, Subtraction, Multiplication and Division of Two Integers"<<endl;
cout<<endl;
cout<<"Enter the Operator(+,-,*,/): ";
cin>>op;
switch(op)
{
case '+': cout<<"ADDITION"<<endl;
cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
sum=a+b;
cout<<"Answer: "<<sum; break;
case '-': cout<<"SUBTRACTION"<<endl;
cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
diff=a-b;
cout<<"Answer: "<<diff; break;
case '*': cout<<"MULTIPLICATION"<<endl;
cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
product=a*b;
cout<<"Answer: "<<product; break;
case '/': cout<<"DIVISION"<<endl;

28

cout<<"Enter First Number: ";


cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
div=a/b;
cout<<"Answer: "<<div; break;
default: cout<<"Wrong Choice!";
}

Output:

29

19. Write a Program to Print the Given Numbers in


Reverse Order
#include<iostream.h>
void main()
{
float A[5];
int i;

//'A[ ]' is an array

cout<<"Print Given Numbers in Reverse Order"<<endl;


cout<<endl;
cout<<"Enter 5 Numbers";
cout<<endl;
for(i=0 ; i<5 ; i++)
{
cin>>A[i];
}
cout<<endl;
cout<<"Reverse Order:-"<<endl;

for(i=4 ; i>=0 ; i--)


{
cout<<A[i]<<endl;
}

30

Output:

31

20. Write a Program to Find the Sum and Mean Average


of 10 Integers
#include<iostream.h>
void main()
{
int A[10];
float avg, sum=0;
cout<<":::Sum and Mean Average of 10 Integers:::"<<endl;
cout<<endl;
for(int i=0 ; i<10 ; i++)
{
cin>>A[i];
}

//this is done for the user to input the array

for(i=0 ; i<10 ; i++)


{
sum=sum+A[i];
}
cout<<"Sum= "<<sum<<endl;
avg=sum/10;
cout<<"Average= "<<avg;

}
Output:

32

21. Write a Program to Find the Largest and the


Smallest Number in an Array
#include<iostream.h>
void main()
{
int A[10], MAX, MIN, i;
cout<<":::Smallest & Largest Number:::"<<endl;
cout<<endl;
cout<<"Enter 10 Numbers: ";
for(i=0 ; i<10 ; i++)
{
cin>>A[i];
}
MAX=MIN=A[0];
for(i=1 ; i<10 ; i++)
{
if(A[i]>MAX)
MAX=A[i];
if(A[i]<MIN)
MIN=A[i];
}

cout<<"Smallest Number: "<<MIN<<endl;


cout<<"Largest Number: "<<MAX<<endl;

33

Output:

34

22. Write a Program to Display all Even Numbers in


an Array
#include<iostream.h>
void main()
{
int A[10], i;
cout<<"Even Numbers in an Array"<<endl;
cout<<endl;
cout<<"Enter 10 Numbers: "<<endl;
for(i=0 ; i<10 ; i++)
{
cin>>A[i];
}
cout<<endl;
cout<<"Even Numbers are:-"<<endl;
for(i=0 ; i<10 ; i++)
{
if(A[i]%2==0)
cout<<A[i]<<endl;
}
}
Output:

35

23. Write a Program to display the First 20 Numbers


of the Fibonaccis Sequence
#include<iostream.h>
void main()
{
int fib[20], i, a, b;
cout<<":::FIBONACCI'S SEQUENCE:::"<<endl;
cout<<endl;
fib[0]=0, fib[1]=1;
for(i=2 ; i<20 ; i++)
{
a=i-1 ; b=i-2;
fib[i] = fib[a] + fib[b];
}

for(i=0 ; i<20 ; i++)


cout<<fib[i]<<endl;

Output:

36

24. Write a Program to sort an Array in Ascending


Order (Linear Sort)
#include<iostream.h>
void main()
{
int A[6], i, j, flag;
cout<<"ASCENDING ORDER"<<endl;
cout<<endl;
cout<<"Enter 6 Numbers: "<<endl;
for(i=0 ; i<6 ; i++)
cin>>A[i];
for(i=0 ; i<5 ; i++)
// i goes till 5 because the last position will settle on its own
{
for(j=i+1 ; j<6 ; j++)
{
if(A[i]>A[j])
{
flag=A[i];
A[i]=A[j];
A[j]=flag;
}
}
}
cout<<endl;
cout<<"Ascending Order:-"<<endl;
for(i=0 ; i<6 ; i++)
cout<<A[i]<<endl;
}

37

Output:

38

25. Write a Program to check if a Given String is a


Palindrome
#include<iostream.h>
#include<stdio.h>
#include<string.h>
void main()
{
int i, j, len=0, flag=0;
char str[100];

//this is to use gets function

cout<<":::PALINDROME:::"<<endl;
cout<<endl;
cout<<"Enter String: ";
gets(str);
len=strlen(str);

//Length of string

j=len-1;
for(i=0 ; i<(len/2) ; i++)
{
if (str[i]==str[j])
{
flag=1;
}
else
flag=0;
j--;
}

if(flag==1)
cout<<"The given string is a palindrome.";
else if(flag==0)
cout<<"The given string is not a palindrome.";

39

Output:

40

26. Write a Program to find the Factorial of a


Number
#include<iostream.h>
void main()
{
float i, x, factorial=1;
cout<<":::FACTORIAL OF A NUMBER:::"<<endl;
cout<<endl;
cout<<"Enter Number: ";
cin>>x;
for(i=1 ; i<=x ; i++)
factorial=factorial*i;

cout<<"Factorial of "<<x<<" is "<<factorial<<".";

Output:

41

27. Write a Program to Find the Logical Length of a


String
#include<iostream.h>
#include<string.h>
#include<stdio.h>
void main()
{
int length;
char str[50];
cout<<":::LENGTH OF A STRING:::"<<endl;
cout<<endl;
cout<<"Enter String: ";
gets(str);
length=strlen(str);

//this gives us the logical length of the string

cout<<endl;
cout<<"Length of String= "<<length;

Output:

42

28. Write a Program to sort an Array in Descending


Order (Bubble Sort)
#include<iostream.h>
void main()
{
int A[6], i, j, flag;
cout<<"DESCENDING ORDER"<<endl;
cout<<endl;
cout<<"Enter 6 Numbers: "<<endl;
for(i=0 ; i<6 ; i++)
cin>>A[i];
for(i=0 ; i<5 ; i++)
// i goes till 5 because the last position will settle on its own
{
for(j=0 ; j<5-i ; j++)
{
if(A[j+1]>A[j])
{
flag=A[j+1];
A[j+1]=A[j];
A[j]=flag;
}
}
}
cout<<endl;
cout<<"Descending Order:-"<<endl;
for(i=0 ; i<6 ; i++)
cout<<A[i]<<endl;
}

43

Output:

44

29. Write a Program to count the Number of Spaces


and Words in a String.
#include<iostream.h>
#include<string.h>
#include<stdio.h>
void main()
{
int count=0, i;
char str[100];
cout<<"Number of Spaces and Words in a String"<<endl;
cout<<endl;
cout<<"Enter String: ";
gets(str);
cout<<endl;
for(i=0 ; str[i]!='\0' ; i++)
{
if(str[i]==' ')
count=count+1;

//there is space between single quotes

}
cout<<"Number of Spaces= "<<count;
cout<<endl;
count=count+1;
cout<<"Number of Words= "<<count;
}

45

Output:

46

30. Write a Program to Display the Initials of only


the First Two Words in a String
#include<iostream.h>
#include<string.h>
#include<stdio.h>
void main()
{
int i;
char str[100];
cout<<"Enter String: ";
gets(str);
cout<<endl;
cout<<str[0]<<".";
for(i=1 ; str[i]!=' ' ; i++)
{}
i=i+1;

//loop will go up till space

cout<<str[i]<<".";
for( ; str[i]!=' ' ; i++)
i=i+1;

for( ; str[i]!='\0' ; i++)


cout<<str[i];

47

Output:

48