Pages

Friday, July 31, 2020

GETS AND PUTS FUNCTION IN C

GETS FUNCTION
The gets() function is used to read only the string values in C programming language.

SYNTAX OF GETS() FUNCTION
gets(string variable name);

DIFFERENCE BETWEEN SCANF() AND GETS()
The scanf() function is used to read different datatypes inputs (character, integer, float, string) but it cannot read the blank spaces. 
The gets() function is used to read only the string values and it can also read the blank space between the string values. 

GETS() FUNCTION EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[40];
printf("Enter your name: ");
gets(name);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter your name:
Rakesh kumar


PUTS FUNCTION
The puts() function is used to display/print only the string values in C programming language.

SYNTAX OF GETS() FUNCTION
puts(string variable name);

DIFFERENCE BETWEEN PRINTF() AND PUTS()
The printf() function is used to display/print the different datatype variable's values (character, integer, float, string)
The puts() function is used to display/print only the string values.

We cannot combine the text message + string variable's value in the puts() function, to combine the text message + variable's value we need to use the printf() function.  

PUTS() FUNCTION EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[40];
printf("Enter your name: ");
gets(name);
puts(name);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter your name:
Rakesh kumar
Rakesh kumar

STRLEN IN C >>

STRING ARRAY IN C



String values are consisted with number of characters and terminated by a special character NULL (\0).

C language does not support string as data type, actually string is one dimensional array of character data type in C language.

STRING ARRAY EXAMPLE - I
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[] ={'R', 'a', 'k', 'e', 's', 'h'};
printf("%s", name);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Rakesh




STRING ARRAY EXAMPLE - II
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[40];
printf("Enter your name: ");
scanf("%s", &name);
printf("Welcome ...%s", name);
getch();
}



----------------------------

         OUTPUT 
----------------------------

Enter your name: Rakesh
Welcome ... Rakesh

2D ARRAY IN C



2D Array consisted with number of rows and columns, Therefore it store data in matrix form.

2D ARRAY SYNTAX
int a[2][3];

Explanation: In the above example 'a' is name of 2D Array, [2] is a number of rows and [3] is number of columns.

2D ARRAYP ROGRAM IN C
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][3], i, j;
printf("Enter values in 2D array\n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &a[i][j]);
}}

// Loop to print 2D array in matrix form
for(i=0; i<2; i++)
{
printf("\n\n\n\n");
for(j=0; j<3; j++)
{
printf("\t%d", a[i][j]);
}}
getch();
}


----------------------------

         OUTPUT 
----------------------------

Enter values in 2D array
2
4
3
7
5
6

2D values in matrix form
2  4  3
7  5  6


PROGRAM TO CALCULATE SUM OF ROWS AND COLUMNS VALUES IN 2D ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3], i, j, sum=0;
printf("Enter values in 2D array\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &a[i][j]);
}}

// Loop to print 2D array in matrix form and calculate sum
for(i=0; i<2; i++)
{
printf("\n\n\n\n");
for(j=0; j<3; j++)
{
printf("\t%d", a[i][j]);
sum=sum+a[i][j];
}
}
printf("\nSum of 2D array is: %d",sum);
getch();

}


----------------------------


         OUTPUT 

----------------------------

Enter values in 2D array
2
4
3
7
5
6
8
9
1

2D values in matrix form
2  4  3 
7  5  6
8  9  1

Sum of 2D array is: 45 

ARRAY SORTING IN C

C PROGRAM TO SORT ARRAY IN ASCENDING 

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

printf("Enter array's size: ");
scanf("%d",&size);

printf("Enter array's values:\n");
for(i=0; i<size; i++)
{
scanf("%d", &a[i]);
}

// Loop to sort array in ascending order
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}}

printf("\nArray values after sorting\n")
for(i=0; i<size; i++)
{
printf("%d", a[i]);
}
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter array size: 5

Enter values in array:
6
7
3
4
8

Array values after sorting
3
4
6
7
8

STRING ARRAY IN C >>

SEARCHING IN ARRAY

C PROGRAM TO SEARCHING IN ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10];
int i, num;

printf("Enter values in array:\n");
for(i=0; i<10; i++)
{
scanf("%d", a[i]);
}

printf("Enter value to search :");

scanf("%d",&num);

// Loop to search entered value's position in array
for(i=0; i<10; i++)
{
if(num == a[i])
{
break;
}}

printf("Value found at position: %d", i+1);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter values in array:
16
17
13
14
18
11
12
15
10

Enter value to search: 13
Value found at position: 3

ARRAY SORTING IN C >>

DELETION IN ARRAY

C PROGRAM TO DELETE ELEMENT IN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[60];
int size, n, i, j;
printf("Enter array size:");
scanf("%d",&size);

printf("Enter values in array:\n");
for(i=0; i<size; i++)
{
scanf("%d", a[i]);
}

printf("Enter value to delete :");
scanf("%d",&n);

// Loop to find value in array
for(i=0; i<size; i++)
{
if(n == a[i])
{
break;
}}

//Loop to delete value from array

for(j=i; i<size; j++)

{

a[j] = a[j+1];
}

size = size - 1;

// Print array's values after deletion
printf("\nArray's values after deletion - - -\n")
for(i=0; i<size; i++)
{
printf("%d\n", a[i]);
}

getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter array size: 5

Enter values in array:
6
7
3
4
8

Enter value to delete: 3
Array's values after deletion - - -
6
7
4
8

SEARCHING IN ARRAY >>

INSERTION IN ARRAY

C PROGRAM TO INSERT NEW ELEMENT IN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[60];
int size, n, i;
printf("Enter array size:");
scanf("%d",&size);

printf("Enter values in array:\n");
for(i=0; i<size; i++)
{
scanf("%d", a[i]);
}

printf("Enter new value for array:");
scanf("%d",&n);

// Insert new value in array
size ++;
a[size]=n;

// Print array's values along with new value
printf("\nArray's values after insertion\n")
for(i=0; i<size; i++)
{
printf("%d\n", a[i]);
}

getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter array size: 5

Enter values in array:
6
7
3
4
8

Enter new value for array: 12
Array's values after insertion
6
7
3
4
8
12

DELETION IN ARRAY >>

ARRAY INTRODUCTION IN C


Array is a collection those values which are related with same data type.
Suppose we want to insert marks for class XII students and total 50 students are there.
Every student require marks for five subjects. 
So we need to declare (50 x 5 = 250) variables that need several lines code but by using of array we can do this task by just one variable.
int m[250]

ARRAY EXAMPLE IN C
int a[40]

Explanation: in the above statement “int” is a predefined data type in C language and it is used to create a variable in the C language.
“a” is name of array and [40] is size of the array.


FEATURES OF ARRAY
1. Array is an example of static data type and static memory should be allocate before the run time, therefore it causes of overflow and underflow errors.

2. In array all elements store in one variable by help of unique index number and first value of array always store in zero (0) index number.


3. All the elements of array should be related with their one data type.



HOW TO DECLARE ARRAY IN C LANGUAGE
int m=[5];

Explanation: In the above example, we are declaring an integer array with the help of 'int' data type and as we know, that array stores its value by help of unique index number.
m[0] = 4
m[1] = 7
m[2] = 5
m[3] = 9
m[4] = 8

GOTO STATEMENT IN C

Goto statement is used to transfer the program control from one statement to another statement by help of label.

GOTO STATEMENT EXAMPMLE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int age;
char c;
abc:                 //  Define a label
printf(“enter your age:”);
scanf(“%d”,&age);

printf(“do you want to continue (y/n):”);
scanf(“%c”,&c);
if(c==’y’)
{
goto abc;
}
getch();
}


PROGRAM EXPLANATION
The above program first ask to enter your age and then ask to continue.
If you press 'y' then it will throw the program's execution control to the 'abc' label and statements below 'abc' control will execute again.