0% found this document useful (0 votes)
4 views33 pages

UNIT-2 C Programming

UNIT-2 C Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views33 pages

UNIT-2 C Programming

UNIT-2 C Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

UNIT-2 Arrays and Strings

Introduction to Arrays: Declaration, Initialization – One dimensional array –Two


dimensional arrays – String operations and String functions Illustrative problems:
Compute Mean. Matrix Operations (Addition, Transpose), Palindrome of a given String.

1. Introduction to Arrays
 An Array is a collection of similar data elements. These data elements have same data
types
 Array is a collection of elements of same data type stored under common name.
 An array is a data structure that is used for storage of homogeneous data.
 It is classified into three types
i) One dimensional Array
ii) Two dimensional Array
iii) Multi-dimensional Array
1.1 Characteristics of an Array
 The elements of the array are stored in continuous memory location.
 Individual elements of an array can be accessed with the integer value known as index.
 Array index starts from 0.

 Memory space of an array can be calculated as


Size of datatype * number of elements of an array
 The location of an array in the location of its first element.
 A variable that can store multiple values is an array variable.
 It is a derived data type which stores related information together.
 The length of an array is the number of elements in the array.
 Arrays are used in the situation where we are asked read and prints the marks of 30
students .It is inefficient way of declaring 30 variables to hold marks. So the concept of
Array is used where one common variable is used for assigning 30 values.
1.2 Advantage of an array
 It is used to represent multiple data items of same type by using only single name.
 2D arrays are used to represent matrices.
 It allows random accessing of elements i.e. any element of the array can be randomly
accessed using indexes
1.3 Disadvantage of an array
 Insertion and deletion of elements in an array is difficult
 Size of the Array should be known in advance.
 Elements in the Array must be same data type

2. One dimensional array - Declaration, Initialization


 One dimensional array is collection of elements of same data type organized as a
simple linear sequence.
 Elements of an array can be accessed by using a single subscript.
2.1 Declaration of one dimensional Array
We need to declare the array variable before they are used. To declare and define
an array we must specify its name, type and size.
Syntax:
datatype arrayname[size];
datatype : kind of values it can store Example int, float, char, double.
arrayname : variable name of an Array
Size : maximum number of values that an array can hold.
Example
int a[4];

2.2 Initialization of One Dimensional Array


2.2.1. Compile time initialization: Initialization of the values of an array at the time
of declaration.
datatype arrayname [size] = {list of values separated by comma};
Example
int a[4]={20,45,67,89};

Sample program1:

Array element can be accessed using the index

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4]={20,45,67,89};
printf(“%d \t%d\t%d\t%d”,a[0],a[1],a[2],a[3]);
getch();
}

Output:

20 45 67 89

Sample program2:
The efficient way of accessing array elements is using loop

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4]={20,45,67,89};
int i;
for(i=0;i<4;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
Output:
20 45 67 89

Note:
By default the elements of an array are not initialized. They may contain some garbage
value, so before using array we must initialize the array or read some meaningful data into it.
2.3 Run time initialization:

The value of an Array can be initialized during run time.

int a[4];
for(i=0;i<4;i++)
{
scanf(“%d”,&a[i]);
}

Sample program 3

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4],i;

printf(“Enter the Elements\n”);


for(i=0;i<4;i++)
{
scanf(“%d\t”,&a[i]);
}
Printf(“The Elements are\n”);
for(i=0;i<4;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
Output:

Enter the Elements


20 45 67 89
The Elements are
20 45 67 89
3. Two dimensional array
 It is a collection of data elements of same data type arranged in rows and columns.
 It is collection of one dimensional array.
 An array with two subscript is called two dimensional arrays.
 It enables us to store multiple rows of elements such as table of values or matrix.
 Referring to Array Elements:
To access the elements of a two-dimensional array, we need a pair of indices: first index
selects the row and the second index selects the column.
Syntax:
datatype arrayname[size of row][size of column];
Example: int A[3][2];
Col Col
0 1
Row 0 A[0][0] A[0][1]
Row 1 A[1][0] A[1][1]
Row 2 A[2][1] A[2][2]

4.1 Compile Time Initialization


 An two-dimensional array can be initialized along with declaration.
 For two-dimensional array initialization, elements of each row are enclosed within
curly braces and separated by commas.
 All rows are enclosed within curly braces.
Syntax:
datatype arrayname[size of row][size of column]={values separated by comma};

Example:

int a[3][2]={{10,20},{30,40},{50,60}};

Col 0 Col 1

Row 0 10 20
Row 1 30 40
Row 2 50 60
Sample program 4:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][2]={{10,20},{30,40},{50,60}};
printf(“%d\t”,a[0][0]);
printf(“%d\n”,a[0][1]);
printf(“%d\t”,a[1][0]);
printf(“%d\n”,a[1][1]);
printf(“%d\t”,a[2][0]);
printf(“%d\n”,a[2][1]);
}

Output:
10 20
30 40
50 60
Sample program 5

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2]={{10,20},{30,40},{50,60}};
int i,j;
printf("\nThe Matrix");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",a[i][j]);
}
}
getch();
}

Output:

10 20
30 40
50 60
4.2 Run time initialization:
Two-dimensional array can be initialization at run time using loop.

int a[2][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &a[i][j]);
}
}

Sample program 5:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][3];
int i,j;
printf(“Enter the elements\n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &a[i][j]);
}
}

printf(“\nThe elements are”);


for(i=0;i<2;i++)
{
printf(“\n”);
for(j=0;j<3;j++)
{
printf(“%d\t”, a[i][j]);
}
}

Output:
Enter the elements
1
2
3
4
The elements are
1 2
2 4
4. String operations

 A string is a set of characters or sequence of two or more characters enclosed within


double quotes.
 The character array is called as String.
 It always ended with null character (\0).
Syntax:
datatype variablename[size];
Example:
char name[10];
Initializing a character array:
char name[10]=”IYEAR”;
Or
char name[10]={„I‟,‟Y‟,‟E‟,‟A‟,‟R‟,‟\0‟};
Or
char name[ ]=”IYEAR”;

Array of characters:

I Y E A R \0

Reading and writing string:


The %s control string can be used in scanf() to read the string and the same may be used in
printf() to display the output.
1. Assign a string input and display using printf
#include<stdio.h>
#include<conio.h>
void main()
{
char name[10]=”IYEAR”;
printf(“The string is: %s”,name);
getch();
}
Output:
The string is: IYEAR
2. Assign a string input and display using puts
#include<stdio.h>
#include<conio.h>
void main()
{
char name[10]=”IYEAR”;
puts(name);
getch();
}
Output:
The string is: IYEAR

3.To get a string input using scanf and display using printf:
#include<stio.h>
#include<conio.h>
void main()
{
char name[10];
printf(“Enter the string : ”);
scanf(“%s”, name);
printf(“The string is: %s”,name);
getch();
}
Output:
Enter the string: IYEAR
The string is: IYEAR
4.To get a string input using gets and display using puts:
#include<stdio.h>
#include<conio.h>
void main()
{
char name[10];
printf(“Enter the string : ”);
gets(name);
puts(name);
getch();
}
Output:
Enter the string: IYEAR
The string is: IYEAR
5. String functions

In C there are large number of string function. These functions use the standard header file
string.h.
5.1 C string functions are:
1.strlen(s1)
2.strcpy(s1,s2)
3.strcmp(s1,s2)
4.strcat(s1,s2)
5.strcmpi(s1,s2)
6.strrev(s1)
7.strlwr(s1)
8.strupr(s1)
9.strstr(s1,s2)
1.strlen(s1)
 It is returns the length of a string.
Syntax:
strlen (“string”);
or
variable = strlen(“string”);
or
strlen (variablename);

Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
clrscr();
printf("Enter the string");
gets(s1);
printf("The length of the string",strlen(s1));
getch();
}
Output:
Enter the string
C Program
The length of the string is 9

2.strcpy(s1,s2)
 It copy the string s2 to the string s1.
Syntax:
strcpy (s1 , s2);

Example Program to copy two strings

#include <stdio.h>
#include <string.h>
void main()
{
char s1[10], s2[10];
printf("Input a string\n");
gets(s1);
strcpy(s2, s1);
printf("Source string:%s\n", s1);
printf("Destination string: %s\n", s2);
getch();
}

Output :
Input a string: 1 year
Source string: 1 year
Destination string: 1 year

3.strcmp(s1,s2)
 It compares two strings. string s1 compared with string s2 character by character
until the corresponding character differs or until the end of the string is reached.
 Returns 0 if s1 is same as s2.
 Returns <0 if sl < s2.
 Returns >0 if s1 > s2
Syntax:
strcmp(s1,s2);

Example Program For String Comparision

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
int res;
clrscr();
printf("Enter String1\n");
gets(s1);
printf("Enter String2\n");
gets(s2);
res=strcmp(s1,s2);
if(res==0)
{
printf("\nString are equal");
}
else
{
printf("\nStrings are not equal");
}
getch();
}
Output:
Enter String1
Hello
Enter String2
Hai
Strings are not equal
4.strcat(s1,s2 )
 String concatenate function append(adding) a string s2 to the string s1 and
returns a pointer to string s1.
Syntax:
strcat(s1,s2);

Example Program to concatenate two strings.


#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char s1[10], s2[10];
printf("Enter the first string\n");
gets(s1);
printf("Enter the second string\n");
gets(s2);
strcat(s1,s2);
printf("String obtained on concatenation is %s\n",s1);
getch();
}
Output:
Enter the first string: first
Enter the second string: year
String obtained on concatenation is: first year
5.strcmpi( s1,s2 )
It is same as strcmp() function. But, strcmpi( ) function is not case sensitive. i.e, “A”
and “a” are treated as same characters. Where as, strcmp() function treats “A” and “a” as
different characters.
Syntax:
strcmpi(s1,s2);

Example Program For String Comparision ignoring case sensistive

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
int res;
clrscr();
printf("Enter String1\n");
gets(s1);
printf("Enter String2\n");
gets(s2);
res=strcmpi(s1,s2);
if(res==0)
{
printf("\nString are equal");
}
else
{
printf("\nStrings are not equal");
}
getch();
}

Output:
Enter String1
Hello
Enter String2
hello
Strings are equal
6.strrev()
 It reverses the given string.

Syntax:
strrev(s1);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
clrscr();
printf("Enter the string");
gets(s1);
printf("Reverse of the string",strrev(s1));
getch();
}
Output:
Enter the string: civil
Reverse of the string: livic

7.strlwr()
 Converts given string to lowercase.
Syntax:
strlwr(s1);

8.strupr()
 Converts given string to uppercase.
Syntax:
strupr(s2);

C Program To Convert Upper Case And Lower Case Of The String


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
printf("Enter the string in uppercase\n");
gets(str1);
printf("The lower case %s",strlwr(s1));
printf("\nThe upper case %s",strupr(s1));
getch();
}
Output:
Enter the string in uppercase
C PROGRAM
The lower case c program
The upper case C PROGRAM
9.strstr()
 The strstr() function locates the first occurrence of the string in the string1.
Syntax:
strstr(string1,”string”);
Example:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s1[20]="hello hai IYEAR";
printf("the substring in s1: %s",strstr(s1,"hai"));
getch();
}
Output:
The substring in s1 is hai IYEAR
5.2 String of arrays:
 The list of strings are stored by using two dimensional character array is termed as
string of arrays.
Synatax:

datatype arrayname[row-size][column-size];

row-size: number of string


column-size: maximum number of character in each string

Example:
char a[4][7];
//it can store 4 strings and each string stores maximum 7 characters
Initialization of String of Array:

char A[4][5]={“Ram”,”Raj”,”Ravi”,”Jai”};

A[0] R a m \0
A[1] R a j \0
A[2] R a v i \0
A[3] J a i \0
1.Example Program For Sorting Of String In C Language
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int i, j, n;
char str[20][20],temp[20];
clrscr();
printf("Enter the no. of strings to be sorted");
scanf("%d",&n);
printf("\nEnter the string");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("\nThe sorted strings\n");
for(i=0;i<n;i++)
{
printf("\n%s",str[i]);
}
getch();
}
Output:

Enter the no. of strings to be sorted 3


Php
Mysql
C program

The sorted strings


C program
Mysql
Php
5.3 String operation without using String operation
 String operation without using string function is performed by checking null
character,(„\0‟).
 The null character is the end of the string. The string is accessed character by
character using loop (for, while, do-while)
i)String length without using string function
#include<stdio.h>
#include<conio.h>
void main()
{
char str[100],temp;
int i=0,len=0;
clrscr();
printf("\nEnter the string :");
gets(str);
while(str[i])
{
len++;
i++;
}
printf("\nLength of string is :%d",len);
getch();
}
Output:
Enter the string: C program
Length of string: 9
ii) String reverse without using string function
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[100],temp;
int i=0,j;
clrscr();
printf("\nEnter the string :");
gets(str);
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("\nReverse string is :%s",str);
getch();

}
output
Enter the string:c program
Reverse string is:margop c

iii)converting upper case string to lower case string without using string function
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],lower[20];
int i=0;
clrscr();
printf("\nEnter the string in upper case:");
gets(s1);
while(s1[i])
{
if(s1[i]>='A' && s1[i]<='Z')
lower[i]=s1[i]+32;
else
lower[i]=s1[i];
i++;

lower[i]='\0';
printf("\nLower String");
puts(lower);
getch();

Output:
Enter the string in upper case: C PROGRAM
Lower String c program

iv) Converting lower case string to upper case string without using string function
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],upper[20];
int i=0;
clrscr();
printf("\nEnter the string:");
gets(s1);
while(s1[i])
{
if(s1[i]>='a' && s1[i]<='z')
upper[i]=s1[i]-32;
else
upper[i]=s1[i];
i++;

}
upper[i]='\0';
printf("\nUpper String");
puts(upper);
getch();

}
output:
Enter the string in lower case: c program
Upper String C PROGRAM
v) Concatenating two string without using string function
#include<stdio.h>
#include<string.h>
void main()
{
char s1[50], s2[30];
int i,j=0;
clrscr();
printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
i = strlen(s1);
while(s2[j]!='\0')
{
s1[i]=s2[j];
i++;
j++;
}
s1[i] = '\0';
printf("\nconcated string is:%s",s1);
getch();
}
output:
Enter String 1:computer
Enter String 2:programming
Concated string is : computerprogramming
6. Illustrative problems:

6.1 Compute Mean


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],sum=0,avg;
int i,j;
clrscr();
printf("Enter Elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
sum=sum+a[i];
}
printf("\nThe Sum of elemnts in an array is %d",sum);
avg=sum/5;
printf("\nThe Avg of elemnets in an array is%d",avg);
getch();
}
Output:

Enter Elements
2
3
4
5
6
The Sum of elements in an array is 20
The Avg of elements in an array is 4

6.2 Matrix Operation( Addition,Transpose)


C Program to find the addition of two matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2];
int i,j,k;
clrscr();
printf("Enter Matrix A");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nThe result");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}

Output:
Enter Matrix A
12
34
Enter Matrix B
12
34
The result
24
68
C Program to print Transpose of matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2] , i, j;
clrscr();
printf("Enter matrix A");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",a[i][j]);
}
}
printf("\nTranspose");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",a[j][i]);
}
}

getch();
}

Output:
Enter matrix A
1 2
3 4
Transpose
1 3
2 4
6.3 Palindrome of a given String.

Example 1
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0;i < len ;i++)
{
if(str[i] != str[len-i-1])
{
temp = 1;
break;
}
}
if (temp==0) {
printf("String is a palindrome");
}
else {
printf("String is not a palindrome");
}
return 0;
}
Output:
Enter a string: madam
String is a palindrome

Enter a string: program

String is not a palindrome


Example 2

#include <stdio.h>
#include <string.h>
void main()
{
char s1[1000],s2[1000];
printf("Enter the string: ");
scanf("%s", s1);
strcpy(s2,s1);
strrev(s2);
if(!strcmp(s1,s2))
{
printf("string is palindrome");
}
else
{
printf("string is not palindrome");
}
}
Output:
Enter a string: madam
String is a palindrome

Enter a string: program

String is not a palindrome


C programs:

C Program to print Matrix Subtraction


#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2];
int i,j,k;
clrscr();
printf("Enter Matrix A");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf("\nThe result");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}

Output:
Enter Matrix A
12
34
Enter Matrix B
11
11
The result
01
23c
C Program to Print Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2]={0};
int i,j,k;
clrscr();
printf("Enter Matrix A");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nThe result");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}
Output:
Enter Matrix A
12
34
Enter Matrix B
12
34
The result
7 10
15 22

C Program for Scaling a Matrix

#include <stdio.h>
#include<conio.h>
int main()
{
int A[3][3]={1,2,3,4,5,6,7,8,9};
int i,j,num;
clrscr();

printf("The elements are");


for( i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d\t", A[i][j]);
}
printf("\n");
}

printf("Enter any number to multiply with matrix A: ");


scanf("%d", &num);

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


{
for(j=0; j<3; j++)
{
A[i][j] = num * A[i][j];
}
}

printf("\nResultant matrix c.A = \n");


for(i=0; i<3; i++)
{
for(j=0; j<3;j++)
{
printf("%d ", A[i][j]);
}
printf("\n");
}
getch();
return 0;
}
The elements are
123
456
789
Enter any number to multiply with matrix A:5
5 10 15
20 25 30
35 40 45

Program to calculate determinant of 2x2 matrix


Determinant
The Determinant of a matrix is a special number that can be calculated from the elements
of a square matrix. The determinant of a matrix A is denoted by det (A), det A or |A|.
Determinant of 2x2 matrix

Determinant of 3x3 matrix

C Program to calculate determinant of 2x2 matrix

#include <stdio.h>
int main()
{
int A[2][2];
int i,j;
long det;
printf("Enter elements in matrix of size 2x2: \n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &A[i][j]);
}
}
det = (A[0][0] * A[1][1]) - (A[0][1] * A[1][0]);
printf("Determinant of matrix A = %ld", det);
return 0;
}

Enter elements in matrix of size 2x2:


12
34
Determinant of matrix A = -2

C Program to calculate determinant of 3x3 matrix

#include<stdio.h>
int main()
{
int i,j, a[3][3];
int x, y, z, Determinant = 0;

printf("\n Please Enter the 3 * 3 Matrix Elements \n");


for(i = 0; i < 3; i++)
{
for(j = 0;j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}

x = (a[1][1] * a[2][2]) - (a[2][1] * a[1][2]);


y = (a[1][0] * a[2][2]) - (a[2][0] * a[1][2]);
z = (a[1][0] * a[2][1]) - (a[2][0] * a[1][1]);

Determinant = (a[0][0] * x) - (a[0][1] * y) + (a[0][2] * z);

printf("\n The Determinant of 3 * 3 Matrix = %d", Determinant);


return 0;
}

Output:

Please Enter the 3 * 3 Matrix Elements


4 2 1
7 2 8
9 2 1

The Determinant of 3 * 3 Matrix =70

Program to search a given number in a list.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,search,a[10];
printf("Enter the values");
for(i=0;i<6;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value to be search");
scanf("%d",&search);
for(i=0;i<5;i++)
{
if(a[i]==search)
{
printf("The value is present");
break;
}
}
if(i==5)
printf("not present");
getch();
}
Output:

Enter the values


1
2
3
4
5
6
Enter the value to be search 3
The value is present
1.Program to read the numbers and print the given numbers in reverse order
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
printf("Enter the elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("The elements are");
for(i=4;i>=0;i--)
{
printf("%d\t",a[i]);
}
getch();
}
Output:

Output:

Enter the elements:


1 2 3 4 5
The elements are
5 4 3 2 1

4.Sorting the given array elements in ascending order and printing smallest and largest
element in the array

#include<stdio.h>
#include<conio.h>
void main()
{
int s,i,j,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
{
scanf("%d",&a[i]);
}
for(i = 0; i < s; i++)
{
for( j=i+1 ;j < s ;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
{
printf(" %d",a[i]);
}
printf(“Largest element in the array : %d ”,a[s -1]);
printf(“Smallest element in the array :%d ”, a[0]);
getch();
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 2 7
The array after sorting is: 0 2 4 5 7
Largest element in the array : 7
Smallest element in the array : 0
4.Sorting the given array elements in ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
int s,i,j,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
{
printf(" %d",a[i]);
}
getch();
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is: 0 4 5 7 21

You might also like