0% found this document useful (0 votes)
4 views

Suk Pps Lab Manual 2024

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)
4 views

Suk Pps Lab Manual 2024

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/ 22

Programming For Problem Solving Laboratory(22PPSL19/29)

Programming For Problem Solving Laboratory


[As per Choice Based Credit System (CBCS) scheme]
(Effective from the academic year 2022-23)

Course Code: 21PPSL19/29 CIE Marks : 50


Teaching Hours/Week: 02 SEE Marks : 50
Total Teaching Hours: 38 Exams. Hours : 03
Semester : I/II Credits : 01

Course Learning Objectives:


1. Explain problem statements and identify appropriate solutions.
2. Develop algorithms and programs using constructs of C programming language..
3. Develop C programs using appropriate data type, control/decision statements.
4. Identify and rectify the syntax and syntactic errors during programming.

Descriptions (if any):


● The laboratory should be preceded or followed by a tutorial to explain the algorithm
and logical approach to be implemented for the problems given and real life application.
Every experiment should have algorithm and flowchart be written before writing the
program.
● Code should be traced minimum two test cases which should be recorded.

PART-A

1 Programs to understand the basic concepts of C programming (Two Programs)


2 Programs to illustrate the use of C operators. ( Two Programs)
3 Programs to illustrate the application of conditional branching statements. (Four Programs)
4 Programs to demonstrate the use of looping constructs (Four Programs)
5 Programs on arrays. ( Two Programs)

PART-B
1. Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built in library function. Print both the results with appropriate inferences.

SHARNBASVA UNIVERSITY,
Programming For Problem Solving
2. Write functions to implement string operations such as compare, concatenate, string
length. Convince the parameter passing techniques.
3. Develop a program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of N real numbers.
4. Implement sorting and searching algorithm using function.
5. Implement Recursive functions, namely, GCD and Binary to Decimal Conversion.
6. Implement a C program to maintain a record of “n” student details using an array of structures
with four fields (Roll number, Name, Marks, and Grade). Each field is of an appropriate data
type. Print the marks of the student given student name as input.

Course Outcomes:
The students shall able to:
1. Analyze a given problem and propose a solution
2. Develop algorithm, flowchart and write programs to solve the given problem.
3. Debug and execute a given program.
4. Identify the programming constructs and apply appropriate control / decision statement
given problem.

Question paper pattern:

1. All laboratory experiments are to be included for practical examination.


2. Part A – 20 Marks and Part B – 30 Marks.
3. Students are allowed to pick one experiment from part A and one experiment part B.
4. Strictly follow the instructions as printed on the cover page of answer script for breakup of
marks.
5. Change of experiment is allowed only once and 15% Marks is deducted from the
procedure part.

SHARNBASVA UNIVERSITY,
Programming For Problem Solving
PART -A

1. Program to understand the basic concepts of C programming (Two Programs)

(i) Printing Hello World

#include <stdio.h>
int main(void)
{
printf("Hello World”);
return 0;
}

OUTPUT: Hello World

(ii) Swapping of Two numbers.

#include <stdio.h>
void main()
{
int firstNumber, secondNumber, temporaryVariable;
printf("Enter first number: ");
scanf("%d", &firstNumber);
printf("Enter second number: ");
scanf("%d",&secondNumber);
temporaryVariable = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryVariable;
printf("\nAfter swapping, firstNumber = %d\n", secondNumber);
printf("After swapping, secondNumber = %d", firstNumber);
}

OUTPUT:

Enter first number : 34


Enter second number
:45
After swapping, firstNumber = 45
After swapping, secondNumber = 34

SHARNBASVA UNIVERSITY,
Programming For Problem Solving

2 . Programs to illustrate the use of C operators. (Two Programs)

(i). Arithmetic Operators

#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}

Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

SHARNBASVA UNIVERSITY,
Programming For Problem Solving
(ii). Increment and Decrement Operators

#include<stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}

Output:-
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000

2. Problems to illustrate the application of conditional branching statements. (Four Programs)

(i) Program for if statement

#include<stdio.h>
int main()
{
int num1=1;
int num2=2;
if(num1<num2) //test-condition
{
printf("num1 is smaller than num2");
}
return 0;
}
Output :num1 is smaller than num2

SHARNBASVA UNIVERSITY,
Programming For Problem Solving

(ii) Program for if-else statement

#include<stdio.h>
int main()
{
int num=19;
if(num<10)
{
printf("The value is less than 10");
}
else
{
printf("The value is greater than 10");
}
return 0;
}

Output: The value is greater than 10

SHARNBASVA UNIVERSITY,
Programming For Problem Solving
(iii) Program for Nested if-else statement

#include<stdio.h>
int main()
{
int num=1;
if(num<10)
{
if(num==1)
{
printf("The value is:%d\n",num);
}
else
{
printf("The value is greater than 1");
}
}
else
{
printf("The value is greater than 10");
}
return 0;
}

Output
The value is :1

SHARNBASVA UNIVERSITY,
Programming For Problem Solving

(iv) Program for Nested if-else statement

#include<stdio.h>
int main()
{
int marks=83;

if(marks>75)
{
printf("First class");
}
else if(marks>65)
{
printf("Second class");
}
else if(marks>55)

{
printf("Third class");
}
Else
{
printf("Fourth class");
}
return 0;
}

Output :First class

SHARNBASVA UNIVERSITY,
Programming For Problem Solving
3. Problems to demonstrate the use of lopping construct.(Four Programs)

(i) Example of while statement

int main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6)
{
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}

Output:
Hello World

Hello World

Hello World

Hello World

Hello World

SHARNBASVA UNIVERSITY,
(ii) Example of do while statement

#include <stdio.h>
int main()
{
int i = 2; // Initialization expression
do
{
// loop body
Printf( "Hello World\n");
// update expression
i++;
}
while (i < 1); // test expression
return 0;
}
Output :Hello World

(iii) Example C program to demonstrate infinite loops using while

#include <stdio.h>
int main ()
{
int i;
while (i != 0)
{
i-- ;
printf( "This loop will run forever.\n");
}}
Output:
This loop will run forever.

This loop will run forever.

SHARNBASVA UNIVERSITY,
(iv) Example of for statement

// C program to illustrate for loop


#include <stdio.h>

int main()
{
int i=0;

for (i = 1; i <= 10; i++)


{
printf( "Hello World\n");
}

return 0;
}

Output

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

SHARNBASVA UNIVERSITY,
4. Programs on array.(Two Programs)

(i) Array Input/Output

// Program to take 5 values from the user and store them in an array// Print the elements stored in the
array
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

OUTPUT:
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3

S HARNBASVA UNIVERSITY, KALABURAGI


PPS 20

(ii). Calculate Average

// Program to find the average of n numbers using


arrays #include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i < n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable
sum += marks[i];
}
average = sum / n;
printf("Average = %d", average);
return 0;
}

OUTPUT:
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39

SHARNBASVA UNIVERSITY,
COMPUTER PROGRAMMING 2021-

PART – B

1. Introduce Iterative problem solving and implement Taylor series approximation to compute
Sin(x) or polynomial.
#include<stdio.h>
#include<math.h>
#define PI 3.142
void main()
{
int i, degree;
float x, sum=0,term,nume,deno;
printf("Enter the value of degree\n");
scanf("%d",&degree);
x = degree * (PI/180);
nume = x;
deno = 1;
i=2;
do
{
term = nume/deno;
nume = -nume*x*x;
deno = deno*i*(i+1);
sum=sum+term;
i=i+2;
}
while(fabs(term) >= 0.00001);
printf("The sine of %d is %.3f\n", degree, sum);
printf("The sine function of %d is %.3f\n", degree, sin(x));
}

SHARNBASVA UNIVERSITY,
COMPUTER PROGRAMMING 2021-

2. Write functions to implement string operations such as compare, concatenate, string


length. Convince the parameter passing techniques.
#include<stdio.h>
#include<string.h>
void stringlength(char a[100],char b[100]);
void concatenate(char a[100],char b[100]);
void stringcompare(char a[100],char
b[100]); void main()
{
char p[100],q[100],ch[100];
int len1,len2;
printf("Enter the first string:\n");
gets(p);
printf("Enter the second string:\n");
gets(q);
stringlength(p,q);
stringcompare(p,q);
concatenate(p,q);
}
void stringlength(char a[100], char b[100])
{
int len1,len2;
len1=strlen(a);
len2=strlen(b);
printf("First string length is :%d \nSecond string lenght is: %d",len1,len2);
}
void concatenate(char a[100], char b[100])
{
printf("The concatenated String is :%s ", strcat(a,b));
}
void stringcompare(char a[100], char b[100])
{
if(strcmp(a,b)==0) printf("\
nSTRINGS ARE EQUAL\n");

SHARNBASVA UNIVERSITY,
COMPUTER PROGRAMMING 2021-
else
printf("\nSTRINGS ARE NOT EQUAL\n");
}

3. Develop a program using pointers to compute the sum, mean and standard deviation of
all elements stored in an array of N real numbers.

#include<stdio.h>
void main()
{
int arr[10],n,i,sum=0,mean;
//Assigning array to pointer
int *ptr=arr;

printf("Enter the number of elements you want to use(<=10):


"); scanf("%d",&n);
printf("\nEnter %d Elements:",n);
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
//sum of elements of array using pointer
for (i=0;i<n;i++)
{
sum+=*ptr;
*ptr++;
}
mean=sum/n;
//Display sum,mean and standard deviation on screen
printf("\nSum = %d\nmean = %d",sum,mean);
}

SHARNBASVA UNIVERSITY,
COMPUTER PROGRAMMING 2021-

4. Write a program to find the value of nCr using functions */

#include <stdio.h>

// Function to calculate factorial


int factorial(int num)
{
if (num == 0 || num == 1)
return 1;
else
return num * factorial(num - 1);
}

// Function to calculate nCr


int nCr(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}

int main()
{
int n, r;

printf("Enter the value of n: ");


scanf("%d", &n);

printf("Enter the value of r: ");


scanf("%d", &r);

if (n < 0 || r < 0 || r > n)


{
printf("Invalid input. Make sure 0 <= r <= n.\n");
}
else
{
int result = nCr(n, r);
printf("%dC%d = %d\n", n, r,
result);
}

return 0;
}

SHARNBASVA UNIVERSITY,
COMPUTER PROGRAMMING 2021-
5. Implement Recursive functions, namely, GCD and Binary to Decimal
Conversion. Program for GCD

#include <stdio.h>
int gcd(int n1, int
n2); void main()
{
int n1, n2,res;
printf("Enter two positive integers: \n");
scanf("%d %d", &n1, &n2);
res=gcd(n1,n2);
printf("G.C.D of %d and %d is %d\n", n1, n2, res);
}
int gcd(int n1, int n2)
{
if (n2 != 0)
return gcd(n2, n1%n2);
else
return n1;
}

SHARNBASVA UNIVERSITY,
COMPUTER PROGRAMMING 2021-

Program for Binary to Decimal

#include <stdio.h>
#include <math.h>
int B2D(int n);
void main()
{
int num,res;
printf("Enter a binary number: ");
scanf("%d", &num);
res=B2D(num);
printf("The Decimal number is %d\n", res);
}
int B2D(int n)
{
if( !(n/10) )
return n;
else
return (n%10+B2D(n/10)*2);
}

SHARNBASVA UNIVERSITY,
PPS 20

6. Implement a C program to maintain a record of “n” student details using an array of


structures with four fields (Roll number, Name, Marks, and Grade). Each field is of an
appropriate data type. Print the marks of the student given student name as input.

#include<stdio.h>
#include<string.h>
struct student
{
int rollno, marks;
char name[50], grade[3];
};
void main()
{
struct student
s[20]; int
i,n,found=0; char
key[50];
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d student details\n",i+1);
printf("Enter roll number\n");
scanf("%d",&s[i].rollno);
printf("Enter name\n");
scanf("%s",s[i].name);
printf("Enter marks\n");
scanf("%d",&s[i].marks);
printf("Enter grade\n");
scanf("%s",s[i].grade);
}
printf("Student details are \n"); printf("Rollno\
tName\t\tMarks\tGrade\n"); for(i=0;i<n;i++)
printf("%d\t%s\t\t%d\t%s\n", s[i].rollno, s[i].name, s[i].marks,s[i].grade);
printf("Enter the name of the student to search\n");

SHARNBASVA UNIVERSITY,
PPS 20

scanf("%s",key);
for(i=0;i<n;i++)
{
if(strcmp(key,s[i].name)==0)
{
printf("Roll number=%d\nMarks=%d\nGrade=%s\n",s[i].rollno,s[i].marks,s[i].grade);
found=1;
}
}
if(found==0)
printf("Student Record not found\n");
}

SHARNBASVA UNIVERSITY,
PPS 20

7. Write a C Program to copy the contents of one file to another.


#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;

printf("Enter the filename to open for reading \n");


scanf("%s",filename);

// Open one file for reading


fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);

// Open another file for writing


fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

// Read contents from file


c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s",
filename); fclose(fptr1);
fclose(fptr2);
return 0;
}

SHARNBASVA UNIVERSITY,

You might also like