Programming Fundamental handouts

9
Manual # 2 Topic: Programming Fundamental Assignment Subject: Programming Fundamental For BS(CS), BS(EE), DAE(Computer Science), BSC(14 Years) Author: Sunawar Khan Ahsan MS(Computer Vision) Mehwish Shabbir

Transcript of Programming Fundamental handouts

Manual # 2

Topic:

Programming Fundamental Assignment

Subject:

Programming Fundamental

For BS(CS), BS(EE), DAE(Computer Science), BSC(14 Years)

Author:

Sunawar Khan Ahsan MS(Computer Vision)

Mehwish Shabbir

Program 1: Write a program that prints a text of 4 lines consisting of characters, integer values and floating point value using cout statement.

Program 2: Write a program that inputs name, age and address from the user and display it on the screen.

Program 3: Write a program that inputs base height from the user and calculates area of a triangle by using the formula Area = ½ * Base *Height.

Program 4: Write a program that inputs temperature from the using Celsius and converts it into Fahrenheit using the formula F = 9/5 * C + 32.

Program 5: Write a program that inputs 4 numbers and calculates the sum, average, and product of all the numbers

Program 6: write a program that converts a person’s height from inches to centimeters using the formula 2.54 *height.

Program 7: Write a program that inputs radius from the user and calculates area and circumference of circle using formula Area = πr2. Program 8: Write a program that inputs two numbers and exchange their values the program should display the values before and after exchange.

Program 9: Write a program that inputs radius from the user and calculates area and circumference of sphere using formula Area = 4πr2 Circumference = 4/3πr3. Program 10: Write a program that inputs a three digit number from the user and displays it in reverse order. For example if the user enter 123, it displays 321. Program 11: Write a program that inputs miles from the user and converts miles into kilometers. One mile is equal to 1.609km. are given. It inputs values of a, b and c. Formula for the area of triangle is Area = √( − )( − 0( − Program 12: Write a program that finds area of triangle when three sides a, b and c of the triangle

) Where s = (a + b + c)/2.

Program 13: Write a program that inputs temperature in Fahrenheit and convert it into Celsius. Program 14: Write a program that inputs a number and finds whether it is even or odd using if-else structure.

Program 15: Write a program that inputs salary and grade. It adds 50% bonus if grade is greater than 15. It adds 25% bonus if grade is 15 or less and then displays the salary.

Program 16: Write a program that inputs salary. If salary is 20000 or more, it deducts 7% of salary.

If salary is 10000 or more or more but less than 20000, it deducts 1000. If salary is less than 10000,

it deducts nothing and then displays net salary.

Program 17: Write a program that inputs from user and determines whether it is positive,

negative or zero.

Program 18: Write a program that inputs test score of a student and displays his grade on the following scale

Test Score Grade

>=90 A

80-90 B

70-79 C

60-69 D

Below 50 F

Program 19: Write a program that inputs radius. It calculates area of circle if user enters1 as choice. It calculates circumference if the user enters 2 as choice. It displays error message in case of any other choice. Program 20: Write a program that inputs temperature and displays a message according to

following table: Temperature Message Greater than 35 Hot Day

Between 25 and 25 Pleasant Day

Less than 25 Cool Day

Program 21: Write a program that inputs three numbers and displays the smallest number by using nested if condition.

Program 22: Write a program that inputs three numbers and displays the maximum number by using logical operators.

Program 23: Write a program that inputs a character and displays whether it is vowel or consonant using switch statement.

Program 24: Write a program that inputs two numbers and one arithmetic operator. It applies arithmetic operation on two numbers on the basis of operator entered by user using switch statement.

Program 25: Write a program that displays counting from 1 to 10 using while loop.

Program 26: Write a program that displays first five numbers and their sum using while loop. Program 27: Write a program that displays first five numbers with their squares using while loop. Program 28: Write a program that inputs two numbers and exchange their values the program should display the values of variables before and after exchange.

Program 29: Write a program that inputs a number from the user and displays a table of that number using while loop.

Program 30: Write a program that inputs a number from the user and displays the factorial of

that number using while loop.

Program 31: Write a program that displays the sum of following series using while

loop. 1 + 1 / 2 + 1 / 4 + 1 / 6 + . . . . . . . + 1 / 100.

Program 32: Write a program that displays sum of following series using do-while loop. Program 33: Write a program that inputs starting and ending point from the user and displays all odd numbers in the given range using do-while loop. Program 34: Write a program that inputs two numbers from the user and displays the result of first number raise to the power of second number using do-while loop. Program 35: Write a program that produces the following output:

0 1 1 2 2 4 3 8 4 16 5 32 6 64

Program 36: Write a program that inputs a number from the user and displays the factorial of that number using do-while loop. Program 37: Write a program that displays alphabets from A to Z using for loop. Program 38: Write a program that displays product of all odd numbers from 1 to 10 using for loop. Program 39: Write a program that inputs a number from the user and displays the factorial of that number using for loop. Program 40: Write a program that inputs table number and length of table and then displays the table using for loop. Program 41: Write a program that inputs a positive integer number from the keyboard and displays it’s in reverse number. For example, the reverse of 345 is 543. Program 42: Write a program that calculates and displays the sum of following series using for loop.

Program 43: Write a program that finds sum of the squares of integers from 1 to n. Where n is appositive value entered by the user (i.e. sum = 12 + 22 + 32 + ….. + n2) Program 44: Write a program that displays the following shape using while loop. Program 45: Write a program that calculates and displays sum of the following series using for loop: 1! + 2! + 3! + 4! + 5! Where “!” represents factorial of the number.

Program 46: Write a program that calculates and displays the sum of the following series using

for loop: 1 + 2x +3x2 + 4x3 + 5x4

Program 47: Write a program that calculates and displays the sum of the following series using for loop: 1/2 + 2/3 + 3/4 +. . . . . . + 99/100

Program 48: Write a program that inputs five values and displays their squares and cubes using loop. Program 49: Write a program that inputs two numbers and displays greatest common divisor of the numbers. Program 50: Write a program that displays the following shape using nested loops. The outer loop should be for loop and inner loop should be while loop. *******

******

*****

****

***

**

* Program 51: Write a program that displays the following block using nested for loop.

*****

*****

*****

*****

***** Program 52: Write a program that displays the following shape using nested for loop.

*******

* * * * * *

******* Program 53: Write a program that displays the following shape using nested do-while loop.

4 4 4 4

3 3 3

2 2

1

Program 54: Write a program that displays the following shape using nested for loop. *

**

***

****

*****

Program 55: Write a program that displays the following shape using nested for loop. 0 01 012

0123

01234

012345 Program 56: Write a program that displays the following shape using nested for loop. *****

****

***

**

* Program 57: Write a program that displays the following shape using nested for loop.

*

**

***

****

***** Program 58: Write a program that displays the following shape using nested for loop. 1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Program 59: Write a program that displays the following shape using nested for loop. 1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Program 60: Write a program that inputs an integer and displays whether it is a prime number or not.

Program 61: Write a program that inputs a number from the user and displays all prime numbers

which are less than the input number using any loop. Program 62: Write a program that inputs two numbers in main function and passes these numbers

to a function. The function displays the maximum number. Program 63: Write a program that inputs a number in main function and passes the number to a

function. The function displays the factorial of that number. Program 64: Write a program that inputs a number in main function and passes the number to a

function. The function displays the table of that number. Program 65: Write a program that inputs two numbers in main function and passes these numbers

to a function. The function displays first number raised to the power of second number.

Program 66: Write a program that prompts the user to enter a number and reverse it. Write a

function Reverse () to reverse the number. For example, if the user enters 2765, the function

should reverse it so that it becomes 5672. The function should accept the number as a parameter

and return the reverse number. Program 67: Write a function GCD that has two input parameters and returns the greatest common

divisor of two numbers passed to it. Write a program that inputs two numbers and calls GCD to

compute greatest common divisor of the number. Program 68: Write a program that inputs two integers and then passes them to four function add,

subtract multiply and divide one by one. Program 69: Write a function is prime that has an input parameter num and returns a value of 1 if

it is prime otherwise it returns the value of 0. Program 70: Write a program that prompts user for Cartesian coordinates of two points x1, y1 and

x2, y2 and displays distance between them. Write a function Distance () with four parameters to

compare the distance. The function uses the following formula to compute the distance and returns

the result to call function:

Distance = sqrt ((a2 – a1)2 + (b2 – b1)2)

-----------------------------------------------------------------------------------------------------------

S u n a w a r K h a n A h s a n For solution contact me at 03334892200

Mail: [email protected]

-----------------------------------------------------------------------------------------------------------

ALL CREDIT GOES TO MEHWISH SHABBIR

I LOVE HER