Practical of 'PPS' file

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

INTERNATIONAL INSTITUTE

OF TECHNOLOGY
&MANAGEMENT MURTHAL,
SONIPAT, HARYANA

Practical file
Of
Programming and
problem solving using C

Submitted to: Submitted By:


Ms. Kirti
INDEX

S.No. PROGRAM DATE REMARKS

1. Program to find the largest of three numbers using


If and else.

2. Program to find the largest of n numbers.

3. Program to multiply two matrices.

4. Program to read a string and write it in reverse


Order.

5. Program to concatenate two strings.

6. Program to swap two integers using pointers.

7. Program for Bubble sort.

8 Program for Selection sort.


PROGRAM NO : 1
/*Program to find the largest of three numbers using if and else*/

#include<stdio.h>
int main()
{
int num1, num2, num3;
printf("\n Ener value of num1, num2 and num3:");
scanf("%d %d %d", &num1, &num2, &num3);
if ((num1>num2)&&(num1>num3))
printf("\n Number1 is greatest amng three");
else if((num2>num3)&&(num2>num1))
printf("\n Number2 is greatest among three");
else
printf("\n Number3 is greatest among three");
return 0;
}
Output
PROGRAM NO: 2
/* Program to find the largest number out of n numbers using for statement*/

#include <stdio.h>
#include <conio.h>
int main()
{
int i,num,n,large=0;
printf("How many numbers: ");
scanf("%d",&n);

for(i=0; i<n; i++)


{
printf("\nEnter number %d: ",i+1);
scanf("%d",&num);
if(num>large)
large=num;
}
printf("\n\nThe Largest Number is %d",large);

getch();
return 0;
}
OUTPUT
PROGRAM NO: 3
/* Program to multiply two matrices*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output
PROGRAM NO: 4
/*Program to read a string and write it in reverse ordere*/
#include <stdio.h>
int main()
{
char s[1000], r[1000];
int begin, end, count = 0;

printf("Input a string\n");
gets(s);

while (s[count] != '\0')


count++;
end = count - 1;

for (begin = 0; begin < count; begin++) {


r[begin] = s[end];
end--;
}

r[begin] = '\0';
printf("%s\n", r);
return 0;
}
Output
PROGRAM NO : 5
/*Program to concatenate two strings*/
#include <stdio.h>
#include <string.h>
int main()
{
char Str1[100], Str2[100];
int i, j;

printf("\n Please Enter the First String : ");


gets(Str1);
printf("\n Please Enter the Second String : ");
gets(Str2);
for (i = 0; Str1[i]!='\0'; i++);
for (j = 0; Str2[j]!='\0'; j++, i++)
{

Str1[i] = Str2[j];

}
Str1[i] = '\0';

printf("\n String after concatenation = %s", Str1);

return 0;
}
Output
PRACTICAL NO : 6
/*Swapping two integers using pointers*/
#include <stdio.h>
void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}

int main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);
printf("Before Swapping: num1 is: %d, num2 is:
%d\n",num1,num2);
swap(&num1,&num2);
printf("After Swapping: num1 is: %d, num2 is:
%d\n",num1,num2);
return 0;
}
Output
PRACTICAL NO : 7
Bubble Sort

//Bubble Sort
#include<conio.h>
#include<stdio.h>
int main()
{
int i,j,a[20],n,tmp;
printf("Enter how much element you want in this array:\
n"); scanf("%d",&n);
printf("Enter %d in the array :\n",n);
for (i=0;i<n;i++) scanf("\n
%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
printf("Sorted array is :\n");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
} getch();

}
OUTPUT
PRACTICAL NO : 8
Selection Sort
#include
<stdio.h>
int main()
{
int data[100],i,n,steps,temp;
printf("Enter the number of elements to be sorted:
"); scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)

{ if(data[steps]>d
ata[i])
//To sort in descending order, change > to <.
{
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending
order: "); for(i=0;i<n;+
+i) printf("%d
",data[i]); return 0;
}
OUTPUT

You might also like