Programming For Problem Solving Practical File - 092344
Programming For Problem Solving Practical File - 092344
PROBLEM SOLVING
PRACTICAL FILE
ANISHA TYAGI
1
INDEX
S.NO PROGRAM DATE SIGNATURE
1. WAP to print single sentence
2. WAP find square of a given number
3. WAP to find simple interest
4. WAP to perform arithmetic operations
5. WAP to convert Celsius to Fahrenheit
6. WAP to find the greatest of two
numbers
7. WAP to find whether numbers is even
or odd
8. WAP to find whether number is
divisible by 5 and 11
9. WAP to find roots of the quadratic
equation
10. WAP to make calculator
11. WAP to find greatest between three
number
12. WAP to find the sum of digit of given
number
13. WAP to print Fibonacci series
14. WAP to find whether the number is
Armstrong or not
15. WAP to find whether the number is
palindrome or not
16. WAP to check whether the number is
prime or not
17. WAP to print sum of first n natural
number
18. WAP to swap values by call by
reference
19. WAP to swap number by call by value
20. WAP of Structure
21. WAP of union
ANISHA TYAGI
2
22. WAP of array of marks of student
23. WAP to count the number of elements
in array
24. WAP to find the sum of elements of
array
25. WAP to add two matrices
26. WAP of matrices multiplication
27. WAP of transpose of matrix
28. WAP to find Fibonacci series using
recursion
29. WAP to find factorial of the given
number using recursion
30. WAP for the pointer of array
31. WAP for pointer of structure
32. WAP of Binary search
33. WAP of Linear search
34. Bubble sort
35. Selection sort
36. WAP to print a string
ANISHA TYAGI
3
PROGRAM 1
WAP TO PRINT SIMPLE SENTENCE
Source Code:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Output :
ANISHA TYAGI
4
PROGRAM 2
WAP to find square of a given number
Source code :
#include<stdio.h>
int main()
{
int number, square;
printf(" \n Please Enter any integer Value : ");
scanf("%d", &number);
square = number * number;
printf("\n square of a given number %d is = %d", number,
square);
return 0;
Output :
ANISHA TYAGI
5
PROGRAM 3
WAP TO FIND SIMPLE INTEREST
Source code :
#include <stdio.h>
Int main() {
Int principal, rate, time, interest;
printf(“Enter the principal:”);
scanf(“%d”, &principal);
printf(“Enter the rate:”);
scanf(“%d”, &rate);
printf(“Enter the time:”);
scanf(“%d”, &time);
interest = principal*rate*time/100;
printf(“The simple interest is %d”, interest);
return 0;
}
ANISHA TYAGI
6
Output :
PROGRAM 4
WAP TO PERFORM ARITHMETIC OPERATIONS
Source code:
#include <stdio.h>
int main() {
int num1, num2, sum, sub, mult, mod;
float div;
printf(“Enter any two numbers:”);
scanf(“%d%d”, &num1, &num2);
sum = num1+num2;
sub = num1-num2;
mult= num1*num2;
div = (float) num1 / num2;
mod = num1 % num2;
printf(“sum = %d\n”, sum);
printf(“difference = %d\n”, sub);
ANISHA TYAGI
7
printf(“product = %d\n”, mult);
printf(“quotient =%f\n”, div);
printf(“modulus = %d”, mod);
return 0;
Output:
PROGRAM 5
WAP TO CONVERT CELCIUS TO FAHRENHEIT
Source code:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf(“Enter temperature in celsius: “);
scanf(“%f”, &celsius);
fahrenheit = (celsius*9/5) +32;
ANISHA TYAGI
8
printf(“%.2f celsius = %.2f fahrenheit”, celsius, fahrenheit);
return 0;
}
Output:
PROGRAM 6
WAP TO FIND THE GREATEST OF TWO NUMBERS
Source code:
#include <stdio.h>
int main () {
int num1, num2;
printf(“Enter any two numbers”);
scanf(“%d%d”, &num1, &num2);
if(num1 > num2)
{ printf(“%d is greater”, num1);}
ANISHA TYAGI
9
Else
{ printf(“%d is greater”,num2);}
return 0;
}
Output:
PROGRAM 7
WAP TO FIND WHETHER NUMBERS IS EVEN OR ODD
Source code:
#include <stdio.h>
int main() {
int num;
printf(“Enter an integer:”);
scanf(“%d”, &num);
if (num % 2==0)
ANISHA TYAGI
10
printf(“%d is even.”, num);
else
printf(“%d is odd.”, num);
return 0;
}
Output:
PROGRAM 8
WAP TO FIND WHETHER NUMBER IS DIVISIBLE BY 5 AND 11
Source code:
#include <stdio.h>
Int main()
{
Int num;
Printf(“Enter any number:”);
Scanf(“%d”, &num);
If ((num % 5 == 0) && (num % 11 == 0))
{printf(“Number is divisible by 5 and 11”);}
ANISHA TYAGI
11
Else
{printf(“Number is not divisible by 5 and 11”);}
Return 0;
}
Output:
PROGRAM 9
WAP TO FIND ROOTS OF THE QUADRATIC EQUATION
Source code:
#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);
disctiminant = b*b-4*a*c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2*a);
ANISHA TYAGI
12
root2 = (-b – sqrt(discriminant)) / (2*a);
printf(“root1 = %.2lf and root = %.2f”, root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2*a);
printf(“root1 = root2 = %.2lf;”, root1);
}
else {
realPart = -b / (2*a);
imagPart = sqrt (-disciminant) / (2*a);
printf(“root1 = %.2lf+%.2lfi and root2 = %.2f-%,2fi”, realPart,
imagPart, realPart, imagPart);
}
return 0;
}
Output:
ANISHA TYAGI
13
PROGRAM 10
WAP TO MAKE CALCULATOR
Source code:
#include <stdio.h>
Int main () {
Char op;
Double first, second;
Printf(“Enter an operator (+, -, *, /): ”);
Scanf(“%c”, &op);
Printf(“Enter two operands:”);
Scanf(“%lf %lf”, &first, &second);
Switch (op) {
Case ‘+’:
printf(“%.1lf+ %.1lf = %.1lf”, first, second, first + second);
Break;
Case ‘-‘:
printf(“%.1lf- %.1lf = %.1lf”, first, second, first- second);
break;
Case ‘*‘:
printf(“%.1lf-*%.1lf = %.1lf”, first, second, first* second);
break;
ANISHA TYAGI
14
Case ‘/‘:
printf(“%.1lf / %.1lf = %.1lf”, first, second, first / second);
break;
default;
printf(“Error! Operator is not correct”);
}
Return 0;
Output:
ANISHA TYAGI
15
PROGRAM 11
WAP TO FIND GREATEST BETWEEN THREE NUMBER
Source code:
#include <stdio.h>
Int main() {
Double n1, n2, n3;
Printf(“Enter three numbers:”);
Scanf(“%lf %lf %lf”, &n1, &2, &n3);
If (n1 >= n2 && n1 >= n3)
Printf(“%.2lf is the greatest number.”, n1);
Else if (n2 >+ n1 && n2 >+ n3)
Printf(“%.2f is the greatest number.”, n2);
Else
Printf( “%.2lf is the largest number.”, n3);
Return 0;
}
Output:
ANISHA TYAGI
16
PROGRAM 12
WAP TO FIND THE SUM OF DIGIT OF GIVEN NUMBER
Source code:
#include <stdio.h>
Int main ()
{
Int n, sum=0, m;
Printf(“Enter a number:”);
Scanf(“%d”,&n);
While(n>0)
{
m=n%10;
sum= sum+m;
n = n/10;
}
Printf(“Sum is =%d”, sum);
Return 0;
}
ANISHA TYAGI
17
Output:
PROGRAM 13
WAP TO PRINT FIBONACCI SERIES
Source code
#include <stdio.h> {
Int i, n;
Int t1 = 0, t2 = 1;
Int nextTerm = t1 + t2;
Printf(“Enter the number of terms:”);
Scanf(“%d”, &n);
Printf(“Fibonacci series: %d,%D”, t1, t2);
For (i=3; i<=n; ++i) {
Printf(“%d”, nextTerm);
t1 = t2;
ANISHA TYAGI
18
t2 = nextTerm;
nextTerm = t1 + t2;
}
Return 0;
}
Output:
PROGRAM 14
WAP to find whether the number is armstrong or not
Source code:
#include <stdio.h>
Int main () {
Int num, originalNum, remainder, result = 0;
Scanf(“%d”, &num);
originalNum = num;
ANISHA TYAGI
19
while (originalNum != 0) {
remainder += originalNum % 10;
result = remainder*remainder*remainder;
originalNum /= 10;
}
If (result == num)
Printf(“%d is an Armstrong number.”, num);
Else
Printf(“%d is not an Armstrong number.”, num);
Return 0;
}
Output:
ANISHA TYAGI
20
PROGRAM 15
WAP to find whether the number is palindrome or not
Source code:
#include <stdio.h>
Int main() {
Int n, reversed = 0, remainder, original;
Printf(“Enter an integer:”);
Scanf(“%d”, &n);
Original = n;
While (n!= 0) {
Remainder = n% 10;
Reversed = reversed * 10 + remainder;
n/= 10;
}
If (original == reversed)
Printf(“%d is a palindrome.”, original);
Else
Printf(“%d is not a palindrome.”, original);
Return 0;
ANISHA TYAGI
21
Output:
PROGRAM 16
WAP to check whether the number is prime or not
Source code:
#include <stdio.h>
Int main() {
Int n, i, flag = 0;
Printf(“Enter a positive integer:”);
Scanf(“%d”, &n);
If (n==0 || n ==1)
Flag = 1;
For (i = 2; I <= n/2; ++i) {
If (n % i == 0) {
Flag = 1;
Break;
}
ANISHA TYAGI
22
}
If (flag == 0)
Printf(“%d is a prime number.”, n);
Else
Printf(“%d is not a prime number.”,n);
Return o;
}
Output:
ANISHA TYAGI
23
PROGRAM 17
WAP to print sum of first n natural number
Source code:
#include <stdio.h>
Int main() {
Int n, i, sum = 0;
Printf(“Enter a positive integer:”);
Scanf(“%d”, &n);
For (i=1; i <= n; ++i) {
Sum += i;
}
Printf(“Sum = %d”, sum);
Return 0;
}
Output:
ANISHA TYAGI
24
PROGRAM 18
WAP to swap values by call by reference
Source code:
#include <stdio.h>
Void swap (int* num1, int* num2);
int main() {
int num1, num2;
printf(“Enter two numbers:”);
scanf(“%d%d”, &num1, &num2);
printf(“Before swapping in main n”);
printf(“Value of num1 = %d \n”, num1);
printf(“Value of num2 = %d \n\n”, num2);
swap(&num1, &num2);
printf(“After swapping in main n”);
printf(“Value of num1 = %d \n”, num1);
printf(“Value of num2 = %d \n\n”, num2);
return 0;
}
ANISHA TYAGI
25
Output:
PROGRAM 19
WAP to swap number by call by value
Source code:
#include <stdio.h>
void swap (int, int);
void main ()
{
int n1, n2;
ANISHA TYAGI
26
printf(“Enter the two numbers to be swapped \n”);
scanf(“%d%d”, &n1, &n2);
printf(“\n The values of n1 and n2 in the main function
before calling the swap function are n1=%d n2=%d”, n1, n2);
swap(n1, n2);
printf(“\n The values of n1 and n2 in the main function after
calling the swap function are n1=%d n2%d”, n1, n2);}
void swap( int n1, int n2)
{
int temp;
temp=n1;
n1=n2;
n2=temp;
printf(“\n The values of n1 and n2 in the swap function after
swapping are n1=%d n2=%d”, n1, n2);
}
Output:
ANISHA TYAGI
27
PROGRAM 20
WAP of Structure
Source code:
#include <stdio.h>
struct Person {
char name [50];
int citNo;
float salary;
} person1;
int main() {
strcpy(person1.name, “George Orwell”);
person1.citNo = 1984;
person1.salary = 2500;
print(“Name: %s\n”, person1.name);
printf(“Citizenship No. : %d\n”, person1.citNo);
printf(“Salary: %.2f”, person1.salary);
return 0;
}
ANISHA TYAGI
28
Output:
PROGRAM 21
WAP of union
Source code:
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
j.workerNo = 100;
printf(“Salary = %.1f\n”, j.salary);
printf(“Number of workers = %d”, j.workerNo);
return 0;
}
ANISHA TYAGI
29
Output:
PROGRAM 22
WAP of array of marks of student
Source code:
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
int main() {
int I;
printf(“Enter information of students: \n”);
for (i=0; i<5; ++i) {
s[i].roll = i + 1;
printf(“\nFor roll number%d, \n”, s[i].roll);
ANISHA TYAGI
30
printf(“Enter first name:”);
scanf(“%s”, s[i].firstName);
printf(“Enter marks:”);
scanf(“%f”, &s[i].marks);
}
printf(“Displaying Information:\n\n”);
for (i=0; i < 5; ++i) {
printf(“\nRoll number: %d\n”, i + 1);
printf(“First name:”);
puts(s[i].firstName);
printf(“Marks: %.1f”, s[i].marks);
print(“\n”);
}
return 0;
}
ANISHA TYAGI
31
Output:
ANISHA TYAGI
32
PROGRAM 23
WAP to count the number of elements in array
Source code:
#include <stdio.h>
int main()
{
int arr[]={10, 20, 30, 40, 50};
int n;
n= sizeof (arr)/sizeof (int);
printf(“Number of elements are: %d\n”, n);
return 0;
}
Output:
ANISHA TYAGI
33
PROGRAM 24
WAP to find the sum of elements of array
Source code:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
int length = sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < length; i++) {
sum = sum + arr[i];
}
printf(“Sum of all the elements of an array: %d”, sum);
return 0;
}
Output:
ANISHA TYAGI
34
PROGRAM 25
WAP to add two matrices
Source code:
#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]);
ANISHA TYAGI
35
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
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;
}
ANISHA TYAGI
36
Output:
ANISHA TYAGI
37
PROGRAM 26
WAP of matrices multiplication
Source code:
#include <stdio.h>
void getMatrixElements(int matrix[][10], int row, int column){
printf(“\nEnter elements: \n”);
for (int i =0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf(“Enter a%d%d: “, i + 1, j + 1);
scanf(“%d”, &matrix[i][j]);
}
}
void multiplyMatrices(int first[][10],
int second[][10],
int result[][10],
int r1, int c1, int r2, int c2) {
for (int i=0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
result[i][j] = 0;
}
}
ANISHA TYAGI
38
for (int i = 0; i < r1; ++i) {
for (int j = 0; j <c2; ++j) {
result [i][j] = 0;
}
}
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
result[i][j] += first[i][k]*second[k][j];
}
}
}
}
void display(int result[][10], int row, int column) {
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”)
}
}
ANISHA TYAGI
39
}
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);
while (c1!= r2){
printf(“Error! Enter rows and column again.\n”);
printf(“Enter rows and columns for the first matrix:”);
scanf(“%d%d”, &r1, &c1);
printf(“Enter rows and columns for the second matrix:”);
scanf(“%d%d”, &r2, &c2);
}
getMatrixElements(first, r1, c1);
getMatrixElements(seconf, r2, c2);
multiplyMatrices(first, second, result, r1, c1, r2, c2);
display(result, r1, c2);
return 0;
}
ANISHA TYAGI
40
Output:
ANISHA TYAGI
41
PROGRAM 27
WAP of transpose of matrix
Source code:
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf(“Enter rows and columns:”);
scanf(“%d%d”, &r, &c);
printf(“\nEnter matrix elements:\n”);
for (int i=0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf (“Enter element a%d%d:”, i +1, j + 1);
scanf(“%d”, &a[i][j]);
}
printf(“\nEntered matrix:\n”);
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf(“%d”, a[i][j]);
if (j==c-1)
printf(“\n”);
}
ANISHA TYAGI
42
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose [j][i] = a[i][j];
}
printf(“\nTranspose of the matrix:\n”);
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf(“%d”, transpose[i][j]);
if (j == r – 1)
print(“\n”);
}
return 0;
}
ANISHA TYAGI
43
Output:
ANISHA TYAGI
44
PROGRAM 28
WAP to find Fibonacci series using recursion
Source code:
#include <stdio.h>
int Fibonacci (int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
[
return Fibonacci (num – 1) + Fibonacci(num – 2 );
}
}
int main()
{
ANISHA TYAGI
45
int num;
printf(“Enter the number of elements to be in the series :”);
scanf(“%d”, &num);
for (int i = 0; i < num; i++)
{
printf(“%d,”, Fibonacci(i));
}
return 0;
}
Output:
ANISHA TYAGI
46
PROGRAM 29
WAP to find factorial of the given number using recursion
Source code:
#include <stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf(“Enter a positive integer:”);
scanf(“%d”,&n);
printf(“Factorial of %d = %1d”, n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumber(n-1);
else return 1;
}
Output:
ANISHA TYAGI
47
PROGRAM 30
WAP for the pointer of array
Source code:
#include <stdio.h>
int main() {
int data[5];
printf(“Enter elements:”);
for (int i=0; i<5; ++i)
scanf(“%d”, data + i);
printf(“You entered:\n”);
for (int i=0; i < 5; ++i)
printf(“%d\n”, *(data + i));
return 0;
}
ANISHA TYAGI
48
Output:
PROGRAM 31
WAP for pointer of structure
Source code:
#include <stdio.h>
#include <stdlib.h>
struct person {
int age:
float weight;
ANISHA TYAGI
49
char name[30];
};
int main()
{ struct person *ptr;
int i, n;
printf(“Enter the number of persons:”);
scanf(“%d”, &n);
ptr = (struct person*) malloc(n*sizeof(struct person));
for(i = 0; i < n; ++i)
{
print(“Enter first name and age respectively:”);
scanf(“%s %d”, (ptr+i)-> name, &(ptr+i)->age);
}
printf(“Displaying Information:\n”);
for(i = 0; i<n; ++i)
printf(“Name: %s\tAge: %d\n”, (ptr+i)->name, (ptr+i)->age);
return 0;
}
ANISHA TYAGI
50
Output:
PROGRAM 32
WAP of Binary search
Source code:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, arr[100];
printf(“Enter number of elements\n”);
scanf(“%d”,&n);
printf(“Enter %d integers\n”, n);
for (c=0; c<n; c++)
scanf(“%d”, &arr[c]);
printf(“Enter value to find\n”);
scanf(“%d”, &search);
first=0;
ANISHA TYAGI
51
last=n-1;
middle= (first+last)/2;
while (first <=last) {
if (arr[middle] < search)
first = middle + 1;
else if (arr[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;
}
ANISHA TYAGI
52
Output:
PROGRAM 33
WAP of Linear search
Source code:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf(“Enter number of elements in array\n”);
scanf(“%d”, &n);
printf(“Enter %d integer\n”, n);
for(c = 0; c < n; c++)
ANISHA TYAGI
53
scanf(“%d”, &arr[c]);
printf(“Enter a number to search\n”);
scanf(“%d”, &search);
for (c = 0; c < n; c++)
{
if (arr[c] == search)
{
print(“%d is present at location %d.\n”, search, c+1);
break;
}
}
if(c == n)
print (“%d isn’t present in the array.\n”, search);
return 0;
}
ANISHA TYAGI
54
Output:
PROGRAM 34
Bubble sort
Source code:
#include <stdio.h>
int main()
{
int arr[100], n, c, d, swap;
print(“Enter number of elements\n);
scanf(“%d”, &n);
printf(“Enter %d intergers\n”, n);
for (c = 0; c < n; c++)
scanf(“%d”, &arr[c]);
ANISHA TYAGI
55
for (c=0; c<n-1; c++)
{
for (d = 0; d < n-c-1; d++)
{
if (arr[d] > arr[d+1])
{
swap = arr[d];
arr[d] = arr [d+1]
arr[d+1] = swap;
}
}
}
print(“Sorter list in ascending order: \n”);
for (c = 0; c < n; c++)
printf(“%d\n”, arr[c]);
return 0;
}
ANISHA TYAGI
56
Output:
PROGRAM 35
Selection sort
Source code:
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf(“Enter number of elements”);
ANISHA TYAGI
57
scanf(“%d”, &n);
printf(“Enter %d Number”,n);
for (i = 0; i < n; i++)
scanf(“%d”, &a[i]);
for (i = 0; i < n-1; i++)
{
position = i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position = j;
}
if(position != i)
{
swap = a[i];
a[i] = a[position];
a[position] = swap;
}
}
printf(“Sorter Array:\n”);
for(i = 0; i < n; i++)
printf(“%d\n”, a[i]);
ANISHA TYAGI
58
return 0;
}
Output:
ANISHA TYAGI
59
PROGRAM 36
WAP to print a string
Source code:
#include <stdio.h>
int main()
{
char name[20];
printf(“Enter name:”);
scanf(“%s”, name);
printf(“Your name is %s.”, name);
return 0;
}
Output:
ANISHA TYAGI
60