0% found this document useful (0 votes)
23 views17 pages

PSC Practical

The document discusses solutions to 11 programming problems involving arrays, functions, recursion, conditionals, loops and mathematical operations. The problems cover a range of basic to intermediate concepts in C programming.

Uploaded by

Pankaj Desai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views17 pages

PSC Practical

The document discusses solutions to 11 programming problems involving arrays, functions, recursion, conditionals, loops and mathematical operations. The problems cover a range of basic to intermediate concepts in C programming.

Uploaded by

Pankaj Desai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Subject:- Problem Solving Using Computer

Q.1] Prepare flowchart, write algorithm and then write a program to perform the mathematical
operations such as addition, subtraction multiplication, division and mod of two numbers.
[A.1] SOLUTION

#include <stdio.h>
int main()
{
int a, b;
printf("Enter the number\n");
scanf("%d %d", &a,&b);
printf("a + b = %d\n", a+b);
printf("a - b = %d\n", a-b);
printf("a * b = %d\n", a*b);
printf("a / b = %d\n", a/b);
printf("a % b = %d\n", a%b);
return 0;
}

Start

a and b

a+b

a-b

a*b

a/b

a%b

Sum of
the
given
number

end
Q.2] Write a program to find greatest among the 3 numbers using if statements. Write a program
to find smallest among the 3 numbers using conditional operators.
[A.2] SOLUTION

Example no- 1
#include <stdio.h>
int main()
{
int A, B, C;

printf("Enter the numbers A, B and C: "”\n”);


scanf("%d %d %d", &A, &B, &C);

if (A >= B && A >= C)


printf("%d is the largest number.", A);

if (B >= A && B >= C)


printf("%d is the largest number.", B);

if (C >= A && C >= B)


printf("%d is the largest number.", C);

return 0;
}

OUTPUT
Enter the numbers A, B and C:
5
9
8
9 is the largest number
Q.3] Write a program to input a character and decide whether it is a vowel or not. Try to use
toupper() or tolower() function to ignore the character case of the vowels.
[A.3] SOLUTION

#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is not a vowel", c);
return 0;
}

OUTPUT
Enter an alphabet: d
D is not a vowel
Q.4] Write a program to find factorial of number.
[A.4] SOLUTION

#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}

OUTPUT
Enter an integer: 88
Factorial of 88 = 0
Q.5] Write a program to find sum and average of „n‟ numbers. Declare average as float and other
variables as integers.
[A.5] SOLUTION

#include<stdio.h>

int main()
{
int i,n,Sum=0,numbers;
float Average;

printf("\nPlease Enter How many Number you want?\n");


scanf("%d",&n);

printf("\nPlease Enter the elements one by one\n");


for(i=0;i<n;++i)
{
scanf("%d",&numbers);
Sum = Sum +numbers;
}

Average = Sum/n;

printf("\nSum of the %d Numbers = %d",n, Sum);


printf("\nAverage of the %d Numbers = %.2f",n, Average);

return 0;
}

OUTPUT
Please Enter How many Number you want?
2

Please Enter the element one by one


58
Sum of the 2 Number = 13
Average of the 2 Number = 6.00
Q.6] Write a program to input name and marks of 3 subjects. Calculate total, percentage and
grade the students according to the slab:
Per Grade >=75 and <=100 ‟O‟
>=60 and =50 and < 60 ‟B‟
>=40 and < 50 ‟C‟
>=0 and < 40 ‟D‟
[A.6] SOLUTION

#include <stdio.h>
int main()
{
int eng, math, comp;
float per;

printf("Enter three subjects marks: ");


scanf("%d%d%d",&eng, &math, &comp);

per = (eng + math + comp) / 3.0;

printf("Percentage = %.2f\n", per);

if(per >= 75)


{
printf("Grade O");
}
else if(per >=60)
{
printf("Grade A");
}
else if(per >= 50)
{
printf("Grade B");
}
else if(per >=40)
{
printf("Grade C");
}
else if(per >= 0)
{
printf("Grade D");
}
return 0;
}

OUTPUT
Enter three subjects marks: 55
65
75
Percentage = 65.00
Grade A
Q.7] Write a menu driven program to convert dollars to rupees and rupees to dollars.
[A.7] SOLUTION

#include <stdio.h>
int main()
{
float rupees,dollar,result;
int choice;

{
printf("Convert Your Currency here\n");
printf("Press 1: for convert RUPEE to DOLLAR");
printf("\nPress 2: for convert DOLLAR to RUPEE");
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the Currency in Rupees = Rs.");
scanf("%f",&rupees);
result=rupees/74.64;
printf("\nRate of Rs.%.1f/- in US Dollar is = %.3f$\n\n",rupees,result);
break;
case 2:printf("\n\nEnter the currency in Dollar = $");
scanf("%f",&dollar);
result=dollar*74.64;
printf("Rate of %.1f$ in Indian Rupee is = Rs.%.2f/-\n\n",dollar,result);
break;
default :
printf("\n\n[SORRY! YOU HAVE ENTERED WRONG NUMBER]\n\n");
}
return 0;
}
}

OUTPUT
Convert Your Currency here
Press 1: for convert RUPEE to DOLLAR
Press 2: for convert DOLLAR to RUPEE
Enter Your Choice : 1

Enter the Currency in Rupees = Rs.100

Rate of Rs.100.0/- in US Dollar is = 1.340$


Q.8] Write a program using functions to find reverse of a number and decide whether it is
palindrome or not. Develop two functions getnum() and reverse() with proper prototypes.
[A.8] SOLUTION

#include <stdio.h>

int main()
{
int num, temp, remainder, reverse = 0;

printf("Enter an integer \n");


scanf("%d", &num);
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", reverse);
if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n");
return 0;
}

OUTPUT
Enter an integer
86
Given number is = 86
Its reverse is = 68
Number is a palindrome
Q.9] Write a program to find factorial of a number using the concept of recursion.
[A.9] SOLUTION

#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

OUTPUT
Enter a positive integer: 12
Factorial of 12 = 479001600
Q.10] Write a menu driven program to create one dimensional array, display it , find the sum of all
the elements, find maximum and minimum element within the array, include the facility to search
an element. Finally sort the array.
[A.10] SOLUTION

#include<stdio.h>
#include<stdlib.h>

int a[10], pos, elem;


int n = 0;
void create();
void display();
void insert();
void del();
void main()
{
int choice;
while(1)
{
printf("\n\n~~~~MENU~~~~");
printf("\n=>1. Create an array of N integers");
printf("\n=>2. Display of array elements");
printf("\n=>3. Insert ELEM at a given POS");
printf("\n=>4. Delete an element at a given POS");
printf("\n=>5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4:del();
break;
case 5:exit(1);
break;
default:printf("\nPlease enter a valid choice:");
}
}
}
void create()
{
int i;

printf("\nEnter the number of elements: ");


scanf("%d", &n);
printf("\nEnter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]); }
}
void display()
{
int i;
if(n == 0)
{
printf("\nNo elements to display");
return;
}
printf("\nArray elements are: ");
for(i=0; i<n;i++)
printf("%d\t ", a[i]);
}
void insert()
{
int i;
if(n == 5)
{
printf("\nArray is full. Insertion is not possible");
return;
}
do
{
printf("\nEnter a valid position where element to be inserted: ");
scanf("%d", &pos);
}while(pos > n);
printf("\nEnter the value to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos ; i--)
{
a[i+1] = a[i];
}
a[pos] = elem;
n = n+1;
display();
}
void del()
{
int i;
if(n == 0)
{
printf("\nArray is empty and no elements to delete");
return; }

do
{
printf("\nEnter a valid position from where element to be deleted: ");
scanf("%d", &pos);
}while(pos>=n);
elem = a[pos];
printf("\nDeleted element is : %d \n", elem);
for( i = pos; i< n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
display();
}

OUTPUT
~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 1

Enter the number of elements: 4

Enter the elements: 5


6
7
9

~~~~MENU~~~~
=>1. Create an array of N integers
=>2. Display of array elements
=>3. Insert ELEM at a given POS
=>4. Delete an element at a given POS
=>5. Exit
Enter your choice: 5
Q.11] Write a program to create two arrays, find sum and difference of these two matrices.
[A.11] SOLUTION

#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n"); }
}
return 0; }
OUTPUT
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 5
Enter element a12: 6
Enter elements of 2nd matrix:
Enter element b11: 6
Enter element b23: 6

Sum of two matrices:


11 9 8

17 12 15
Q.12] Write a program using the concept of array of structures to create a list of students having
the following fields, roll no, name, marks[3], total. Marks should be stored as an integer array.
[A.12] SOLUTION

#include<stdio.h>

struct student {
char name[20];
int rollno;
float marks;
};
int main( )
{
int i,n;
printf("Enter how many records u want to store :: ");
scanf("%d",&n);
struct student stuarr[3];
printf("Enter name, roll no. and marks Below :: \n");

for(i=0; i<n; i++)


{
printf("\nEnter %d record :: \n",i+1);
printf("Enter Name :: ");
scanf("%s",stuarr[i].name);
printf("Enter Roll No. :: ");
scanf("%d",&stuarr[i].rollno);
printf("Enter Marks :: ");
scanf("%f",&stuarr[i].marks);

}
printf("\n\tName\tRoll No\tMarks\t\n");
for(i=0; i<n; i++)
printf("\t%s\t%d\t%.2f\t\n", stuarr[i].name, stuarr[i].rollno, stuarr[i].marks);

return 0; }
OUTPUT
Enter how many records u want to store :: 2
Enter name, roll no. and marks Below ::
Enter 1 record ::
Enter Name :: Yash
Enter Roll No. :: Enter Marks :: 86
Enter Marks :: 75

Enter 2 record ::
Enter Name :: Ravi
Enter Roll No. :: 15
Enter Marks :: 75

Name Roll No Marks


Yash 20 86.00
Ravi 15 75.00
Q.13] Write a program to swap the values of two variables by using call by reference method in
functions.
[A.13] SOLUTION

#include<stdio.h>
void swap(int*n1,int*n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
}
int main()
{
int a,b;
printf("Enter Two Numbers: \n");
scanf("%d%d",&a,&b);
printf("\nNumbers Before Swapping: \n");
printf("\n a=%d b=%d\n",a,b);
swap(&a,&b);
printf("\nNumbers After Swapping: \n");
printf("\n a=%d b=%d",a,b);
return 0;
}

OUTPUT
Enter Two Numbers:
56
58

Numbers Before Swapping:

a=56 b=58

Numbers After Swapping:

a=58 b=56
Q.14] Write a program to create a text file, L1.txt‟ which stores a line of text till the user presses
the enter. Copy this file into L2.txt.
[A.14] SOLUTION

#include <stdio.h>
int main()
{
FILE *fp; /* file pointer*/
char fName[20];
char ch;
printf("\nEnter file name to create :");
scanf("%s",fName);
/*creating (open) a file*/
fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
exit(0); /*exit from program*/
}
printf("File created successfully.");
/*writting into file*/
printf("\nEnter text to write (press < enter > to save & quit):\n");
while( (ch=getchar())!='\n')
{
putc(ch,fp); /*write character into file*/
}
printf("\nData written successfully.");
fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");
if(fp==NULL)
{
printf("\nCan't open file!!!");
exit(0);
}
printf("\nContents of file is :\n");
/*read text untill, end of file is not detected*/
while( (ch=getc(fp))!=EOF )
{
printf("%c",ch); /*print character on screen*/ }
fclose(fp);
return 0;
}
OUTPUT
Enter file name to create :L1.txt
File created successfully.
Enter text to write (press < enter > to save & quit):

Data written successfully.


Contents of file is :
Q.15] Write a menu driven program to create an array of structure which stores names of the
countries and their capitals. Display the list and include the facility such that if the user enters the
country name, the program should give its capital and also include the reverse facility ,in case the
country or capital is not found proper message should be printed. The Menu should be as follow:
MENU
1. Create
2. Display
3. Country to Capital
4. Capital to country
5. Exit

[A.15] SOLUTION

#include <stdio.h>
int main()
{
char India, China , Pakistan , America;

printf("Enter the country name:\n ");


scanf("%c", &India, &China , &Pakistan , &America);

printf("capital of country\n");

if(India)
{
printf("New Delhi");
}
else if (China)

{
printf("Beijing");
}

else if(Pakistan)
{
printf("Karachi");
}
else if(America)
{
printf("Washington, D.C.");

return 0;
}

OUTPUT
Enter the country name: India
capital of country New Delhi

You might also like