0% found this document useful (0 votes)
87 views14 pages

PPS Practical Solution

The document contains 10 programming problems and their solutions in C language. The problems cover basics of C like input/output, conditional statements, loops, functions, arrays, structures, file handling etc. Detailed programs with comments are given to solve problems like calculating salary components, checking pass/fail, finding quadrant of coordinates, evaluating expressions based on user input etc. Other problems include printing multiplication tables, finding factorial using recursion, searching and sorting arrays, adding diagonal elements of a matrix and counting characters in a string. The last problems deals with storing book details in a structure and writing integers from one file to their squares in another file.

Uploaded by

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

PPS Practical Solution

The document contains 10 programming problems and their solutions in C language. The problems cover basics of C like input/output, conditional statements, loops, functions, arrays, structures, file handling etc. Detailed programs with comments are given to solve problems like calculating salary components, checking pass/fail, finding quadrant of coordinates, evaluating expressions based on user input etc. Other problems include printing multiplication tables, finding factorial using recursion, searching and sorting arrays, adding diagonal elements of a matrix and counting characters in a string. The last problems deals with storing book details in a structure and writing integers from one file to their squares in another file.

Uploaded by

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

PROGRAMMING FOR PROBLEM SOLVING(PPS) PRACTICAL SOLUTION

2(A) Basic salary of an employee is input through the keyboard. The DA is 25% of the
basic salary while the HRA is 15% of the basic salary. Provident Fund is deducted at the
rate of 10% of the gross salary (BS+DA+HRA). Program to calculate the Net Salary.

PROGRAM:
#include<stdio.h>
int main()
{
float basic, da, hra, ta, gs;
float pf;
float net_salary;
printf("Enter Basic Salary : ");
scanf("%f",&basic);
da = (basic*25)/100;
hra = (basic*15)/100;
gs = basic+da+hra;
pf = (gs*10)/100;
net_salary = gs-pf;
printf("Net Salary is: %.02f\n",net_salary);
return 0;
}

2(B) Write a C program to check whether the student is pass or fail using conditional
operator (marks>=40)

PROGRAM:
#include <stdio.h>
int main()
{
int mark;
printf("Enter mark: ");
scanf("%d", &mark);
puts(mark >= 40 ? "Passed" : "Failed");
return 0;
}
3(A) Write a C program to find the quadrant for the given coordinates using if else ladder

PROGRAM:
#include<stdio.h>
void main()
{
int co1,co2;
printf("Input the values for X and Y coordinate : ");
scanf("%d %d",&co1,&co2);
if( co1 > 0 && co2 > 0)
printf("The coordinate point (%d,%d) lies in the First quandrant.\n",co1,co2);
else if( co1 < 0 && co2 > 0)
printf("The coordinate point (%d,%d) lies in the Second quandrant.\n",co1,co2);
else if( co1 < 0 && co2 < 0)
printf("The coordinate point (%d, %d) lies in the Third quandrant.\n",co1,co2);
else if( co1 > 0 && co2 < 0)
printf("The coordinate point (%d,%d) lies in the Fourth quandrant.\n",co1,co2);
else if( co1 == 0 && co2 == 0)
printf("The coordinate point (%d,%d) lies at the origin.\n",co1,co2);
}

3(B) Write a program to find the value of y for a particular value of n. The a, x, b, n is
input by user

a) if n=1 y=ax%b
b) if n=2 y=ax2 +b2
c) if n=3 y=a-bx
d) if n=4 y=a+x/b

PROGRAM:
#include<stdio.h>
int main()
{
int n,a,b,x;
float y;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
printf("Enter x: ");
scanf("%d", &x);
switch(n)
{
case 1:
y=(a*x)%b;
break;
case 2:
y=(a*x*x)+(b*b);
break;
case 3:
y=a-(b*x);
break;
case 4:
y=a+(x/b);
break;
}
return 0;
}

4. Write a program in C to display the multiplication table vertically from 1 to n using for
loop and while loop.

PROGRAM:
Using for loop:
#include <stdio.h>
int main()
{
int j,i,n;
printf("Input upto the table number starting from 1 : ");
scanf("%d",&n);
printf("Multiplication table from 1 to %d \n",n);
for(i=1;i<=10;i++)
{
for(j=1;j<=n;j++)
{
printf("%dx%d = %d, ",j,i,i*j);
}
printf("\n");
}
}
Using while loop
#include < stdio.h >
int main()
{
int num, count = 1;
printf("Enter a number\n");
scanf("%d", &num);
printf("\nMultiplication table for %d is:\n\n", num);
while(count <= 10)
{
printf("%d x %d = %d\n", num, count, (num*count));
count++;
}
return 0;
}

5. Write a C program to find the factorial of given number using function and recursion.

PROGRAM:
Using Function
#include<stdio.h>
int fact(int);
void main()
{
int no,factorial;
printf("Enter a number to calculate it's factorial\n");
scanf("%d",&no);
factorial=fact(no);
printf("Factorial of the num(%d) = %d\n",no,factorial);
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
Using Recursion
#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;
}

6(A) Write C program to search the element from list of n integers using binary search
technique.

PROGRAM:
#include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);


for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}

6(b) Write C program to sort the list of n integers using bubble sorting technique.

PROGRAM:
#include<stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n); for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}

7. Write a C program to evaluate the addition of diagonal elements of square matrix.

PROGRAM:
#include<stdio.h>
void main()
{
int mat[12][12];
int i,j,row,col,sum=0;
printf("Enter the number of rows and columns for 1st matrix\n");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("The matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+mat[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix = %d\n",sum);
}

8. Write a program in C to count total number of alphabets, digits and special characters in
a string.

PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define str_size 100 //Declare the maximum size of the string

void main()
{
char str[str_size];
int alp, digit, splch, i;
alp = digit = splch = i = 0;

printf("\n\nCount total number of alphabets, digits and special characters :\n");


printf("--------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
/* Checks each character of string*/

while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alp++;
}
else if(str[i]>='0' && str[i]<='9')
{
digit++;
}
else
{
splch++;
}
i++;
}
printf("Number of Alphabets in the string is : %d\n", alp);
printf("Number of Digits in the string is : %d\n", digit);
printf("Number of Special characters in the string is : %d\n\n", splch);
}
9. Write a c program to store the information of Books using structure .The information of
each book to be stored as.
Name of book:
Author’s name:
price:
No. of copies available in library:

PROGRAM:
#include<stdio.h>
struct bookdetail
{
char name[20];
char author[20];
float price;
int total_copies;
};
void main()
{
struct bookdetail b[50];
int num,i;
printf("Enter the Numbers of Books:");
scanf("%d",&num);
printf("\n");
for(i=0;i<num;i++)
{
printf("\t=:Book %d Detail:=\n",i+1);
printf("\nEnter the Book Name:\n");
scanf("%s",b[i].name);
printf("Enter the Author of Book:\n");
scanf("%s",b[i].author);
printf("Enter the Price of Book:\n");
scanf("%d",&b[i].price);
printf("Enter the number of copies available in Library:\n");
scanf("%f",&b[i].total_copies);
}
for(i=0;i<n;i++,t++)
{
printf("\n");
printf("\t\tBook %d Name is=%s \n",i+1,b[i].name);
printf("\t\tBook %d Author is=%s \n",i+1,b[i].author);
printf("\t\tBook %d Price is=%f \n",i+1,b[i].price);
printf("\t\tBooks available in Library=%d \n",b[i].total_copies);
printf("\n");
}
}

10. Write a program to take 10 integers from file and write square of these integer in other
file.

PROGRAM:
#include<stdio.h>
void main()
{
FILE * f,*s;
int a;
f=fopen(“abc.txt”,”r”);
s=fopen(“xyz.txt”,”w”);
while((fscanf(f,”%d”,&a)!=EOF)
{
fprintf(s,”%d”,a*a);
}
fclose(f);
fclose(s);
}

You might also like