Suk Pps Lab Manual 2024
Suk Pps Lab Manual 2024
PART-A
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.
SHARNBASVA UNIVERSITY,
Programming For Problem Solving
PART -A
#include <stdio.h>
int main(void)
{
printf("Hello World”);
return 0;
}
#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:
SHARNBASVA UNIVERSITY,
Programming For Problem Solving
#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
#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
#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;
}
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
#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;
}
SHARNBASVA UNIVERSITY,
Programming For Problem Solving
3. Problems to demonstrate the use of lopping construct.(Four Programs)
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
#include <stdio.h>
int main ()
{
int i;
while (i != 0)
{
i-- ;
printf( "This loop will run forever.\n");
}}
Output:
This loop will run forever.
SHARNBASVA UNIVERSITY,
(iv) Example of for statement
int main()
{
int i=0;
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)
// 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
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",°ree);
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-
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;
SHARNBASVA UNIVERSITY,
COMPUTER PROGRAMMING 2021-
#include <stdio.h>
int main()
{
int n, r;
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-
#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
#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
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
SHARNBASVA UNIVERSITY,