You are on page 1of 48

IPS - C - SOLUTIONS - GREEN TEAM

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:

100.00 // Rs 100 in hand

10.00 // Price of each chocolate

5 // Number of wrappers to get free chocolate

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.

Input : Basic Pay

Process : Calculate Salary

( Basic Pay + ​( Basic Pay * 0.8) + ( Basic Pay * 0.3) ​- ​( Basic Pay * 0.12) ​)

-----------allowances --------------​ ​--- deductions----

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

Labour cost = number of hours worked * wages per hour

Input:

100 // Cost of 1 Computer

20 // Cost of 1 Table

10 // Cost of 1 Chair

5 // Number of computer= Number of chair=Number of table

15 //Number of hours worked

4 // Wage per hour

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

Write a C program to implement a calculator based operation using switch statement:-

Where the different cases are:-

a -> addition

s -> subtraction

m -> multiplication

d -> division

r -> remainder

default: No such case

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

Write a C Program to find the GCD and LCM of two number?

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:-

a) Arithmetic operation (+, -, *, /, %)

b) Print the results

Input:

24 // 1st value entered

12 // 2nd value entered


Output:

Addition: 25

Subtraction: 12

Multiplication: 300

Division: 2

Remainder: 1

PAC:

Input :

Accept two numbers

Output:

Print the sum,difference,product Division and remainder of the two numbers

Processing:

Arithmetic operations

Algorithm:

Step1: Accept two numbers a and b

Step2: print the following

Sum=a+b, diff=a-b product=a*b, div=a/b,remainder=a mod b

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 student is grade according to his/her score. The grading is as follows:-

A : >= than 85

B : < 85 and >=70

C : < 70 and >=60

D : < 60 and >=45

Else: Try again

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 :

Accept the score from user

Output:

grade of the student

Processing:

condition statement (if...else)

Algorithm:

Step1: Accept the score from user

Step2: Check using if condition

if (score>=85) Print A

else if (score >=70) Print B

else if (score >=60) Print C

else if (score >=45) Print D

Else: print("Try again")

Solution:

  

#include <stdio.h> 

int main() 

int score; 
scanf("%d",&score); 

if (score >=85) 

  printf("A"); 

else if (score >=70) 

  printf("B"); 

}else if (score >=60) 

  printf("C"); 

}else if (score >=45) 

  printf("D"); 

}else 

  printf("Try again"); 

return 0; 

 
IPS 23 - C - Q1

PAC:

Input:​ Accept a number

Output:​ Check if number is odd or even and print the reverse of the number

Processing Involved:​ Through functions, selection and iteration

Solution Alternatives:​ Without using functions

Algorithm:

1. Accept a number

2. Define a function to find even or odd

3. If number divided by 2 is 0 print even

4. else print odd

5. Define a function to reverse the number

6. assign s=0

7. while b not equal to 0

remainder = b % 10
s= s*10 + remainder

assign b= b/10

8. print s

9. call functions and pass the input value

Code: 

#include<stdio.h>

/* defining function to calculate even or odd*/

int evenodd(int a)

if (a%2==0)

printf("%s\n","Even");

else

printf("%s\n","Odd");

/* function to calculate reverse of the number*/

int reverse(int b)

int s=0;

while (b!=0)

int r=b%10;/*remainder of number when dividedby10*/

s=s*10+r; /*multiplying s with 10 and adding remainder


to it*/

b/=10; /*assigning b as b/10*/


}

printf("%d",s);

int main()

int n;

scanf("%d",&n);

evenodd(n);

reverse(n);

IPS23 -C -Q3

PAC:

Input:​ Accept two numbers

Output:​ Print gcd of the numbers

Processing Involved:​ Recursive function

Solution Alternative:​ Iteration


Algorithm:

1. accept two numbers

2. define function gcd

3. if the numbers are equal print gcd as the number itself

else if number1>number2

call gcd function recursively for number1-number2 and number2

else if number2>number1

call gcd function recursively for number1 and number2-number1

4. call function and pass input values

Code:  

#include<stdio.h>

/* declaring function to find gcd*/

int gcd(int a,int b)

if (a==b) /* if both numbers are equal then gcd is the


number itself*/

printf("%d",a);

else if (a>b) /* if a>b recursive call of function gcd for


a-b and b*/

gcd(a-b,b);

else if (b>a) /* if b>a recursive call of function gcd for a


and b-a*/

gcd(a,b-a);

}
int main()

int n1,n2;

scanf("%d\n%d",&n1,&n2);

/* calling function and passing input values*/

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:-

If his batting average is above or equal to 50


If his strike rate is above or equal to 90

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

100 // Runs scored in match 1

55 // Runs scored in match 2

45 // Runs scored in match 3

20 // Runs scored in match 4

30 // Runs scored in match 5

90 // Ball faced in match 1

60 // Ball faced in match 2

50 // Ball faced in match 3

20 // Ball faced in match 4

40 // Ball faced in match 5

Output1:

50 // Batting average

96 // Strike rate

Selected

Input2:
5 // Number of matches

30 // Runs scored in match 1

55 // Runs scored in match 2

45 // Runs scored in match 3

20 // Runs scored in match 4

30 // Runs scored in match 5

20 // Ball faced in match 1

60 // Ball faced in match 2

50 // Ball faced in match 3

20 // Ball faced in match 4

40 // Ball faced in match 5

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;
}

for (int i=0;i<m;i++)


{
scanf("%d",&a);
balls[i]=a;
c+=a;
}
printf("%d\n",ba=r/m);
printf("%d\n",sr=(r/c)*100);
if (ba>=50 && sr>=90)
printf("Selected");
else
printf("Not Selected");
return 0;

 
 
 
 
 
 
 
IPS21-C-Q2 
Write a C program that will perform the following:- 

1. Ask a user to input a string. 

2. Perform a character count   

3. Print the character along with the count, in which the character that has the most 
count gets printed first. 

Input1: 

a big fat cat 

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? 

A sparse matrix is a matrix in which the number of elements = 0 is greater in numbers 


than non-zero elements. 

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? 

1. Create an array of size 50. 

2. Stored the first two values as 1 and 1. 

3. Asked from user for a number (say n) between 1 to 50 (i.e 1<n<50). Check for 
boundary conditions? 

4. Create a function to generate fibonacci series upto n term. 

5. Create a recursive function to print the factorial of n 


Input1: 

Output1: 

1 1 2 3 5 

120 

Input2: 

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? 

Armstrong number is a number: 

abcd... = a​n​ + b​n​ + c​n​ + d​n

153 = 1​3​ + 5​3+


​ 3​3 

1634 = 1​4​ + 6​4 ​+ 3​4​ + 4​4​ = 1 + 1296 + 81 + 256 

Input1: 

153 

Output1: 

Yes 

Input2: 

111 

Output: 

No 

Input:

given positive number

Output:

yes -Armstrong number or

no-not a Armstrong number

Processing Involved: 

addition, looping 
 

Algorithm/ Pseudocode: 

step 1: accept the number 

step 2: start a while with a condition that the given number is not zero 

step 3: divide the number with 10  

step 4: add it with the variable which is declared 0 as the value 

step 5: start another while loop and a nested for loop to add all the values from 1 to 
the value given 

step 6: given name for the value to square it 

step 7: add it one by one in another variable 

step 8: if the number obtained is equal to the number given print yes else no 

Code: 

#include <stdio.h>

int main()

int num, real,rem,res=0,count=0,res1=1,copy;

scanf("%d",&num);

real=num;

copy=num;

while (num != 0)

{
num /=10;

++count;

while (real != 0)

res1 = 1;

rem=real%10;

for (int i=0;i<count;i++)

res1 *= rem;

res += res1;

real /= 10;

if (res == copy)

printf("Yes");

else

printf("No");

return 0;

}
 

IPS 22 -C -Q3 
 
Given the board configu​r​ation 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 


Output2: 

Intermediate 

Input3: 

Output3: 

Draw 

Input4: 


Output4: 

1 wins 

Input5: 

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;

 
 
 

You might also like