You are on page 1of 9



Advanced Problem Solving Technique

Date: January 16th, 2012

5. m. the marks of the students. Read the variables for number of students. } else printf(“ student is fail:”). and set count to 0 3. Stop Program: # include <studio.&n).44. i<=100.88. 6. Example: Let the mark obtain by student are 30. of students:”).90. getch. printf(“ Enter the no. The students who has scored more than equal to 50 is 6. scanf(“%d”. for(i=0. Apply if – else loop 4.43. make a count of number of student passed the examination. A pass is awarded for all marks of 50 and above. Start 2.Problem 1 : Given a set of n students examination mark in the range of 0 to 100. scanf(“%d”. of marks:”). Algorithm 1. count=0. i. If the mark in step 2 is greater than equal to 50 then increase the counter by 1 and print pass statement. If mark is less than 50. count++.i++) { printf(“ Enter the no.70. if (m>=50) { printf(“ student is pass:”).54.&m).h> void main { int n.50.52. . print fail statement.

Define the variable to find the factorial 3. Start 2.g. &num). Define the main body 5. equals 1. Pass the arg to step 3 to compute factorial till input number 7.h> long int factorial (int n) { if (n==1) return (1). Print the required solution 8. Define the call function i. If no. printf(“Enter a no to compute factorial”). scanf(“%d”.factorial(num)). Example: Let the nth term of factorial is 6 then it’s value should be 6 x5x4x3x2x1 = 720 Algorithm 1. compute n factorial ( written as n! ) when n >=0. else return (n*factorial(n-1)).Problem 2: Given a number n. } . getch(). } void main( ) { int num. 6 6. Read the number to compute factorial e. then return the value 1 otherwise return n*factorial(n-1) 4. printf(“The factorial is %d”. Stop Program: # include <studio.

i<=terms.h> int fib(int n) { if( n==1) return 0. &terms). 7. Stop Program: # include <studio.5 Algorithm 1. else return( fib(n-1)+fib(n-2)). Define the main body 5. Return value as 1 iii. return value as 0 ii. Run the loop by calling the step 3 to compute the Fibonacci series and stop if the no is greater than loop value. . Define the return type 3. to compute Fibonacci series 6.Problem 3: Generate and print nth term of Fibonacci sequence when n>=1 Example: Let the nth term of Fibonacci is 5 then it’s value should be 4 from its sequence as 0. If no enter is 1. scanf(“%d”. printf(“how amnt terms do you need?”). for(i=1.1.return third value as the sum of nearest predecessors as sum= fib(n-1) + fib(n-2) 4. } void main( ) { int terms. If no is more than three .factorial(i)). getch(). else if (n==1) return1.3.2. Enter the no. Define the calling function i.1. Start 2.i++) printf(“The Fibonacci is %d”. If no enter is 2.

Problem 4: Design algorithm that accepts a positive integer and reverse it order of its digit Example: Let the number be 4562 then its reverse is 2654 Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10. is %d". while(num > 0) { rev_num = rev_num*10 + num%10. printf("Reverse of no. } .h> int reversDigits(int num) { int rev_num = 0. return 0. num = num/10. getchar(). } int main() { int num = 4562. reversDigits(num)). (b) Divide num by 10 (3) Return rev_num Program: #include <stdio. } return rev_num.

i. This is 3.h> index = 0 . Divide 13 by 2. and go back to to of loop. d2=1. Give the number to find the binary equivalent 3. } . i. Print the result 6. d3=1. we store the remainder in the array. we again divide by 2. // assume K > 1 num = num / K . We store the remainder in the array. We store the remainder in the array. So. our value is num. d[index]=1 where index=0.. Calculate the modulus value divided by base K and integer value divided by kof entered value 4. we're means that d0=1. At this point. with a remainder of 1. Increment index to 2. so we're done. with a remainder of 1. Equivalently. Since 1 is not 0. Declare variable in array 2. Increment index to 1. This is 0. Print the modulus 5.Problem 5: Convert a decimal integer to its binary representation. // integer division digit[ index ] = remainder . Since 3 is not 0. This is 1. Basically.float digit[50].. we again divide by 2. Since 6 is not 0. while ( num != 0 ) { remainder = num % K . i. and go back to to of loop..e. d[index]=1 where index=2.e. This is 6 with a remainder of 1. index ++ . We store the remainder in the array. Program: the pseudoProgram for converting from base 10 to base K // num stores a value in base 10 // solution will have digits in an array #include <stdio. Equivalently. d[index]=1 where index=3. Increment index to 3. and go back to to of loop.. with a remainder of 0. Equivalently. Example: Let the decimal number be 13 then its binary number is 1101 Algorithm 1.e. we again divide by 2. d1=0. Increment index to 4. i. d[index]=0 where index=1. Repeat step 3 and 4 till integer value is greater than zero           Let index=0. and go back to to of loop.e.

n-1)). Algorithm 1. s) \* recursive function to sum all the terms*/ { if (n==1) return x.n-2.s) finds the sum of all the terms in the series. Print the result Program: #include<stdio. Compute(x. int n) \* recursive function to calculate xn/ { if (n==1) return x.n)/fact(n)+compute(x. else return(x*numerator(x.Problem:6 Design an algorithm to evaluate the function sin(x) as defined by the infinite series expansion Sin(x) = x/1! – x3/ 3! + x5/5! – x7/7! + …………. int n. } float numerator (float x. Read the number of terms n and the value x in main 3. Start 2.h> long int fact(int j) \*recursive function to calculate factorial*/ { if(j==0) return 1. Recursive function compute() call other recursive function fact() with argument the index of each term that return denominator(factorial) of the term 6..-1*s)). else return(s*numerator(x. } float numerator (float x. Recursive function compute() call other recursive function numerator() with arguments x and index of each term that returns numerator of the term 5. . Call the recursive function compute() from main with argument x and n 4. else return (j*fact(j-1)).2*n-1.

scanf(“%d”. of terms:”).&n). scanf(“%”f.s)). } . printf(“sum of terms:%f”.14/180 /* to convert degree in radian*/ if(n%2==0) /* if the no of terms even. printf("Enter the value of angle in degree:”). float x.&x).} void main(void) { int n. printf("Enter the no. else s=1.sign begins from negative*/ s=-1.s.2*n-1. x= x*3.compute(x.

Print the value Program: { int i. } . char Input [123]. i = atoi (Input). printf ("Enter a number: ").Problem:7 Given the character representation of a integer convert it to its conventional decimal format Algorithm: 1.Define and Initialize variables 2. Calculate the ascii to integer function 4. Enter the string number to convert in conventional decimal format 3. gets ( Input ). return 0. printf ("Value entered is %d).