PSC Practical
PSC Practical
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;
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);
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;
Average = Sum/n;
return 0;
}
OUTPUT
Please Enter How many Number you want?
2
#include <stdio.h>
int main()
{
int eng, math, comp;
float per;
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
#include <stdio.h>
int main()
{
int num, temp, remainder, reverse = 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;
}
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>
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
~~~~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
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");
}
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
#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
a=56 b=58
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):
[A.15] SOLUTION
#include <stdio.h>
int main()
{
char 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