You are on page 1of 16

Winter-2019

Semester
COMP 150 (Introduction to
Programming)

C++ Coding
Assignment 2

Submitted to:
Prof. Siddhartha Shyam Vyas

Submitted by:
Manav Babbar (300166069)
BCIS 1st Year 2nd Semester

Date of Submission: March 19, 2019


March 19, 2019

TABLE OF CONTENTS

S. No. CONTENTS Page


Number
1 CERTIFICATE OF ORIGINALITY i

2 ACKNOWLEDGEMENT ii

3 Q1.Write a program to create a user-defined function - 'conditional_expression()' 1


Take two integers as user input in main()
'conditional_expression()' must print the maximum of two numbers using
conditional expression operators.
'conditional_expression()' must return a value to the main() for printing the result.
4 Q2.Write a program to create a user-defined function - 'test_equality()' 2
Take three integers as a user input in main()
'test_equality()' must test whether the three user inputs are equal or not using
'nested-if'
'test_equality()' must return a value to main() for printing whether the numbers are
equal or not.
5 Q3.Write a program to create a user-defined function - 'finding_vowel()' 3
Take character as a user input in main()
'finding_vowel()' must find whether the user input is a vowel or not using 'if-else'
statement with logical operator - 'OR (||)'
'finding_vowel()' must return a value to main() for printing whether the user input is
a vowel or not
6 Q4.Write a program to create a user-defined function - 'exploring_characters()' 4
Take character as a user input in main()
'exploring_characters()' determines user input is uppercase, lowercase, digit or
special symbol using 'nested if-else'with logical operator 'AND (&&)'
'exploring_characters()' must print the result and not return any value to main().
7 Q5.Write a program to create a user-defined function 'Logical_not()' 5
Take integer as a user input in main()
'logical_not()' must find whether the given number is an even or odd using 'if-else'
statement with logical operator '!'
'logical_not()' must return a value to main() for printing the result.
8 Q6.Write a program to create a user-defined function 'evenodd()'. Find whether a 6
given number is an even or odd number using functions
Take an integer value as a user input in main()
The evenodd() must find whether the integer value is an even or odd number
The evenodd() must return an integer value to the main() to print whether the given
number is an even or odd

9 Q7.Write a program to create a user-defined function 'right_half()' and print the 7


following pattern using the loop of your choice (for, while, do-while).
Take the user-limit in the main()
right_half() must print the pattern upto the size of user-limit entered by the user
right_half() must not return any value to main().
*
**
***
****
*****
10 Q8.Write a program to create a user-defined function 'right_half()' and print the 8
following pattern using the loop of your choice (for, while, do-while).
Take the user-limit in the main()
right_half() must print the pattern upto the size of user-limit entered by the user
right_half() must not return any value to main().
*****
****
*** NOTE: There is a space between the stars
**
*
11 Q9.Write a program to create a user-defined function 'left-half()' and print the 9
following pattern using the loop of your choice (for, while, do-while):
left_half() must print the pattern upto the size of user-limit entered by the user
left_half() must not return any value to main().
*
**
*** NOTE: There is a space between the stars
****
*****
12 Q10.Write a program to create a user-defined function 'pyramid()' and print the 10
following pattern using the loop of your choice (for, while, do-while):
pyramid() must print the pattern upto the size of user-limit entered by the user
pyramid() must not return any value to main().
*
***
***** NOTE: There is a space between the stars
*******
*********
13 REFERENCES 11
CERTIFICATE OF ORIGINALITY
I hereby declare that:
 I have used my own words
 I have not copied
 I have not plagiarized
 I have cited all the sources from where I have adapted / sourced

Manav Babbar (300166069)


Signature:
ACKNOWLEDGEMENT

We want to express our profound gratefulness to our Prof. Siddhartha Shyam


Vyas, Ph.D. for his time and the knowledge he provides through his teaching.
I’ve learnt many new things through this assignment and cleared my basics.
Thank You.

Manav Babbar (300166069)


Signature: -
NOTE:
Dear student,

There should be no page# in the footer section of FRONT PAGE, TABLE OF CONTENTS.

ACKNOWLEDGEMENT & CERTIFICATE OF ORIGINALITY pages will have Roman Numeral page# in
the footer section as specified in the TABLE OF CONTENTS (i) (ii)

CERTIFICATE OF ORIGINALITY must remain exactly the same as shown in the document. The name
of the student must be typed neatly with hand-signatures using a blue / black pen beside it.

Please check the ACKNOWLEDGEMENT part. However, you can modify it further by adding more
people you have connected with apart from your COMP150 professor for the assignment. The name of
the student must be typed neatly with hand-signatures using a blue / black pen beside it.

The normal page# like: 1, 2 etc. in the footer section must start from Question 1 of the ASSIGNMENT.
Also, the first question should be written completely followed by the code for the question typed neatly
and legibly. Once the code part is done, then also put the screen-shot of the output below it. Every
question of the assignment must start from a fresh new page.

If you have consulted any organization, you should have a good explanation of how efficient is the
algorithm that you are typing for the program compared to what is done and shown to you all in the
class. Also, you can always give a brief overview of the organization consulted and then start the
Question 1 from the fresh new page for the assignment. Kindly staple the visiting cards of people if
consulted any, with regard to your assignment. If visiting cards aren't available, kindly mention the
following: Name of the person, Telephone/ cell number, email-id, designation, organization name

REFERENCES must start from a new page. No page# required in the footer section of REFERENCES
page. REFERENCES must be in APA Style format. If no references are there, then it must be written in
the “REFERENCES” section as – NO REFERENCES AVAILABLE FOR THIS ASSIGNMENT.

FONT STYLE: is your choice but must look professional, neat & tidy.

HEADING FONT-SIZE: 16 pts

SUB-HEADING FONT-SIZE: 14 pts

NORMAL FONT-SIZE OF CONTENTS: 12 pts

Assignments shall be communicated to the students in the class and via email. The due date of
assignment submission has been mentioned in the course outline.
Answers: -

1. WAP using user defined function - 'find_max()'


Take two user inputs in main() function. Make sure that the two numbers entered by the user
aren't equal
'find_max()' must find the maximum of two numbers using 'if-else' statement.
'find_max()' must return the maximum of two numbers to the main() function for printing the
maximum value.

#include<iostream.h>
#include<conio.h>
int max (int x,int y);
void main()
{
clrscr();
int var1, var2, fetch;
cout<<"Enter the 1st number: ";
cin>>var1;
cout<<"Enter the 2nd number: ";
cin>>var2;
fetch=max(var1 , var2);
cout<<" The maximum number is : "<<fetch;
getch();
}
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
2. WAP to perform a four-function calculator using 'nested if-else' / 'switch-case'. Create a different user-
defined functions for the following:
Take the user input in main() function.
addition() - for calculating addition two numbers
difference() - for calculating the difference of two numbers
product() - for calculating the product of two numbers
division() - for calculating the division of two numbers.
All these user-defined functions must return a value to main() function for printing the result

#include<iostream.h>
#include<conio.h>
#include<process.h>
int add(int g, int h);
int sub(int g, int h);
int prod(int g, int h);
float div(int g, int h);
void main()
{
clrscr();
int no1, no2, draw, option;
char answer;
do
{
clrscr();
cout<<"Enter the 1st Number: ";
cin>>no1;
cout<<"Enter the 2nd Number: ";
cin>>no2;
cout<<"\n\n\t\t\t Menu ";
cout<<"\n 1. Addition ";
cout<<"\n 2. Subtraction ";
cout<<"\n 3. Multiplication ";
cout<<"\n 4. Division ";
cout<<"\n 5. Exit ";
cout<<"\n Enter any choice ";
cin>>option;
switch(option)
{
{
case 1:draw=add(no1 , no2);
break;
}
{
case 2:draw=sub(no1 , no2);
break;
}
{
case 3:draw=prod(no1 , no2);
break;
}
{
case 4:draw=div(no1 , no2);
break;
}
{
case 5: cout<<"Exit";
getch();
exit(0);
}
{
default: cout<<"Wrong Choice";
goto i;
}
}
cout<<"The Result of "<<no1<<" & "<<no2<<" is: "<<draw;
i:cout<<"\n Do you want to continue : ";
answer=getch();
}
while((answer=='y')||(answer=='Y'));
getch();
}
int add(int g, int h)
{
return g+h;
}
int sub(int g, int h)
{
return g-h;
}
int prod(int g, int h)
{
return g*h;
}
float div(int g, int h)
{
return float(g/h);
}

3. WAP to create a user-defined function 'factorial()'


Take a user input in main() function.
factorial() must calculate the factorial of the given number
factorial() must return a value to the main() function for printing the result
#include<iostream.h>
#include<conio.h>
int factorial(int x);
void main()
{
clrscr();
int num1, retrieve;
cout<<"Enter a number: ";
cin>>num1;
retrieve=factorial(num1);
cout<<"\nFactorial is: "<<retrieve;
getch();
}
int factorial(int x)
{
int z;
int facto=1;
for(z=1;z<=x;z++)
facto=facto*z;
return facto;
}

4. WAP to create a user-defined function - 'Sum_of_natural_numbers()'


Take a user limit in main() function
'sum_of_natural_numbers()' must compute the sum of natural numbers
'sum_of_natural_numbers()' must return a value to the main() function for printing the result
#include<iostream.h>
#include<conio.h>
int add(int n);
int main()
{
clrscr();
int n;
cout<<"Enter a positive integer: ";
cin>>n;
cout<<"Sum= "<<add(n);
getch();
}
int add(int n)
{
if(n!=0)
return n+ add(n-1);

5. WAP to create two user_defined functions - 'Sum_of_even numbers()' and


'sum_of_odd_numbers()'
Take the range limit in main() function
'sum_of_even_numbers()' must compute the sum of even numbers
'sum_of_odd_numbers()' must compute the sum of odd numbers
Both the user-defined functions must return the values to the main() function for printing the

result

#include<iostream.h>
#include<conio.h>
int sum_of_even(int m);
int sum_of_odd(int m);
void main()
{
clrscr();
int h,i,even,odd;
even=odd=0;
cout<<"\n Enter the limit : ";
cin>>h;

even=sum_of_even(h);
odd=sum_of_odd(h);

cout<<"\n Sum of Even Numbers till "<<h<<" is : "<<even;


cout<<"\n Sum of Odd Numbers till "<<h<<" is : "<<odd;
getch();
}
sum_of_even(int m)
{
int result=0;
for(int i=1;i<=m;i++)
{
if (i%2==0)
result+=i;
}
return result;
}
sum_of_odd(int m)
{
int result=0;
for(int i=1;i<=m;i++)
{
if (i%2!=0)
result+=i;
}
return result;
}

6. WAP to sum up two numbers using functions


* Take two integer values as user input in the main()
* The sum() must compute the addition of two numbers
* The sum() must return an integer value, that is, the result of addition to main() for printing

the result
#include<iostream.h>
#include<conio.h>
int sum(int x,int y);
void main()
{
clrscr();
int var1,var2,grab;
cout<<"Enter the 1st Number: ";
cin>>var1;
cout<<"Enter the 2nd Number: ";
cin>>var2;
grab=sum(var1,var2);
cout<<"\n The addition of 2 Numbers "<<var1<<" & "<<var2<<" is : "<<grab;
getch();
}
int sum(int x,int y)
{
int add=0;
add=x+y;
return add;
}

7. WAP to create a user-defined function - print_days()


Take an integer value as a user input in the range of 1-7 in main()
print_days() must print the days as per the values entered by the user
If the user enters 1 -----> "It is Monday" must be printed in a newline
If the user enters 2 -----> "It is Tuesday" must be printed in a newline and so on and on until 7
is entered by the user.
print_days() must not return a value to main()
Hint: You may use 'switch-case' or 'nested if-else' as per your convenience

#include<iostream.h>
#include<conio.h>
void print_days(int choice);
void main()
{
clrscr();
int d;
cout<<"Enter any no btw 1-7: ";
cin>>d;
print_days(d);
getch();
}
void print_days(int choice)
{
switch(choice)
{
case 1:
{
cout<<" Day is Monday ";
break;
}
case 2:
{
cout<<" Day is Tuesday ";
break;
}
case 3:
{
cout<<" Day is Wednesday ";
break;
}
case 4:
{
cout<<" Day is Thursday ";
break;
}
case 5:
{
cout<<" Day is Friday ";
break;
}
case 6:
{
cout<<" Day is Saturday ";
break;
}
case 7:
{
cout<<" Day is Sunday ";
break;
}
default:
{
cout<<" Wrong Choice ";
}
}

You might also like