Professional Documents
Culture Documents
IPS-C-1
We need to swap the value of A and B, debug the swap function so that value of A and
B are swapped.
Input:
10
Output:
10
Solution:
IPS-25-Revision-C-Q1
Little Bob loves chocolate, and he goes to a store with Rs. N in his pocket. The price of
each chocolate is Rs. C. The store offers a discount: for every M wrappers he gives to
the store, he gets one chocolate for free. This offer is available only once. How many
chocolates does Bob get to eat?
Input:
Output:
12
Input:
Rupees in hand
Price of each chocolate
Number of wrappers to get free chocolate
Output:
print the number of chocolates get to eat
Processing Involved:
total of chocolates=(rupees in hand/price of each chocolate)+(price of each
chocolate/number of wrappers to get free chocolate)
Algorithm/ Pseudocode:
Step 1: Accept the inputs from the user
Step 2: total of chocolates=(rupees in hand/price of each chocolate)+(price of each
chocolate/number of wrappers to get free chocolate)
Step 3: Print the total number of chocolates get to eat
Code:
#include<stdio.h>
int main()
{
float N,C;
int M,chocolates;
scanf("%f",&N); //rupees in hand
scanf("%f",&C); //price of each chocolate
scanf("%d",&M); //number of wrappers to get free chocolate
chocolates=(N/C)+(C/M); //number of chocolates to eat
printf("%d",chocolates);
return 0;
IPS-25-Revision-C-Q2
ABC company Ltd. is interested to computerize the pay calculation of their employee in
the form of Basic Pay, Dearness Allowance (DA) and House Rent Allowance (HRA). DA
and HRA are calculated as certain % of Basic pay(For example, DA is 80% of Basic
Pay, and HRA is 30% of Basic pay). They have the deduction in the salary as PF which
is 12% of Basic pay. Propose a computerized solution for the above said problem.
( Basic Pay + ( Basic Pay * 0.8) + ( Basic Pay * 0.3) - ( Basic Pay * 0.12) )
Output : Salary
Input:
5000
Output:
9900.00
Input:
Basic pay
Output:
print the salary of the employee
Processing Involved:
da=basic pay*0.8
hra=basic pay*0.3
pf=basic pay*0.12
Salary=basic pay+da+hra-pf
Algorithm/ Pseudocode:
Step 1: Accept the input from the user
Step 2: Calculate the dearness allowance, da=basic pay*0.8
Step 3: Calculate the house rent allowance, hra=basic pay*0.3
Step 4: Calculate the pf, pf=basic pay*0.12
Step 5: Salary=basic pay+da+hra-pf
Step 6: Print the salary of the employee
Code:
#include<stdio.h>
int main()
{
int basic_pay;
float da,hra,pf,salary;
scanf("%d",&basic_pay);
da=basic_pay*0.8; //amount of dearness allowance
hra=basic_pay*0.3; //amount of house rent allowance
pf=basic_pay*0.12; //amount of pf
salary=basic_pay+da+hra-pf; //salary of the employee
printf("%.2f",salary); //print upto 2 decimal places
return 0;
IPS-25-Revision-C-Q3
A university is setting up a new lab at their premises. Design an algorithm and write a C
code to determine the approximate cost to be spent for setting up the lab. Cost for
setting the lab is sum of cost of computers, cost of furniture and labour cost. Use the
following formulae for solving the problem:
Cost of computer = cost of one computer * number of computers
Cost of furniture = Number of tables * cost of one table + number of chairs * cost of one
chair
Input:
20 // Cost of 1 Table
10 // Cost of 1 Chair
Output:
710
Input:
Cost of one computer, table and chair
Number of computer, chair and table
Number of hours worked by labour
Wage per hour to the labour
Output:
print the total cost required to set up the lab
Processing Involved:
Total cost= (cost of one computer*number of computers)+(cost of one chair*number of
chairs)+(cost of one table*number of tables)+(number of hours worked by labour*wage
per hour)
Algorithm/ Pseudocode:
Step 1: Accept the input from the user
Step 2: Total cost= (cost of one computer*number of computers)+(cost of one
chair*number of chairs)+(cost of one table*number of tables)+(number of hours worked
by labour*wage per hour)
Step 3: Print the total cost required to set up the lab
Code:
#include<stdio.h>
int main()
{
int cost_of_comp,cost_of_table,cost_of_chair;
int no,hours_worked,wage_per_hour,total;
scanf("%d",&cost_of_comp); //cost of one computer
scanf("%d",&cost_of_table); //cost of one table
scanf("%d",&cost_of_chair); //cost of one chair
scanf("%d",&no); //number of computer,chair and table
scanf("%d",&hours_worked); //number of hours worked by
labour
scanf("%d",&wage_per_hour); //wage per hour to the labour
total=(cost_of_comp*no)+(cost_of_table*no)+(cost_of_chair*no)+(h
ours_worked*wage_per_hour);
printf("%d",total); //print the total cost for setting the
lab
return 0;
IPS23-C-Q2
Write a program to implement a function to count the number of digit in a number using
recursion:-
Input1:
123
Output1:
Input2:
5467
Output2:
Input:
Accept a number
Output:
count the number of digits
Processing Involved:
Using recursion, count the number of digits in a number
Algorithm/ Pseudocode:
Step 1: Accept a number from the user
Step 2: Using recursion count the number of digits in a number
Step 3: Print the count of digits
Code:
#include<stdio.h>
int count(int n)
{
if(n==0)
{
return 0;
}
else
{
return 1+count(n/10); //counting the number of digits
using recursive function
}
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",count(n)); //calling the function
return 0;
}
IPS19-C-Q3
a -> addition
s -> subtraction
m -> multiplication
d -> division
r -> remainder
Input:
The two numbers and a character
Output:
Sum , difference,multiplication, division or remainder on dividing two numbers
depending on the character entered by the user
Processing Involved:
Storing the character entered by the user in a variable and then comparing it with
a,s,m,d and r till a match is found and printing the sum , difference , product ,remainder
, quotient accordingly.
Algorithm/Pseudocode:
Step1: Enter the two numbers
Step2: Enter the character
Step3: Compare the character with a,s,m,d,r and print the result corresponding to it
Step4: If no such case is found, the output will be according to the default case , that is
“No such case”.
Step5 : Stop
Code:
#include <stdio.h>
int main()
{
int a,b;
char e;
scanf(" %d",&a); // accepting first number
scanf(" %d",&b); //accepting second number
scanf(" %c",&e); //accepting the character
switch(e)
{
case'a':
printf(" %d",a+b); //addition
break;
case's':
printf(" %d",a-b); //subtraction
break;
case'm':
printf(" %d",a*b); //multiplication
break;
case'd':
printf(" %d",a/b); //division
break;
case'r':
printf(" %d",a%b); //remainder
break;
default:
printf("No such case");
break;
}
return 0;
}
IPS20-C-Q1
Input1:
12
30
Output1:
60
Input :
The two numbers
Output:
Gcd and lcm
Processing involved :
repeated division till the remainder obtained is 0 to find gcd
the formula gcd*lcm=product of numbers to find lcm
Algorithm/Pseudocode:
Step1 :enter the two numbers
Step2: if first number is greater , assign a variable ,num to it , otherwise assign a
variable to another number
Step3: divide num obtained in the previous step by the other number till the remainder is
0
Step4: take the divisor of the last step as gcd
Step5: use the formula gcd*lcm=product of two numbers to find the lcm
Step6: print the gcd and lcm
Step7: Stop
Code:
#include <stdio.h>
int main()
{
int num1,num2,gcd,lcm,remainder,num,den;
scanf("%d",&num1); //accepting first number
scanf("%d",&num2); //accepting second number
if (num1>num2){
num=num1;
den=num2;
}
else{
num=num2;
den=num1;
}
remainder=num%den;
while (remainder!=0)
{
num=den;
den=remainder;
remainder=num%den; //repeated division to find gcd
}
gcd=den;
lcm=(num1*num2)/gcd; /* formula:lcm*gcd=product of
numbers,to find lcm */
printf("%d\n",gcd); // print the gcd
printf("%d\n",lcm); // print the lcm
IPS19-C-Q1
Write a program that will accept two values from the user and implement the following:-
Input:
Addition: 25
Subtraction: 12
Multiplication: 300
Division: 2
Remainder: 1
PAC:
Input :
Output:
Processing:
Arithmetic operations
Algorithm:
Solution
#include <stdio.h>
int main()
{
int a,b;
scanf("%d",&a);
scanf("%d",&b);
printf("Addition: %d\n",a+b);
printf("Subtraction: %d\n",a-b);
printf("Multiplication: %d\n",a*b);
printf("Division: %d\n",a/b);
printf("Remainder: %d\n",a%b);
return 0;
}
IPS19-C-Q2
A : >= than 85
Read the score from the user and implement a C program using if..else ladder to print
the appropriate grade of the student.
PAC:
Input :
Output:
Processing:
Algorithm:
if (score>=85) Print A
Solution:
#include <stdio.h>
int main()
{
int score;
scanf("%d",&score);
if (score >=85)
{
printf("A");
}
{
printf("B");
{
printf("C");
{
printf("D");
}else
{
printf("Try again");
}
return 0;
}
IPS 23 - C - Q1
PAC:
Output: Check if number is odd or even and print the reverse of the number
Algorithm:
1. Accept a number
6. assign s=0
remainder = b % 10
s= s*10 + remainder
assign b= b/10
8. print s
Code:
#include<stdio.h>
int evenodd(int a)
if (a%2==0)
printf("%s\n","Even");
else
printf("%s\n","Odd");
int reverse(int b)
int s=0;
while (b!=0)
printf("%d",s);
int main()
int n;
scanf("%d",&n);
evenodd(n);
reverse(n);
}
IPS23 -C -Q3
PAC:
else if number1>number2
else if number2>number1
Code:
#include<stdio.h>
printf("%d",a);
gcd(a-b,b);
gcd(a,b-a);
}
int main()
int n1,n2;
scanf("%d\n%d",&n1,&n2);
gcd(n1,n2);
IPS20-C-Q2
Write a C Program to implement fibonacci series upto n terms, where n is read
from the user?
Input:
Output:
Input:
number of fibonacci series
Output:
fibonacci series upto given number
Processing Involved:
addition and looping
Algorithm/ Pseudocode:
step 1: declare the 5 variable and give 0 and 1 for two variable
step 2: accept the number of fibonacci numbers
step 3: start a for loop
step 4: start a if loop with the condition if the term1 which is given the value is equal to
zero
step 5: print the 0 first and increase it by 1 and continuously print the fibonnacci series
value until the series number given
Code:
#include <stdio.h>
int main()
{
int i,n,term1=0,term2=1,next;
scanf ("%d",&n);
for (i=1;i<=n+1; ++i)
{
if (term1==0);
else
printf("%d\n",term1);
next=term1+term2;
term1=term2;
term2=next;
}
IPS21-C-Q1
A cricket selection committee decide to retain a batsman for the next tournament based
on the following:-
Design a program to read the number of matches the player played, for each matched
played read the run scored in one array and the ball faced in another and performed the
above calculation.
Input1:
5 // Number of matches
Output1:
50 // Batting average
96 // Strike rate
Selected
Input2:
5 // Number of matches
Output2:
36 // Batting average
94 // Strike rate
Not Selected
Input:
no of matches
Runs scored in each match
balls faced in each match
Output:
batting average
strike rate
selected or not selected
Processing Involved:
iteration
Selection
Addition
Multiplication
Algorithm/ Pseudocode
Step 1: Accept the number of matches
Step 2: Accept the runs scored and balls faced in separate for loops simultaneously
take the sum
Step 3: Calculate and print batting average as total runs scored/no of matches
Step 4: Calculate and print strike rate as total runs scored/total no of balls faced
Step 5: Using an if statement print 'Selected' if batting average>=50 and strike rate>=90
else 'not selected.
Code:
#include <stdio.h>
int main()
{
int m,a,ba,sr;
float r,c=0;
scanf("%d",&m);
int run[m];
int balls[m];
for (int i=0;i<m;i++)
{
scanf("%d",&a);
run[i]=a;
r+=a;
}
IPS21-C-Q2
Write a C program that will perform the following:-
3. Print the character along with the count, in which the character that has the most
count gets printed first.
Input1:
Output1:
a 3
t 2
b 1
i 1
g 1
f 1
c 1
Input2:
apple
Output2:
p 2
a 1
l 1
e 1
Input:
A string
Output:
counts of various characters in the string
Processing Involved:
iteration
Selection
addition
Algorithm/ Pseudocode:
Step 1: Make a structure which contains a character and an integer
Step 2: Make a function for sorting using the integer part of the structure
Step 3: printing the character and its count only when count>0
Step 4: Inside the main function accept the string
Step 5: Make an array of structures and get the counts of each character
Step 6: Call the 2 functions
#include <stdio.h>
#include <string.h>
struct str
{
char c;
int f;
};
void insertionSort(str l[], int n)
{
int i, j;
str temp;
for (i = 1; i < n; i++)
{
temp = l[i];
j = i - 1;
while (j >= 0 && l[j].f < temp.f)
{
l[j + 1] = l[j];
j = j - 1;
}
l[j + 1] = temp;
}
}
void print(str l[], int n)
{
int i;
for (i = 0; i < n; i++)
{
if(l[i].f!=0)
{
printf("%c %d\n", l[i].c,l[i].f);
}
}
}
int main()
{
char s[100],c;
int i,count;
fgets(s,100,stdin);
str l[26];
for (int j=0; j<26; j++)
{
l[j].c='a'+j;
count=0;
for(i=0;s[i];i++)
{
if(s[i]==l[j].c)
{
count++;
}
}
l[j].f=count;
}
insertionSort(l,26);
print(l,26);
return 0;
}
IPS22-C-Q1
Write a C program to check if a given matrix is a sparse matrix or not?
Example
= [0, 0, 2,
1, 0, 0,
0, 0, 0]
Input:
3 // Number of rows
2 // Number of columns
Output:
Sparse
Input:
Output:
Not Sparse
Input:
Number of rows
Number of columns
Output:
Total number of elements in rows and columns
Algorithm:
Step1: Read number of rows
Step2: Read number of columns
Step3:t=0
Step 4:m=0
Step5: Start a for loop
Step6: Read x
Step7:if (x==0)
t=t+1
Else
m=m+1
Step 8:if (t>m)
print(“Sparse”)
Else
print(“Not sparse”)
Code:
#include<stdio.h>
int main()
{
int m,n,x,t=0,s=0,i;
scanf("%d%d",&m,&n);
for(i=0;i<m*n;i++)
{
scanf("%d",&x);
if (x==0)
{
t+=1;
}
else
{
s+=1;
}
}
if (t>s)
{
printf("Sparse");
}
else
{
printf("Not Sparse");
}
}
IPS22-C-Q2
Write a C program to check if all rows and columns in a 2-dimensional matrix have the
same sum.
Example:
[0, 3, 0,
2, 0, 1,
1, 0, 2]
Input:
3 // number of rows
3 // number of columns
Output:
Yes
Input:
3
0
Output:
No
Input:
Number of rows
Number of columns
Total number of elements in the matrix
Output:
Yes if sums of elements in each row is same
No if sum of elements in each row is not the same
Algorithm:
Step1:Read the number of rows of the matrix m
Step2: Read the number of columns of the element n
Step 3:Create a matrix l[10][10]
Step4: Read the number of elements in the matrix
Step5:for(i=0;i<m;i++)
Step6: for (i=0
Step6:Read x
Step7:Store its value in position l[i][j]
Step 8:
end
end
Step 9: sum,sum1=0,0
Step10:Start a loop through the rows of the matrix
sum=sum+element of that loop
Step 11:’ Start a loop through the columns of the matrix
sum1=sum1+elements of that loop
Step 12:If (sum=sum1)
Print Yes
Else
Print No
Step 13: End
Code:
#include<stdio.h>
int main()
{
int i,j,rows,columns,a[10][10],Sum,Sum1;
scanf("%d %d", &i, &j);
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = 0;
for(columns = 0;columns < j; columns++)
{
Sum = Sum + a[rows][columns];
}
}
for(rows = 0; rows < i; rows++)
{
Sum1 = 0;
for(columns = 0;columns < j; columns++)
{
Sum1= Sum1+a[columns][rows];
}
}
if (Sum==Sum1)
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
IPS-C-2
Write a C program to perform the following?
3. Asked from user for a number (say n) between 1 to 50 (i.e 1<n<50). Check for
boundary conditions?
5
Output1:
1 1 2 3 5
120
Input2:
0
Output2:
Invalid
Solution:
Input:
A Number "n" from user
Output:
Fibonacci series till "n" terms
Factorial of "n"
Processing Involved:
Array Initialization, Array Declaration, Looping, Iteration, Recursion, Functions, Recursive
Functions, Addition, Comparison Operation, Multiplication, Subtraction, Logical and
operator
Solution Alternatives:
Without using Functions
Algorithm:
1. Accept a number "n" from the user
2. Declare an array and store the first two values as 1 and 1
3. Define a function fibb(n) such that it calculates the fibonacci series of the numbers
upto "n" terms. The algorithm for fibb(n) is as follows:
fibb(n)
{
t1=0, t2=1
for i = 1 to n: do
{
ser[i]=t1
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
i=i+1
}
for i = 1 to n: do
{
print ser[i] with a newline
i+=1
}
}
4. Define a recursive function factorial (n) to compute factorial of n. The pseudo code is
as follows
factorial(n)
{
if n less than or equal to 1:
print (n*factorial(n-1))
else:
print 1
}
4. if n is greater than or equal to 1 and less than or equal to 50 go to step 3 and 4 and
print fibb(n) and factorial(n)
5. Else print Invalid Number
Code:
#include<stdio.h>
int fibb(int n)
{
int nextTerm,t1 = 0,t2 = 1,i,ser[50];
for (i = 0; i <= n; ++i)
{
ser[i] = t1;
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
for(i=1;i<=n;i++)
{
printf("%d, ",ser[i]);
}
}
int factorial(int n)
{
if(n>=1)
return(n*factorial(n-1));
else
return(1);
}
int main()
{
int arr[50];
arr[0] = 1;
arr[1] = 1;
int n;
scanf("%d",&n);
if(n>=1 && n<=50)
{
fibb(n);
int f = factorial(n);
printf("\n%d",f);
}
else
printf("Invalid");
IPS20-C-Q3
Write a C program to check if a given number is an Armstrong number?
Input1:
153
Output1:
Yes
Input2:
111
Output:
No
Input:
Output:
Processing Involved:
addition, looping
Algorithm/ Pseudocode:
step 2: start a while with a condition that the given number is not zero
step 5: start another while loop and a nested for loop to add all the values from 1 to
the value given
step 8: if the number obtained is equal to the number given print yes else no
Code:
#include <stdio.h>
int main()
scanf("%d",&num);
real=num;
copy=num;
while (num != 0)
{
num /=10;
++count;
while (real != 0)
res1 = 1;
rem=real%10;
res1 *= rem;
res += res1;
real /= 10;
if (res == copy)
printf("Yes");
else
printf("No");
return 0;
}
IPS 22 -C -Q3
Given the board configuration of the tic tac toe game, determine if the board is in either of the
following states: empty, player1 wins, player2 wins, draw or intermediate. The board is said to
be in initial state if all the cells contain ‘-1’, player1 uses ‘1’ as his coin and player2 uses ‘2’ as
his coin. The game is draw when the board is full and no one has won the game. The game is
in intermediate state when no one has won and board is not full.
Input1:
-1
-1
-1
-1
-1
-1
-1
-1
-1
Output1:
Initial
Input2:
-1
-1
1
1
2
2
2
1
1
Output2:
Intermediate
Input3:
1
2
1
1
2
2
2
1
1
Output3:
Draw
Input4:
1
1
2
1
2
2
1
2
1
Output4:
1 wins
Input5:
1
2
2
1
2
1
2
1
1
Output5:
2 wins
Solution:
Input:
Current board configuration
Output:
State of the board as win, draw,initial or intermediate
Processing Involved:
Average, Difference, Multi Dimensional Arrays and its operations, addition, Comparison,
Logical operators, break statements, looping statements, iteration
Solution Alternatives:
Using Functions
Algorithm:
•Represent the board in memory
•Get the elements in first row, second row and so on
•Process the elements
•If all are -1 then print ‘empty’
•If ‘1’ is placed row wise, column wise or diagonally then print ‘Player 1’ wins
•If ‘2’ is placed row wise, column wise or diagonally then print ‘Player 2’ wins
•If all cells are full and no one has won the game then print ‘Draw’
•Otherwise print intermediate
Coding:
IPS18-C-Q1
Write a C program to perform addition of two numbers read from the user?
Input:
20
10
Output:
30
Solution:
Input:
Numbers to add
Output:
Sum of the numbers
Processing Involved:
Creating integer variables a and b,
Taking input from user and storing 2 numbers in a and b
Printing sum of values stored in a and b
Algorithm:
Step1: Create int variables a and b
Step2: Take input from user and store it in a and b
Step3: Print sum of a and b
Coding:
#include<stdio.h>
int main()
{
int a,b;
scanf("%d",&a);
scanf("%d",&b);
printf("%d",a+b);
return 0;
IPS18-C-Q2
A square playground field has to be converted into a circular playground. The authorities
decided to construct the circular ground inside the square field so that it covers
maximum area. Given the area of the square ground, write a C program to find the area
of the circular field. Consider pi= 3.14
Input:
64
Output:
50.24
Solution:
Input:
Area of square field
Output:
Area of circular field
Processing Involved:
Creating integer variable and float variable
Taking input from user
Arithmetic operations
Printing formatted output
Algorithm:
Step1: Create integer variable a and float variable b
Step2: Take input from user and store it in a
Step3: Calculate area of circular field in b
Step4: Print b formatting it to 2 decimal points
Coding:
#include<stdio.h>
int main()
{
int a;
float b;
scanf("%d",&a);
b=3.14*0.25*a;
printf("%.2f",b);
return 0;