Computer Science Practicals Part II

Q1 :
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[10],b[10],c[10];
int i;
cout<<“Enter the elements of the first array: “;
for(i=0 ; i<10 ; i++)
{
cin>>a[i];
}
cout<<“Enter the elements of the second array: “;
for(i=0 ; i<10 ; i++)
{
cin>>b[i];
}
for(i=0 ; i<10 ; i++)
{
c[i]=a[i]*b[i];
}
cout<<“The product of the corresponding elements of the above arrays is: “;
for(i=0 ; i<10 ; i++)
{
cout<<a[i]<<” * “<<b[i]<<” = “<<c[i]<<“\n”;
}
getch();
}


Q2 Same as Q1 in Computer Science Practicals


Q3:
#include<iostream.h>
#include<conio.h>
long factorial(int);            //Function Declaration
void main()
{
clrscr();
int n;
long result;
cout<<“Enter the number to calculate factorial : “;
cin>>n;
result = factorial(n);        //Function Calling
cout<<“The factorial of “<<n<<” is “<<result;
getch();
}

//Function Definition
long factorial(int n)
{
long fact=1;
for(int i=1; i<=n; i++)
{
fact = fact * i;
}
return fact;
}


Q4 Same as Q9 in Computer Science Practicals


Q5:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[50];
int Acount=0, Dcount=0, Scount=0,SScount=0, len,i;
cout<<“Enter the string : “;
gets(str);
len = strlen(str);
for(i=0; i<len; i++)
{
if((‘a’ <= str[i] && str[i] <= ‘z’) || (‘A’ <= str[i] && str[i] <= ‘Z’))
{
Acount++;
}
else if(‘0’ <= str[i] && str[i] <= ‘9’)
{
Dcount++;
}
else if(str[i]==’   ‘)
{
Scount++;
}
else
{
SScount++;
}
}
cout<<“The Entered String “;
puts(str);
cout<<“Countains “<<Acount<<” Alphabets “<<Dcount<<” Digits “<<Scount<<” Spaces “<<” and “<<SScount<<” Special Symbols”;
getch();
}


Q6:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, greatest;
cout<<“Enter the elements of the array: “;
for(i=0 ; i<10 ; i++)
{
cin>>a[i];
}
greatest = a[0]; //storing the 1st element of the array in variable greatest
//Now our Logic Begins…
for(i=0 ; i<10 ; i++)
{
if(a[i] > greatest)
{
greatest = a[i];
}
}
cout<<“The greatest number in the given array is “<<greatest;

getch();
}


Q7:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[50];
int i,len;
cout<<“Enter the string : “;
gets(str);
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i] == ‘ ‘)
{
str[i] = ‘-‘;
}
}
cout<<“The Modified String is : “;
puts(str);
getch();
}


Q8:
#include<conio.h>
void main()
{
clrscr();
int a[10], i;
cout<<“Enter the elements of the array: “;
for(i=0 ; i<10 ; i++)
{
cin>>a[i];
}
//Now our Logic Begins…
cout<<“The Entered array \t The Double of the array\n”;
for(i=0 ; i<10 ; i++)
{
cout<<“\t”<<a[i]<<“\t\t\t”<< (a[i] * 2) <<“\n”;
}
getch();
}


Q9:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[50];
int i,len;
cout<<“Enter the string : “;
gets(str);
len = strlen(str);
cout<<“The string in reverse order is: “;
for(i=len; i>=0; i–)
{
cout<<str[i]<<“\n”;
}
getch();
}


Q10:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, smallest;
cout<<“Enter the elements of the array: “;
for(i=0 ; i<10 ; i++)
{
cin>>a[i];
}
smallest = a[0]; //storing the 1st element of the array in variable smallest
//Now our Logic Begins…
for(i=0 ; i<10 ; i++)
{
if(a[i] < smallest)
{
smallest = a[i];
}
}
cout<<“The smallest number in the given array is “<<smallest;
getch();
}


Q11 Same as Q12 in Computer Science Practicals


Q12:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, smallest, greatest;
cout<<“Enter the elements of the array: “;
for(i=0 ; i<10 ; i++)
{
cin>>a[i];
}
smallest = a[0]; //storing the 1st element of the array in variable smallest
greatest = a[1]; //storing the 2nd element of the array in variable smallest
//Now our Logic Begins…
for(i=0 ; i<10 ; i++)
{
if(a[i] < smallest)
{
smallest = a[i];
}
else if(a[i] > greatest)
{
greatest = a[i];
}
}
cout<<“The smallest number in the given array is “<<smallest<<“\n”;
cout<<“The greatest number in the given array is “<<greatest;
getch();
}


Q13:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i;
cout<<“Enter the number : “;
cin>>n;
if(n<0)
{
cout<<“The given number is -ve, it can’t be square of any real number.”;
}
else if(n>0)
{
for(i=0 ; i<=n ; i++)
{
if(i*i == n)
{
cout<<“The given number is a perfect square.”;
break;
}
}
}
else
{
cout<<“Enter a positive Number”;
}
getch();
}


Q14 Same as Q1 in Computer Science Practicals


Q15:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, x;
cout<<“Enter the elements of the array: “;
for(i=0 ; i<10 ; i++)
{
cin>>a[i];
}
cout<<“Enter the element to search in the array : “;
cin>>x;
//Now our Logic Begins…
for(i=0 ; i<10 ; i++)
{
if(a[i] == x)
{
cout<<“The “<<x<<” is at position “<<i;
break;
}
else
{
if(i == 9)
cout<<“Search number NOT found in the given list.”;
}
}
getch();
}


Q16:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, even=0, odd=0;
cout<<“Enter the elements of the array: “;
for(i=0 ; i<10 ; i++)
{
cin>>a[i];
}
//Now our Logic Begins…
for(i=0 ; i<10 ; i++)
{
if(a[i]%2 == 0)
{
even++;
}
else
{
odd++;
}
}
cout<<“Total even numbers in the array are : “<<even<<“\n”;
cout<<“Total odd numbers in the array are : “<<odd;
getch();
}


Q17 Same as Q18 in Computer Science Practicals


Q18:
#include<iostream.h>
#include<conio.h>
long maximum(int, int, int);         //Function Declaration
void main()
{
clrscr();
int a, b ,c, result;
cout<<“Enter the three numbers : “;
cin>>a>>b>>c;
result = maximum(a, b, c);           //Function Calling
cout<<“The maximum among “<<a<<” , “<<b<<” and “<<c<<” is “<<result;
getch();
}

//Function Definition
long maximum(int a, int b, int c)
{
if(a>b && a>c)
return a;
else if(b>c && b>a)
return b;
else
return c;
}


Q19:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i;
long sum=0;
float avg, count=0;
cout<<“Enter the NUMBER to find SUM and AVERAGE : “;
for(i=0; i<10; i++)
{
cin>>n;
sum = sum + n;     //calculating addition
count++;
}
cout<<“The ADDITION of above numbers are : “<<sum;
avg = sum/count;
cout<<“\nThe AVERAGE of above numbers is : “<<avg;
getch();
}


Q20:
#include<iostream.h>
#include<conio.h>
int prime(int);    //Function Declaration

void main()
{
clrscr();
int n, i, count = 0, result;
cout<<“Enter a number to check its PRIME or NOT : “;
cin>>n;
result = prime(n);            //Function Calling
if(result == 1)
cout<<n<<” is a PRIME NUMBER”;
else
cout<<n<<” is NOT PRIME NUMBER”;
getch();
}

//Functin Definition
int prime(int n)
{
int i, count=0;
for(i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
return 1;
else
return 0;
}


Q21:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long fact=1,n;
cout<>n;
for(int i=1; i<=n; i++)
fact = fact * i;
cout<<"Factorial of "<<n<<" is "<<fact;
getch();
}


Q23:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,tempa,tempb;
cout<>a>>b;
tempa = a;
tempb = b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout<<"The GCD of "<<tempa<<" and "<<tempb<<" is "<<a<<endl;
getch();
}


Q24:
#include<iostream.h>
#include<conio.h>
float area(float, float); //Function Declaration

void main()
{
clrscr();
float b, h, result;
cout<>b;
cout<>h;
result = area(b,h); //Function Calling
cout<<"The area of the triangle is : "<<result<<" Sq. Units.";
getch();
}

//Functin Definition
float area(float b, float h)
{
return (0.5 * b * h);
}


Q25 Same as Q7 in Computer Science Practicals


Q26:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[25], i, n, num;
cout<>n;
//Now our Logic Begins…
if(n>25 || n<0)
{
cout<<"The value must be positive and less then 25";
}
else
{
cout<<"Enter "<<n<<" integers one by one ";
for(i=0 ; i>num;
a[i]=num%10;
}

cout<<"The array of unit's place of all "<<n<<" integers ";
for(i=0 ; i<n ; i++)
{
cout<<a[i];
}
}
getch();
}


Q27:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
int sumOdd = 0, n, i;
for(i=0; i<15; i++)
{
cout<>n;
if(n%2 != 0)
sumOdd = sumOdd + n;
}
cout<<"The sum of odd numbers from the entered numbers is "<<sumOdd;
getch();
}


Q28:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, sum=0, count=0;
cout<<"Enter the elements of the array: ";
for(i=0 ; i>a[i];
if(a[i]>50)
{
count++;
sum=sum+a[i];
}
}
cout<<"There are "<<count<<" numbers greater then 50\n";
cout<<"The sum of numbers greater then 50 is "<<sum;
getch();
}


Q29 Same as Q30 in Computer Science Practicals


Q30:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int f0=0, f1=1, f2, limit, a[20];
cout<>limit;
if(limit <= 20)
{
a[0] = f0;
a[1] = f1;
for(int i=2; i<limit; i++)
{
f2 = f0 + f1;
a[i] = f2;
f0 = f1;
f1 = f2;
}
cout<<"The desired "<<limit<<" terms stored in the array are : ";
for(int i=0; i<limit; i++)
{
cout<<a[i]<<" ";
}
}
else
{
cout<<"OOOOOOPS… Maximum 20 is allowed !!!";
}
getch();
}


Q31 Same as Q32 in Computer Science Practicals


Q32:
#include<iostream.h>
#include<conio.h>
float avg(float, float, float, float); //Function Declaration
void main()
{
clrscr();
float a, b ,c, d;
float result;
cout<>a>>b>>c>>d;
result = avg(a, b, c, d); //Function Calling
cout<<"The average is : "<<result;
getch();
}

//Function Definition
float avg(float a, float b, float c, float d)
{
return ((a+b+c+d) / 4.0);
}


Q33 Same as Q11 in Computer Science Practicals


Q34:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[50], tempStr[50];
int i,len;
cout<<"Enter the string : ";
gets(str);
strcpy(tempStr,str); //copying the entered string in tempStr variable
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
clrscr(); //To print the output on Fresh screen
cout<<"The Original String was : ";
puts(tempStr);
cout<<"The Modified String is : ";
puts(str);
getch();
}


Q35:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,tempa,tempb;
cout<>a>>b;
tempa = a;
tempb = b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout<<"The GCD of "<<tempa<<" and "<<tempb<<" is "<<a<<endl;
cout<<"The LCM of "<<tempa<<" and "<<tempb<<" is "<<(tempa*tempb)/a<<endl;
getch();
}


Q36:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[50], tempStr[50];
int i,len;
cout<<"Enter the string : ";
gets(str);
strcpy(tempStr,str); //copying the entered string in tempStr variable
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i]>=65&&str[i]<=91)
str[i]=str[i]+32;
}
clrscr(); //To print the output on Fresh screen
cout<<"The Original String was : ";
puts(tempStr);
cout<<"The Modified String is : ";
puts(str);
getch();
}


Q37 Same as Q1 in Computer Science Practicals


Q38:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[50];
int i,len,words=1;
cout<<"Enter the line of text : ";
gets(str);
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i] == ' ')
{
words++;
}
}
cout<<"The number of words in the line of text are : "<<words;
getch();
}


Q39 Same as Q8 in Computer Science Practicals


Q40:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<>ch;
if(ch>=97 && ch<=122)
{
cout<<"You have entered LOWERCASE letter.\n";
cout<<"The UPPERCASE of "<<ch<<" is : "<=65 && ch<=91)
{
cout<<"You have entered UPPERCASE letter.\n";
cout<<"The LOWERCASE of "<<ch<<" is : "<<char(ch+32); // Implicit typecasting
}
else
cout<<"Enter only a single UPPERCASE or LOWERCASE character.";
getch();
}


Q41:
#include<iostream.h>
#include<conio.h>
//function declaration
void enter();
void calculate(int, int);
void main()
{
clrscr();
enter(); //function calling enter()
getch();
}
//function defining enter()
void enter()
{
int a,b;
cout<>a>>b;
calculate(a,b); //function calling calculate()
}

//function defining calculate()
void calculate(int a, int b)
{
int tempa = a, tempb = b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout<<"The GCD of "<<tempa<<" and "<<tempb<<" is "<<a<<endl;
cout<<"The LCM of "<<tempa<<" and "<<tempb<<" is "<<(tempa*tempb)/a;
}


Q42:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, even=0, odd=0;
cout<<"Enter 10 numbers one by one : ";
for(i=0 ; i>n;
if(n%2 == 0)
even++;
else
odd++;
}
cout<<"Total even numbers : "<<even<<"\n";
cout<<"Total odd numbers : "<<odd;
getch();
}


Q43:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i;
long sum=0;
cout<<"Enter the element of the array : ";
for(i=0; i>a[i];
sum = sum + a[i]; //calculating addition
}
cout<<"The SUM of the elements of the array is : "<<sum<<"\n";
cout<<"The AVERAGE of the elements of the array is : "<<(sum/10.0);
getch();
}

Q44 Same as Q3 in Computer Science Practicals Part II

2 thoughts on “Computer Science Practicals Part II

Leave a comment