Programing Problems

Embed Size (px)

Citation preview

  • 8/18/2019 Programing Problems

    1/22

    Intro

    Easy Programming

    ProblemsA Collection of over 150 beginner’s programs to solve in programming languages like

    Java, C, C#, C++, Basic, Python, etc..

    The programming problems are arranged according to their difficulty of writing the

    easiest solution possible. The problems are also further categorized into several sections.

    Also, test cases for the majority of the programs are provided. 

  • 8/18/2019 Programing Problems

    2/22

    1

    Simple Problems:1.  Write a program to print the value of c, when, 

    =  

    =   = ()

     

    Test Case:

    Output -> 13 

    2.  Write a program to print the value of b, when,

    =

    = + √ 

     Test Case:

    Output -> 8.25

    3. 

    A shop decided to give a discount of 10% on all items. Take the price as input and print thediscounted price. Test Case:

    Input -> 100 

    Output -> 90 

    4.  Write a program to print the circumference of a circle. Take the radius as input. Formula of

    Circumference is given bellow.

    =  

    Test Case:

    Input -> 4 

    Output -> 25.13 

    5.  Write a program to print the area of a circle. Take the radius as input. Formula of Area is given

    bellow.

      =  

    Test Case:

    Input -> 4 

    Output -> 50.24

    6.  Write a program to print the Simple Interest (SI). Take the principal, rate of interest, time as input.

    Formula of SI is given bellow

    = × ×

     

    Test Case:

    Input -> 4 

    Output -> 25.13

  • 8/18/2019 Programing Problems

    3/22

    2

    7.  Write a program to print the temperature in Fahrenheit. Take the temperature in Celsius. Formula

    for temperature conversion is given bellow.

    = −

     

    Test Case:

    Input -> 4 

    Output -> 39.2

    8.  Write a program to print the square root of a integer using Math.pow function or its equivalent. 

    Test Case:

    Input -> 4 

    Output -> 2 (Since, 22 = 4) 

    9.  Write a program to print the value of c, when,  =

     

    =  

    = (− − ) √  ∗

     

    Output -> 1.5

    10. Given an integer n, print the reminder when n is divided by 3.Test Case: Test Case 2:

    Input -> 18 Input -> 19 

    Output -> 0 Output -> 1

    11. Given an integer n, print a number that is needed to be added to n to make n a multiple of 5.Test Case: Test Case 2:

    Input -> 19 Input -> 26 

    Output -> 2 Output -> 1

    12. 

    Given an integer n, return the absolute difference between 25 and n.Test Case: Test Case 2:

    Input -> 20 Input -> 31 

    Output -> 5 Output -> 6 

  • 8/18/2019 Programing Problems

    4/22

    3

    If, else Problems:1.  Write a program to print smallest of two integers.

    2. 

    Write a program to print largest of three integers.

    3.  Take the percentage gained by the user in a test and print whether the user passed or not. To

    pass, a person needs to gain a minimum of 40%.

    4.  Take a number as input from user and print whether the number is odd or even. 

    Test Case:

    Input -> 3 

    Output -> Odd 

    5. 

    Take a number as input from user and print whether the number is a factor of 15. Test Case:Input -> 3 

    Output -> 3 is a factor of 15. 

    6.  Take three numbers as input from user and print "True" if any of the number is a factor of 18 odd

    otherwise print "False". Test Case:

    Input -> 9 1 4 

    Output -> True (Since 9 is a factor of 18.) 

    7.  Write a program to print whether a number is negative or positive or zero. 

    Test Case:Input -> -2 

    Output -> Negative 

    8. 

    Take two numbers as input from user and print whether they end with the same last digit. 

    Test Case: Test Case 2:

    Input -> 14 24 Input -> 16 23 

    Output -> True(Both end with no. 4) Output -> False 

    9.  Take two numbers as input from user and print the number which is nearest to 10.  Test Case:

    Input -> 8 11 

    Output -> 11 (Since, 11 is nearer to 10 than 8) 

    10. 

    Write a program to input any character and print whether it is alphabet or a number or a special

    character. 

    Test Case: Test Case:

    Input -> A Input -> @ 

    Output -> A is a alphabet. Output -> @ is a special character.

  • 8/18/2019 Programing Problems

    5/22

    4

    11. Write a program to check whether a character is Uppercase or Lowercase alphabet. Test Case:

    Input -> A 

    Output -> Uppercase 

    12. Take two letters as input and print whether a the first character come before the 2nd one.  

    Test Case:Input -> A E

     

    Output -> True (Since, A comes before alphabet B) 

    13. 

    Write a program to input any year and check whether it is leap year or not. 

    14. A store sells computer at a rate of $1000 per piece and gives a discount of 12% if the buyer makes

    a purchase of over $9900. Write a program to take the number of computers purchased by the

    buyer as input and print the price the buyer has to pay.

    15. A gamer gets 10 coins each for the first 40 kills and 20 coins each for the next 20 kills and 30 coins

    each beyond 60 kills. Take the kills as input and print the coins the gamer gets.Test Case:

    Input -> 61 Output -> 500 (Since, (40 * 10) + (20 * 20) + (30 * 1) = 830) 

    16. Write a program to swap two numbers without using a temp variable. ⍟ 

  • 8/18/2019 Programing Problems

    6/22

    5

    Switch case Problems:1.  Take a grade as input from user and greet the user according to the following:  

    Grade A - Excellent Grade B - Good Work 

    Grade C - Can Improve more. Grade D - Need to Improve. 

    2.  Take a number from one to nine as input from user and print the number in words.  

    Test Case:

    Input -> 3 

    Output -> Three 

    3. 

    Take the current month as input from user and print the number of days in that month. 

    Test Case:

    Input -> 3 

    Output -> 31 

  • 8/18/2019 Programing Problems

    7/22

    6

    Loop problems:1.  Write a program to print the alphabets from A-Z. 

    2. 

    Take a number as input from user and print its first 5 multiples. Test Case:

    Input -> 2 

    Output -> 2 * 1 = 2 

    2 * 2 = 4 

    2 * 3 = 6 

    2 * 4 = 8 

    2 * 5 = 10 

    3. 

    Take 10 numbers as input from user and print the sum of odd numbers and the product of even

    numbers. 

    4. 

    Write a program to print all the odd numbers between 1 and 100. 

    5.  Write a program to print all multiples of 3 up to n. Take a number n as input. Test Case:

    Input -> 10 

    Output -> 3 6 9

    6.  Write a program to print all multiples of 3 & 5 up to n. Take a number n as input.Test Case:

    Input -> 10 

    Output -> 3 5 6 9 10

    7.  Write a program to check whether a no is power of two or not?Test Case:

    Input -> 16 

    Output -> Yes (Since, (2)4 = 16) 

    8.  Write a program to check whether a no is power of three or not?Test Case:

    Input -> 27 

    Output -> Yes (Since, (3)3 = 27) 

    9.  Take a number as input from user and print the Factorial of that number.

    Test Case:Input -> 4 

    Output -> 24 

    10. 

    Take a number as input from user and print whether the number is a prime or not.Test Case:

    Input -> 7 

    Output -> Prime 

  • 8/18/2019 Programing Problems

    8/22

    7

    11. Take a number as input from user and print a number which results in highest remainder when

    divided with the inputted number. Test Case:

    Input -> 10 

    Output -> 6 (Since, 10 % 6 = 4) 

    12. 

    Write a program to print all prime number up to a given numbers. 

    Test Case:

    Input -> 15 

    Output -> 2 3 5 7 11 13

    13. 

    Write a program to print the sum of the first 1000 prime numbers. 

    Output -> 3682913

    Take a number as input from user and print whether the number is a perfect number or not.(A

    perfect number is a positive integer that is equal to the sum of its proper positive diviso rs, that is, the sum of its positive divisors excluding the number itself.) 

    Test Case:

    Input -> 6 

    Output -> Perfect Number (Since, 1 + 2 + 3 = 6) 

    14. Write a program to take n as input and print the sum of the series ⁄

    ⁄ … . . ⁄ . 

    15. Write a program to take n as input and print the sum of the series … . . . 

    16. 

    Write a program to take n as input and print the sum of the series … . . . 

    17. Write a program to take n as input and print the sum of the series ⁄

    ⁄ … . .

    +. 

    18. 

    Write a program to take n as input and print the sum of the series !⁄ !⁄ !⁄ … . . !⁄ . 

    19. Write a program to take n as input and print the sum of the series .  !

    +!

      ++!

    … +…+ !

    20. Take two numbers as input from user and print their hcf. ⍟ Test Case:

    Input -> 10 15 

    Output -> 5 

    21. Take two numbers as input from user and print their lcm. ⍟ (Lcm = (n1 * n2)/hcf) Test Case:

    Input -> 10 15 

    Output -> 30 

  • 8/18/2019 Programing Problems

    9/22

    8

    Pattern Problems:Write a program to print the following Patterns.

    1. 

    *

    **

    ***

    ****

    *****

    2. 

    *****

    ****

    ***

    **

    *

    3.  1 

    22 

    333 

    4444 

    55555

    4.  5 

    44 

    333 

    2222 

    11111

    5. 

    55555 

    4444 

    333 

    22 

    1

    6. 

    11111 

    2222 

    333 

    44 

    5

    7.  1 

    12 

    123 

    1234 

    12345

    8.  5 

    45 

    345 

    2345 

    12345

    9. 

    21 

    321 

    4321 

    54321

    10. 

    54 

    543 

    5432 

    54321

    11. 12345 

    1234 

    123 

    12 

    12. 12345

    2345

    345

    45

    5

    13. 

    A

    AB

    ABCABCD

    ABCDE

    14. 

    DE 

    CDE BCDE 

    ABCDE

  • 8/18/2019 Programing Problems

    10/22

    9

    15. 1

    13

    135

    1357

    13579

    16. 13579

    3579

    579

    79

    9

    17. 

    1123

    12345

    1234567

    123456789

    18. 

    111110000

    111

    00

    1

    19. 

    1

    10

    101

    1010

    10101

    20. 

    01010

    0101

    010

    01

    0

    21. 123

    456

    789

    22. 123 

    45 

    6

    23. 

    ** 

    *** 

    **** 

    *****

    24. 

    ***** 

    **** 

    *** 

    ** 

    *

    25.  1 

    21 

    321 

    4321 

    54321

    26.  1 

    12 

    123 

    1234 

    12345

    27. 

    0000000000

    0 0

    0 0

    0 0

    0000000000

    28. 

    1

    2

    3

    4

    5

    29.  *

    ***

    *****

    *******

    *********

    30. *********

    *******

    *****

    ***

    *

    31.  1 ⍟ 234

    56789

    32. 

    1 1 ⍟ 2 2

    3

    4 4

    5 5

  • 8/18/2019 Programing Problems

    11/22

    10

    Loop Problems 2:1.

     

    Take a number as input from user and count the number of digits in that number.Test Case:

    Input -> 12982 

    Output -> 5 

    2. 

    Write a program to reverse an integer. 

    Test Case:

    Input -> 1245 

    Output -> 5421 

    3. 

    Take a number as input from user and print whether that number is a palindrome or not. (A number is

    said to be palindrome if the number remains the same when its digits are reversed.) 

    Test Case:

    Input -> 16461 

    Output -> Palindrome 

    4.  Take a number as input from user and print the sum of the digits in the number. Test Case:

    Input -> 1245 

    Output -> 12 (Since, 1+2+4+5 = 12) 

    5.  Take a number as input from user and print whether that number is a Harshad number or not.  (A

    Harshad number “is an integer that is divisible by the sum of its digits”) 

    Test Case:

    Input -> 81 

    Output -> Harshad Number (Since, 8+1 = 9, and 81/9 = 9) 

    6.  Take a number as input from user and print whether the number is a Armstrong Number.(An Armstrong

    number of three digits is an integer such that the s um of the cubes of its digits is equal to the number itself.) 

    Test Case:

    Input -> 371 

    Output -> True (Since, 33 + 73 + 13 = 371) 

    7.  Take a number as input from user and print whether that number is a Krishnamurthy or not. (It is a

    number which is equal to the sum of the factorials of all its digits.)  

    Input -> 145 

    Output -> Krishnamurthy Number (Since,1! + 4! + 5! = 1 + 24 + 120 = 145) 

    8.  Take a number as input from user and print the 3rd digit of that number.Test Case:

    Input -> 1245 

    Output -> 4 

  • 8/18/2019 Programing Problems

    12/22

    11

    9.  Take a number as input from user and print the sum of the first and last digits in the number. Test Case:

    Input -> 1245 

    Output -> 6 (Since, 1+5 = 6) 

    10. Take a number as input from user and print the the number after removing all the 0 digits. ⍟ Test Case: Test Case:Input -> 100305 Input -> 2050

     

    Output -> 135 Output -> 25 

    11. 

    Write a program to take a number from user and print Fibonacci series upto that number. 

    ⍟⍟ 

  • 8/18/2019 Programing Problems

    13/22

    12

    Array Problems:1.  Take a array of integers as input and print the sum and average of the even numbers in the array. 

    Test Case:

    Input -> 99 98 94 99 89 100 

    Output -> 292 97 

    2.  Take a array of integers as input and print the numbers in the array in reverse order. Test Case:

    Input -> 1 2 3 

    Output -> 3 2 1

    3.  A student wants to find his percentage after exams. Write a program which takes the score of 6

    subjects as input and prints his percentage. Test Case:

    Input -> 99 98 94 99 89 100 

    Output -> 96% 

    4.  Take a array of integers as input and print whether the array contains the number 2. Test Case:

    Input -> 1 5 4 3 

    Output -> Array doesn’t contain element 2. 

    5.  Write a program to take 2 arrays with same length and print whether two of them are equal. 

    6.  Write a program to take a array of integers as input and print largest number in the array.  Test Case:

    Input -> 1 5 4 3 

    Output -> 5

    7.  Write a program to take a array of integers as input and print smallest number in the array.  Test Case:

    Input -> 1 5 4 3 

    Output -> 1

    8.  Write a program to take a array of integers as input and print 2nd largest number in the array. Test Case:

    Input -> 1 5 4 3 

    Output -> 4

    9. 

    Take a number as input and print the frequency of the digits in that number.Test Case:

    Input -> 10013 

    Output -> Digit 0 appears 2 times.

    Digit 1 appears 2 times.

    Digit 3 appears 1 times. 

  • 8/18/2019 Programing Problems

    14/22

    13

    10. Given two arrays, 1,2,3,4,5 and 2,3,1,0,5 write a program to print the common numbers between

    them. Test Case:

    Output -> 1 2 3 5

    11. 

    Given two arrays, 1,2,3,4,5 and 2,3,1,0,5 write a program to print which number is not present in

    the second array. 

    Test Case:

    Output -> 4

    12. Given an array 1,1,2,3,4,2,4,4,7 write a program to count the number of unique elements in the

    array.Test Case:

    Output -> 5 (Unique elements are 1,2,3,4,7)

    13. Given two arrays, 1,2,3,4 and 5,6,7,8 write a program to print the elements alternatingly. Test Case:

    Output -> 1 5 2 6 3 7 4 8

    14. 

    Given an arrays, 1,2,3,4,5,6 write a program to reverse the array without using a temp array. 

    Test Case:

    Output -> 6 5 4 3 2 1

    15. In an array 1-100 numbers are stored sequentially, one number is missing, write a program to

    print the missing number. 

    16. 

    Given an array of number 1,2,1,3,4,5 , where one of the number is repeated. Write a program to

    print the duplicate number. 

    Test Case:

    Output -> 1 

    17. Given an array of number int[] a = {100,150,200,300,350} , write a program to take an number as

    input and print the element closest to that number in the array.  

    Test Case 1: 120 Test Case 2: 160 

    Output -> 100 Output -> 150 

    18. 

    Take an array of integers as input and print the numbers in ascending order. 

    Test Case:

    Input -> 1 5 4 3 

    Output -> 1 3 4 5

    19. 

    Take an array of integers as input and print the numbers in descending order. 

    Test Case:

    Input -> 1 5 4 3 

    Output -> 5 4 3 1

  • 8/18/2019 Programing Problems

    15/22

    14

    20. Given an array 1,2,50,9, write a program to arrange them such that they form the largest possible

    number and print that number. ⍟ Test Case:

    Output -> 95021 

  • 8/18/2019 Programing Problems

    16/22

    15

    String problems:1.  Take a string as input from user and print whether the string starts with "a".  

    Test Case:

    Input -> Apple 

    Output -> String starts with letter a. 

    2.  Write a program to print the first half of a given string. Test Case:

    Input -> ABCDEF 

    Output -> ABC 

    3.  Take a string as input from user and print the number of words in a string.Test Case:

    Input -> I am a Boy 

    Output -> 4

    4. 

    Write a program to print the number of occurrence of the word "Hello" in the given String. 

    5.  Take a string as input from user and print whether the string contains only uppercase or only

    lowercase or mixed case letters.Test Case:

    Input -> I am a Boy 

    Output -> I.A.A.B

    6.  Take a string as input from user and print the first letter in capital of each word separated by ‘.’ in

    the string.Test Case:

    Input -> I am a Boy Output -> I.A.A.B

    7.  Take a string as input from user and translate the text to Pig Latin. English is translated to Pig Latin

    by taking the first letter of every word, moving it to the end of the word and adding ‘ay’.

    Test Case:

    Input -> The quick brown fox 

    Output -> Hetay uickqay rownbay oxfay

    8.  Write a program to reverse a given string without using reverse() function. Test Case:

    Input -> ABCDE 

    Output -> EDCBA 

    9.  Take a string as input from user and print whether it is palindrome or not.(A palindrome is a word, p hrase,

    number, or other sequence of characters which reads the same backward or forward. ) 

    Test Case:

    Input -> lol 

    Output -> Palindrome 

  • 8/18/2019 Programing Problems

    17/22

    16

    10. Write a program to remove a given word in a string. 

    11. Write a program to print the number of characters common between two given Strings. 

    12. Write a program to take an array of strings as input and print them in alphabetical order.  ⍟ Test Case:

    Input -> {“Duke”,”Andy”,”Andrew”,”Mack”} 

    Output -> Andrew 

    Andy 

    Duke 

    Mack 

    13. 

    Write a program to print first non repeated character of a given String. 

    Test Case:

    Input -> aaaaabcaaaa 

    Output -> b 

    14. 

    Write a program to print the longest substring without repeating characters. 

    ⍟ 

    Test Case:

    Input -> aaaaabcaaaa 

    Output -> abc 

    15. Write a program to print the longest substring which is a palindrome. ⍟ Test Case:

    Input -> abcabbabb 

    Output -> abba 

    16. Take two strings as input from user and print whether they are Anagram.  ⍟ 

  • 8/18/2019 Programing Problems

    18/22

    17

    Matrix Problems:1.  Write a program to check whether two matrices are equal.

    2. 

    Write a program to input a matrix and print whether the matrix is a Sparse Matrix.  .(A Sparse Matrix is onewhere the majority of the elements of the matrix is zero) Test Case:

    Input ->0 0 11 0 0

     

    Output -> Given Matrix is Sparse Matrix 

    3.  Write a program to input 2 matrices of same size and calculate their sum and print the resultant

    matrix.

    4. 

    Write a program to input 2 matrices of same size and calculate their difference and print the

    resultant matrix.

    5.  Write a program to input a matrix and calculate the sum of each row.Test Case:

    Input ->1 2 11 3 2

     

    Output -> Sum of 1st row: 4

    Sum of 2nd row: 6 

    6. 

    Write a program to input a matrix and calculate the sum of each column.Test Case:

    Input ->1 22 3

     

    Output -> Sum of 1st column: 3Sum of 2nd column: 5

    7.  Write a program to input a matrix and calculate the trace of that matrix..(Trace of a matrix is the sum of the

    diagonal elements of that matrix) Test Case:

    Input ->1 22 3

     

    Output -> 4 (Since, 1 and 3 are diagonals and, 3 + 1 = 4)

    8. 

    Write a program to input a matrix and calculate the sum of opposite diagonals of the matrix.Test Case:

    Input ->1 23 3

     

    Output -> 5 (Since, 2 and 3 are opposite diagonals)

  • 8/18/2019 Programing Problems

    19/22

    18

    9.  Write a program to input a matrix and print whether the matrix is a Identity Matrix.  .(A Identity Matrix is a

    square matrix where the diagonals elements are 1 and the other elements are 0) Test Case:

    Input ->1 00 1

     

    Output -> The given matrix is a Identity Matrix.

    10. Write a program to input a matrix and reverse the rows of a matrix and print the resultant matrix.Test Case:

    Input ->1 2 34 5 6

     

    Output ->3 2 16 5 4

     

    11. Write a program to input a matrix and reverse the columns of a matrix and print the resultant

    matrix.Test Case:

    Input ->1 2 34 5 6

     

    Output -> 4 5 61 2 3 

    12. Write a program to input a matrix and print the transpose of that matrix.Test Case:

    Input ->1 2 34 5 6

     

    Output ->

    1 42 53 6

     

    13. Write a program to input a matrix and sort the rows of a matrix in ascending order and print the

    resultant matrix.Test Case:

    Input -> 5 2 17 5 6

     

    Output ->1 2 55 6 7

     

    14. Write a program to input a matrix and sort the columns of a matrix in ascending order and print

    the resultant matrix.Test Case:

    Input ->5 6 77 5 6

     

    Output ->5 5 67 6 7

     

    15. 

    Write a program to input 2 matrices of same size and calculate their product and print theresultant matrix. ⍟ 

  • 8/18/2019 Programing Problems

    20/22

    19

    Date & Time:1.  Write a program to print the current date and time. 

    2. 

    The time in South Africa runs ahead of UTC by 2 hours. Write a program to print the current dateand time in Africa. 

  • 8/18/2019 Programing Problems

    21/22

    20

    Simple Problems 2:1.  Write a program to print 100 random numbers between 12 and 24. 

    2. 

    Write a program to print either “Hi” or “Yo” having a chance of 40% and 60% to print respectively. 

    3.  Write a program to convert a lowercase char to uppercase without using .toLowerCase().Hint:

    Subtract/Add to Ascii Code

  • 8/18/2019 Programing Problems

    22/22

    21

    STL Problems:1.  Write a program to remove all duplicate integers in a Vector.

    Test Case:

    Before ->{ 1 2 2 3 5 2 3}

    After -> {1 2 3 5} 

    2.  Write a program to remove the elements in a vector alternatingly.Test Case:

    Before ->{ 1 2 3 4 5 6}

    After -> {1 3 5}