computer assignment new
TRANSCRIPT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 1/22
Program Number 9
Date : 16/2/12
Q9.) Write a C++ program that intakes two arguments : a character and an integer and prints the
character given number of times. If, however, the integer is missing the function prints the
character twice.
Source Code
#include<iostream.h>
#include<conio.h>
void print_char(char ch,int n=2)
{
for(int i=1;i<=n;i++)
{
cout<<"\n\t"<<ch;
}
}
void main()
{
clrscr();
int x=2;
char a;
cout<<"\n\tEnter a Character : ";cin>>a;
cout<<"\n\tEnter number ['x' to print the Charater 'x' times] : ";
cin>>x;
cout<<endl;
print_char(a,x);
getch();
}
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 2/22
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 3/22
Program Number 8
Date : 16/2/12
Q8.) Write a function to take an int argument and return 0 if the given number is prime otherwise
return -l .
Source Code
#include<iostream.h>
#include<conio.h>
int prime_not(int number)
{
int x=0;
if(number==2||number==3)
x=0;
else
for(int i=2;i<=number/2;i++)
if(number%i==0)
{
x=-1;
break;
}
return (x);}
void main()
{
clrscr();
int a;
cout<<"\n\tEnter a Number : ";
cin>>a;
cout<<"\n\tIf Return Value is '0', then it is Prime.";
cout<<"\n\tOr if Return Value is '-1', then Number is not Prime.";
cout<<"\n\n\tReturn Value : "<<prime_not(a);
getch();
}
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 4/22
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 5/22
Program Number 7
Date : 16/2/12
Q7.) Write a function to receive an int array, its size and a character '+' or '-'. By default, the
character should be '+', For the character '+', the function returns the sum of positive numbers
stored in the array and for the character '-'. the function returns the sum of negative numbers
stored in the array.
Source Code
#include<iostream.h>#include<conio.h>
int sum_pos_neg(int A[],int size,char ch='+'){
int total=0;if(ch=='+'){
for(int i=0;i<size;i++){
if(A[i]>0){
total+=A[i];}
}
}else if(ch=='-'){
for(int i=0;i<size;i++){
if(A[i]<0){
total+=A[i];}
}}return (total);
}void main(){
clrscr();int arr[100],n;char opt;cout<<"\n\tEnter Number of Elements : ";cin>>n;cout<<"\nEnter Elements Of Array : ";for(int i=0;i<n;i++){
cout<<"\nEnter "<<i+1<<" Element : ";
cin>>arr[i];}cout<<"\n\tEnter '+' for sum of all positive elements.";
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 6/22
cout<<"\n\tEnter '-' for sum of all negative elements.";cout<<"\n\tEnter Your Operation Choice : ";cin>>opt;if(opt=='-'||opt=='+')
cout<<"\n\tSum : "<<sum_pos_neg(arr,n,opt);else
cout<<"\n\tEnter a Valid Choice...";getch();
}
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 7/22
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 8/22
Program Number 6
Date : 16/2/12
Q6.)Make a C++ program ‘power()’ which takes base and power as arguments and returns the result
as :
power (0, n) = 0, for each n
power (x, n) = xn, if x≠0 and n > 0
power (x, 0) = 1, for each x
Source Code
#include<iostream.h>
#include<conio.h>
long power(int x,int n)
{
long res=1;
if(x==0)
res=0;
else if(n==0)
res=1;
else if(n>0)
for(int i=0;i<n;i++)
res*=x;else
{
for(int j=0;j>-n;j--)
res*=x;
res=1/res;
}
return res;
}
void main()
{
clrscr();int base,exponent;
cout<<"\n\tEnter Value for Base : ";
cin>>base;
cout<<"\n\tEnter Power for Base : ";
cin>>exponent;
cout<<"\n\t"<<base<<"^"<<exponent<<" : "<<power(base,exponent);
getch();
}
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 9/22
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 10/22
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 11/22
Program Number 5
Date : 16/2/12
Q5.) Write a complete C++ program that uses a function called carea( ) to calculate area of a circle.
The function carea( ) receives radius of float type & returns area of double type. The function main( )
gets a radius value from the user, calls carea( ), and displays the result. The function carea( ) is local
to main( ).
Source Code
#include<iostream.h>
#include<conio.h>double carea(float radius)
{
return (3.14*radius*radius);
}
void main()
{
clrscr();
float a;
cout<<"\n\tEnter Radius of Circle : ";
cin>>a;
cout<<"\n\tArea of Circle : "<<carea(a);
getch();
}
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 12/22
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 13/22
Program Number 4
Date : 16/2/12
Q4.) Write a C++ program that uses a function smallo( ) (that is passed two int
argument by value)
to receive reference of the smaller value. Then using this reference the
smaller value is set to
0. Write a main ( ) function also to exercise this function.
Source Code
#include<iostream.h>
#include<conio.h>
void smallo(int a, int b)
{
if(a>b)
b=0;
else if(a<b)
a=0;
else
cout<<"\nBoth Numbers Are Equal";
cout<<"\nValue of First Number = "<<a;
cout<<"\nValue of Second Number = "<<b;}
void main()
{
clrscr();
int first,second;
cout<<"\nEnter First Number : ";
cin>>first;
cout<<"\nEnter Second Number : ";
cin>>second;
smallo(first,second);
getch();
}
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 14/22
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 15/22
Program Number 3
Date : 16/2/12
Q3.) Write a C++ program that uses following functions :
(i) sqlarge() that is passed two int arguments by reference and then sets
the larger of the two numbers to its square.
(ii) sum() that is passed an int argument by value and that returns the
sum of the individual digits of the passed number.
(iii) main() that exercises above two functions by getting two integers from
the user and by
printing the sum of the individual digits of the square of the largernumber.
Source Code
#include<iostream.h>
#include<conio.h>
void sum(int a)
{
int n=a;
float rem=0,total=0;
while(n!=0)
{
rem=n%10;
total+=rem;
n=n/10;
}
cout<<"\n\n\tSum of Digits of the Square of Larger Number : "<<total;
}
void sqlarge(int a,int b){
int max=a;
if(a<b)
max=b;
int sqlarge=max*max;
cout<<"\n\tLarger Number : "<<max;
cout<<"\n\n\tSquare of Larger Number : "<<sqlarge;
sum(sqlarge);
}
void main()
{clrscr();
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 16/22
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 17/22
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 18/22
Program Number 2
Date : 16/2/12
Q2) Write a C++ program to use the following function :
(iii) display( ) to display a matrix of a size m x n.
(ii) times2( ) to double each number of the matrix of size m x n.
(iii) maint() to read a matrix of size m x n and then to display original
matrix and then to display the new matrix formed by doubling its
elements.
Source Code
#include<iostream.h>
#include<conio.h>
void display(int A[10][10],int m,int n)
{
int i=0,j=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<”\t”<<A[i][j];}
cout<<”\n\t”;
}
}
void times2(int A[10][10],int m,int n)
{
int i=0,j=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
A[i][j]=2*A[i][j];
}
}
cout<<”\n\tDoubled Matrix : \n\n\t”;
display(A,m,n);
}
void main()
{
clrscr();
int i=0,j=0;int array[10][10],row,col;
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 19/22
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 20/22
OUTPUT
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 21/22
Program Number 1
Date : 16/1/12
Q14.) Write a program uses a function power( ) to raise a number m to power n.
The function takes int values for m and n and returns the result correctly. Use a
default value of 2 for n to make the function calculate squares when this
argument is omitted. Write a main( ) to get the value of m and n from the user
and to display the calculated result.
Source Code
#include<iostream.h>
#include<conio.h>
#include<math.h>
double power(float m,float n)
{
return (pow(m,n));
}
void main()
{
clrscr();
float base,exp=2;
cout<<"\nEnter Base Value : ";
cin>>base;
cout<<"\nEnter Exponential Value : ";
cin>>exp;
cout<<"\n\t"<<base<<"^"<<exp<<" : "<<power(base,exp);
getch();
}
8/3/2019 Computer Assignment New
http://slidepdf.com/reader/full/computer-assignment-new 22/22
OUTPUT