Professional Documents
Culture Documents
Practical File: Sbssu Shaheed Bhagat Singh State University, Ferozepur, Punjab
Practical File: Sbssu Shaheed Bhagat Singh State University, Ferozepur, Punjab
Practical file
// calculating sum
sum = number1 + number2;
Output :-
Enter two integers: 2
3
2 + 3 =5
Aim: To translate given algorithms to a working and correct program
Problem 1. C program to compute quotient and remainder.
Program:-
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
return 0;
}
Output:
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
Using nested if else.
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
return 0;}
Output
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
Problem 4. C program to find roots of quadratic equations.
Program:
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
return 0;
}
Output:
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
Aim: To be able to identify and correct logical errors encountered at run time
Problem 1. C program to check weather a number is positive or negative.
Program:
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
} else
printf("You entered a positive number.");
return 0;
}
Output:
Enter a number: 12.3
You entered a positive number.
Enter a number: 0
You entered 0.
Aim: To be able to write iterative as well as recursive programs.
Problem 1. Sum of N natural numbers using recursion.
Program:
#include <stdio.h>
int addNumbers(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}
Output:
Enter a positive integer: 20
Sum = 210
Problem 2. To find a factorial of a number.
Program:
#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;
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
Output:
Enter the numbers of elements: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69
Problem 2. To find sum of two matrices.
Program:
#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);
return 0;
}
Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
10 8 6
Problem 2. To find inverse of a matrix.
Program:
#include<stdio.h>
int main(){
int a[3][3],i,j;
float determinant=0;
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
return 0;
}
Output:
Enter the 9 elements of matrix: 3
5
2
1
5
8
3
9
2
The matrix is
3 5 2
1 5 8
3 9 2
Inverse of matrix is:
printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}
int main()
int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
return 0;
}
Output:
Enter rows and column for the first matrix: 2
3
Enter rows and column for the second matrix: 3
2
Enter elements:
Enter a11: 2
Enter a12: -3
Enter a13: 4
Enter a21: 53
Enter a22: 3
Enter a23: 5
Enter elements:
Enter a11: 3
Enter a12: 3
Enter a21: 5
Enter a22: 0
Enter a31: -3
Enter a32: 4
Output Matrix:
-21 22
159 179
Problem 3. C Program to Sort set of strings in alphabetical order
Program:
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
puts("How many strings u are going to enter?: ");
scanf("%d",&count);
return 0;
}
Output:
How many strings u are going to enter?:
5
Enter Strings one by one:
Atul
Steve
carry
Rahul
Ajeet
Order of Sorted Strings:
Ajeet
Atul
Carry
Rahul
Steve
Aim: To be able to declare pointers of different types and use them in defining self referential structures.
Problem: Write a function in c language that receives 5 integers and return the sum, average and
standard deviation of these numbers. Call this function from main() and print the results in main().
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int a, b, c, d, e, sm, av;
float sd;
getch();
return 0;
}
void calc(int a, int b, int c, int d, int e, int *sm, int *av, float *sd)
{
int i;
*sm = a + b + c + d + e;
*av = *sm / 5;
*sd = pow(a - *av, 2) + pow(b - *av, 2) + pow(c - *av, 2) + pow(d - *av, 2) + pow(e - *av, 2);
*sd = sqrt(*sd / 5);
}
Output:
Enter five numbers : 52
65
47
45
20
Sum : 229
Average : 45
cntV=cntC=0;
while(*ptr!='\0')
{
if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a' ||*ptr=='e' ||*ptr=='i' ||*ptr=='o' ||*ptr=='u')
cntV++;
else
cntC++;
//increase the pointer, to point next character
ptr++;
}
#include <stdio.h>
struct student{
char name[30];
int roll;
float perc;
};
int main()
{
struct student std; //structure variable
struct student *ptr; //pointer to student structure
return 0;
}
Output:
Enter details of student:
Name ?:Mike
Roll No ?:101
Percentage ?:89.7
Entered details:
Name:Mike
RollNo: 101
Percentage: 89.70
Problem4. C program to read array elements and print with addresses.
Program:
#include <stdio.h>
int main()
{
int arr[10]; //declare integer array
int *pa; //declare an integer pointer
int i;
return 0;
}
Output:
Enter array elements:
Enter element 01: 11
Enter element 02: 23
Enter element 03: 444
Enter element 04: 4
Enter element 05: 5
Enter element 06: 6
Enter element 07: 77
Enter element 08: 89
Enter element 09: 67
Enter element 10: 12
In the above program, we are opening a file newfile.txt in r mode, reading the content of the file and
displaying it on the console.
Problem2. An example to show Open, read, write and close operation in C.
Program:
int main()
{
char ch;
return 0;
}
Problem 3. Reading and Writing Binary Files in C.
Program:
#include <stdio.h>
int main()
{
char ch;
return 0;
}