0% found this document useful (0 votes)
2 views35 pages

Pps Lab Programs

The document contains a collection of C programming exercises aimed at solving various problems, including finding maximum and minimum numbers, calculating simple and compound interest, generating Fibonacci sequences, and performing matrix operations. Each exercise includes a brief description, the corresponding C code, and expected output. The exercises cover fundamental programming concepts such as loops, conditionals, functions, arrays, and file handling.

Uploaded by

ggjkkvhh790
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)
2 views35 pages

Pps Lab Programs

The document contains a collection of C programming exercises aimed at solving various problems, including finding maximum and minimum numbers, calculating simple and compound interest, generating Fibonacci sequences, and performing matrix operations. Each exercise includes a brief description, the corresponding C code, and expected output. The exercises cover fundamental programming concepts such as loops, conditionals, functions, arrays, and file handling.

Uploaded by

ggjkkvhh790
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

PROGRAMMING FOR PROBLEM SOLVING LAB

[Link]. I Year I Sem. L T PC


Simple numeric problems:
1. Write a program for fiend the max and min from the three numbers.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("Maximum number is a = %d",a);
else if(b>a && b>c)
printf("Maximum number is b = %d",b);
else
printf("Maximum number is c = %d",c);
printf("\n");
if(a<b && a<c)
printf("Minimum number is a = %d",a);
else if(b<a && b<c)
printf("Minimum number is b = %d",b);
else
printf("Minimum number is c = %d",c);
getch();
}
Output:

1
2. Write the program for the simple, compound interest.
Program:
#include<stdio.h
>
#include<conio.h
>
#include<math.h
> void main()
{
int p,t;
float
r,si,amount,ci;
clrscr( );
printf("Please enter principle,time and rate of interest"); scanf("%d%d
%f",&p,&t,&r);
si=p*t*r/100;
printf("\nSimple interest =
%.3f",si); amount=p*pow((1
+r/100),t); ci=amount-p;
printf("\nCompound interest =
%.3f",ci); getch( );
}
Output:

2
3. Write a program that prints a multiplication table for a given number and the number of
rows in the table. For example, for a number 5 and rows = 3, the output should be:
5x1=5
5 x 2 = 10
5 x 3 = 15

Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, range;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n); printf("Enter
the range: "); scanf("%d",
&range); for(i=1; i<=range;i+
+)
{
printf("%d * %d = %d \n",n,i, n*i);
}
getch();
}

Output:

3
4. Write a program that shows the binary equivalent of a given positive number between 0
to 255.

Program:

#include <stdio.h>
#include<conio.h>
int binary_conversion(int);
void main()
{
int num, bin;
clrscr();
printf("Enter a decimal number: ");
scanf("%d", &num);
bin = binary_conversion(num);
printf("The binary equivalent of %d is %d\n", num, bin); getch();
}
int binary_conversion(int num)
{
if (num == 0)
{
return 0;
}
else
{
return (num % 2) + 10 * binary_conversion(num / 2);
}
}

Output:

4
Expression Evaluation:

5. Write a C program, which takes two integer operands and one operator from the user,
performs the operation and then prints the result. (Consider the operators +,-
,*, /, % and use Switch Statement).

Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d",
c); break;
case '*': c = a * b;
printf("multiplication of two numbers is %d",
c); break;
case '/': c = a / b;
printf("remainder of two numbers is %d",
c); break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}
Output:

5
6. Write a program that finds if a given number is a prime number
Program:
#include<stdio.h>
int main()
{
int n,i,flag=0; printf("\
nEnter a number:”);
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{flag=1; break;
}
}
If(flag==0)
printf(“%d is a prime number”,n);
else
printf(“%d is not a prime number”,n);
return(0);
}

Output:

Enter a number: 29
29 is a prime
number

6
7. Write a C program to find the sum of individual digits of a positive integer and test
given number is palindrome.

Sum of individual digits of a positive integer:


Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int number = 0, digit = 0, sum = 0;
clrscr();
printf("Enter any number\n ");
scanf("%d", &number);
while (number != 0)
{
digit = number % 10; sum
= sum + digit; number =
number / 10;
}
printf ("Sum of individual digits of a given number is %d", sum); getch();
}

Output:

7
Test given number is palindrome.
Program:
#include <stdio.h>
void main()
{
int number, t, rev=0, rmndr;
printf("Please enter a number to check Palindrome : ");
scanf("%d",&number);
printf("\nEntered number: %d", number); t =
number;
while (number > 0)
{
rmndr = number%10; rev
= rev*10 + rmndr;
number = number/10;
}
printf("\nReversed number: %d", rev); if(t
== rev)
{
printf("\nEntered number %d is a palindrome", t);
}
else
{
printf("\nEntered number %d is not a palindrome", t);
}
}

Output:

8
8. A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0
and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.

Program:
#include <stdio.h> int
main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: "); for
(i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2; t1
= t2;
t2 = nextTerm;
}
return 0;
}

Output

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

9
Arrays and Pointers and Functions:
9. Write a C program to find the minimum, maximum and average in an array of integers.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size int
main()
{
int arr[MAX_SIZE]; int
i, max, min, size;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
max = arr[0];
min = arr[0];
for(i=1; i<size; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
if(arr[i] < min)
{
min = arr[i];
}
}
printf("Maximum element = %d\n", max);
printf("Minimum element = %d", min); return 0;
}

10
Output:

11
10. Write a C program that uses functions to perform the following:
Addition of Two Matrices
#include <stdio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10]; clrscr();

printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d", &m,


&n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++) for


(d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n"); for (c =
0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) { for (d
= 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

return 0;
}

12
Output:

Multiplication of Two Matrices


Program:

#include <stdio.h> int


main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10]; printf("Enter number
of rows and columns of first matrix\n"); scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n"); for (c
= 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n"); scanf("%d%d",
&p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n"); else
{
printf("Enter elements of second matrix\n"); for (c
= 0; c < p; c++)
for (d = 0; d < q; d++) scanf("%d",
&second[c][d]);
for (c = 0; c < m; c++) { for
(d = 0; d < q; d++) {

13
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n"); for
(c = 0; c < m; c++) {
for (d = 0; d < q; d++) printf("%d\
t", multiply[c][d]); printf("\n");
}
}
getch();
return 0;
}

Output:

14
Transpose of a matrix with memory dynamically allocated for the new matrix as row and
column counts may not be same

Program:

#include <stdio.h> int


main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix printf("\
nEnter elements of 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]);
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]); if
(j == c-1)
printf("\n\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n"); for(i=0;
i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);

15
if(j==r-1) printf("\
n\n");
}
return 0;
}

Output:

16
11. Write a program for reading elements using pointer into array and display the values
using array.

Program:

#include <stdio.h> int


main()
{
int data[5], i; printf("Enter
elements: "); for(i = 0; i < 5; +
+i) scanf("%d", data + i);
printf("You entered: \n");
for(i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
return 0;
}
Output:
Enter elements: 1
2
3
5
4
You entered: 1
2
3
5
4

17
12. Write a C program using pointers to read in an array of integers and print its elements in
reverse order.

Program:
#include<stdio.h>
#include<conio.h>
#define MAX 30

void main() {
int size, i, arr[MAX]; int
*ptr;
clrscr();

ptr = &arr[0];

printf("\nEnter the size of array : ");


scanf("%d", &size);

printf("\nEnter %d integers into array: ", size); for (i


= 0; i < size; i++) {
scanf("%d", ptr);
ptr++;
}

ptr = &arr[size - 1];


printf("\nElements of array in reverse order are :"); for (i =
size - 1; i >= 0; i--) {
printf("\nElement%d is %d : ", i, *ptr); ptr--;
}

getch();
}

18
Output :
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11

13. Write a C program which copies one file to another, replacing all lowercase characters with their
uppercase equivalents.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("[Link]", "r"); if
(fp1 == NULL)
{
puts("File does not exist..");
exit(1);
}
fp2 = fopen("[Link]", "w"); if
(fp2 == NULL)
{
puts("File does not exist..");
fclose(fp1);
exit(1);
}
while((ch=fgetc(fp1))!=EOF)
{
ch = toupper(ch);
fputc(ch,fp2);
}
printf("\nFile successfully copied..");
return 0;
}
Output:

Content in [Link] file.

19
20
[Link] a C program to merge two files into a third file (i.e., the contents of the first file followed by those
of the second are put in the third file).
#include<stdio.h>
void main()
{
FILE *f1,*f2,*f3; char s[100];
f1=fopen("D:\[Link]","r");
f2=fopen("D:\[Link]","r");
f3=fopen("D:\[Link]","w"); if(f1==NULL || f2==NULL || f3==NULL)
{
printf("error opening file"); exit(0);
}
while(fgets(s,99,f1)!=NULL) fputs(s,f3);
while(fgets(s,99,f2)!=NULL) fputs(s,f3);
fclose(f1);
fclose(f2);
fclose(f3);
} OUTPUT:
Before After
[Link] [Link] [Link] [Link] [Link]

PQRqrs
ABC t ABC PQR ABC
DEF STUuv DEF STU DEF
PQR
STU

21
[Link] a C program that uses functions to perform the following operations:
a)To insert a sub-string into a given main string from a given position.
#include<stdio.h>
#include<string.h>

void insertStr(char m[100],char s[100],int pos); int


main()
{
char m[100],s[100]; int pos,n;
printf("\n enter main string for insertion:");
gets(m);
printf("\nenter sub string:");
gets(s);
printf("\nEnter position:");
scanf("%d",&pos);
insertStr(m,s,pos);
printf("\nMain string after insertion: %s",m);
getch();
return(0);
}
void insertStr(char m[100],char s[100],int pos)
{
int i,lm=strlen(m),ls=strlen(s);
for(i=lm;i>=pos;i--) m[i+ls]=m[i];
for(i=0;i<ls;i++) m[i+pos]=s[i];
}

OUTPUT:
enter main string for insertion:comer enter
sub string:put
Enter position:3
Main string after insertion: computer

22
b)To delete n Characters from a given position in a given string
#include<stdio.h>
#include<string.h>
void deleteStr(char m[100],int n,int pos); int
main()
{
char m[100],s[100]; int pos,n; printf("\nEnter
main string for deletion:"); gets(m);
printf("\nEnter no of characters to be deleted:");
scanf("%d",&n);
printf("\nEnter position:");
scanf("%d",&pos);
deleteStr(m,n,pos);
printf("\nmain string after deletion: %s",m); getch();
return(0);
}
void deleteStr(char m[100],int n,int pos)
{
int i,Len=strlen(m); for(i=pos;i<=(Len+1-n );i++)
m[i]=m[i+n];
}

OUTPUT:
Enter main string for deletion: abcdef Enter
no of characters to be deleted:2 Enter
position:3
main string after deletion: abcf

23
[Link] a C program to determine if the given string is a palindrome or not (Spelled same in both directions
with or without a meaning like madam, civic, noon, abcba, etc.)
#include <stdio.h>
#include <string.h>

int main(){
char string1[20];
int i, length;
int flag = 0;

printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}

if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}

24
[Link] a C program that displays the position of a character ch in the string S or – 1 if S doesn’t contain
ch.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
found = strstr(s, t);
if(found)
{
printf("Second String is found in the First String at %d position.\n", found - s);
}
else
{
printf("-1");
}
getch();
}
Input & Output:
[Link] the first string:
kali
Enter the string to be searched:
li
second string is found in the first string at 2 position [Link]
the first string:
nagaraju
Enter the string to be searched:
raju
second string is found in the first string at 4 position [Link]
the first string:
nagarjuna
Enter the string to be searched: Ma
-1

25
[Link] a C program to count the lines, words and characters in a given text.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[100];
int i = 0, l = 0, f = 1;
clrscr();
puts("Enter any string\n");
gets(str);
for(i = 0; str[i] !='\0'; i++)
{
l = l + 1;
}
printf("The number of characters in the string are %d\n", l); for(i = 0;
i <= l-1; i++)
{
if(str[i] == ' ')
{
f = f + 1;
}
}
printf("The number of words in the string are %d", f); getch();
}
Input & Output:

Enter any string


abc def ghi jkl mno pqr stu vwx yz
The number of characters in the string are 34
The number of words in the string are 9

26
[Link] a C program that uses non-recursive function to search for a Key value in a given list of integers
using linear search method.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a[20], n, key, flag =
0; clrscr();
printf(“Enter the size of an array \n”);
scanf(“%d”, &n);
printf(“Enter the array elements”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);

27
}
printf(“Enter the key elements”);
scanf(“%d”, &key);
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
flag = 1;
break;
}
}
if(flag == 1)
printf(“The key elements is found at location %d”, i + 1);
else
printf(“The key element is not found in the array”);
getch();
}
Input & Output:
Enter the size of an array 6
Enter the array elements 50 10 5 200 20 1
Enter the key element 1
The key Element is found at location 6

[Link] a C program that uses non-recursive function to search for a Key value in a given sorted list of
integers using binary search method.
#include <stdio.h>
int BinarySearching(int arr[], int max, int element)
{
int low = 0, high = max - 1,
middle; while(low <= high)
{
middle = (low + high) /
2; if(element >
arr[middle]) low =
middle + 1;
else if(element < arr[middle])
high = middle - 1;
else
return middle;

28
}
return -1;
}
int main()
{
int count, element, limit, arr[50], position;
printf("Enter the Limit of Elements in Array:\
t"); scanf("%d", &limit);
printf("Enter %d Elements in Array: \n",
limit); for(count = 0; count < limit; count++)
{
scanf("%d", &arr[count]);
}
printf("Enter Element To Search:\t");
scanf("%d", &element);
position = BinarySearching(arr, limit,
element); if(position == -1)
{
printf("Element %d Not Found\n", element);
}
else
{
printf("Element %d Found at Position %d\n", element, position + 1);
}
return 0;
}
OUTPUT:
Enter the Limit of Elements in Array: 5

Enter 5 Elements in Array:

57

29
93

25

57

76

Enter Element to Search: 76

Element 76 Found at Position 5

[Link] a C program that implements the Bubble sort method to sort a given list of integers in ascending
order.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, a[20], temp, i, j;
clrscr();
printf("Enter the size of the array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] =
temp;
}
}
}
printf("The sorted array is\n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}

30
Output:
Enter the size of the array: 5
Enter the array elements: 50 40 30 20 10
The sorted array is: 10 20 30 40 50

[Link] a C program that sorts the given array of integers using selection sort in descending order
#include<stdio.h
>
#include<conio.h
> void main()
{
int n, a[20], min, temp,
i, j; clrscr();
printf("Enter the size of the array\
n"); scanf("%d", &n);
printf("Enter the array elements\n");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < n - 1; i++)
{
min = i;
for(j = i + 1; j < n; j++)
{
if(a[j] >
a[min]) min =
j;
}
temp = a[i];
a[i] = a[min];
a[min] =
temp;
}
printf("The sorted array is\
n"); for(i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();

}
Output:
Enter the size of the array: 7
Enter the array elements: 1 2 3 4 5 6 7

31
The Sorted array is: 7 6 5 4 3 2 1

[Link] a C program that sorts the given array of integers using insertion sort in ascending order
# include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
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 = 1 ; c <= n - 1; c++)
{
d = c;
while ( d > 0 && array[d-1] > array[d])
{
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++)
{
printf("%d\n", array[c]);
}

32
return 0;
}
OUTPUT:

[Link] a C program that sorts a given array of names.


#include <stdio.h>
#include <string.h>
void main()
{
char name[10][8], tname[10][8],
temp[8]; int i, j, n;
printf("Enter the value of n \n");
scanf("%d", &n);

33
printf("Enter %d names n\n", n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("\n \n");
printf("Input NamestSorted names\n");
printf(" \n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf(" \n");
}

34
OUTPUT:

Enter the value of n 4


Enter 4 names n
apple
boy
zeebra
cat

Input NamestSorted names

apple apple

boy boy

zeebra cat

cat zeebra

35

You might also like