Wednesday, October 5, 2022

Sorting in C Language (Bubble Sort)

 Sorting in C Language

Sorting is a process in which we rearrange the elements in any particular order, which can be set ready for further processing by the program logic. In C programming language, there are multiple sorting algorithms available, which can be incorporated inside the code. The various types of sorting methods possible in the C language are Bubble sort, Selection sort, Insertion sort, Quick sort, Merge sort and Heap sort.

Bubble Sort

It is the simplest sorting method in which program algorithm swap the adjacent elements if they are in the wrong order. This method is not suitable for large data sets as its average and worst-case time complexity is quite high. There are n-1 passes and cycle required. For Example we can see the diagram – (sorting in ascending order) 

Source Code :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int arr[100], n, i, j, t;
//Taking input of arr size
printf("Enter number of elements :");
scanf("%d",&n);
//taking input of array elements
printf("Enter %d elements of array\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//sorting (bubble)
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1]) /* For decreasing order use '<' instead of '>' */
{
//swaping
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}}}
//printing sorted element
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
getch();
}



Sunday, September 25, 2022

Binary to Decimal Conversion in C Language

 Binary to Decimal Conversion in C Language

In this section we discuss about Binary to Decimal conversion. A Binary number should we converted as following method :

Suppose we take a binary (1101)2

then we need to find place value of each digits and add them, the addition of place value  is equal to the decimal equivalent of binary.

finding place value :

1

1

0

1

1 X 23=8

1 X 22=4

0 X 21=0

1 X 20=1

addition = 8 + 4 + 0 + 1 = 13

So (13)10­ is the decimal equivalent of binary (1101)2

Method to convert binary to decimal

Ø Take a binary number as the input in variable bin.

Ø Divide the number by 10 and store the remainder into variable "r".

Ø dec = dec + r ´ b;
Initially, the "dec" is 0, and the "b" is 1, where the "r" variable stores the remainder of the number.

Ø Divide the quotient of the original number by 10.

Ø Multiply the "b" by 2.

Ø Print the decimal of the binary number.

Note : variable dec for decimal number, b for base, r for reminder and "bin" for binary.

Source Code : 

#include <stdio.h>

#include <conio.h>

void main()

{

clrscr();

int n, bin, dec=0, b=1, r;

printf ("Enter a binary number \n");

scanf ("%d", &n);

bin = n;

while ( n > 0)

            {

            r = n % 10;

            dec = dec + r * b;

            n = n / 10;

            b = b * 2;

            }

printf("The Decimal equivalent of %d = %d", bin, dec);

getch( );

}

YouTuve Video :



Saturday, September 24, 2022

C Program To Convert Decimal Number To Binary Number without using array

Method to calculate Decimal to Binary Number

Take a decimal input from the user

for example :

if user enter a decimal 12

We keep on dividing the number 12 by 2.

 

12 / 2 = 6, reminder 0

06 / 2 = 3, reminder 0

03 / 2 = 1, reminder 1.

01/ 2 = 1, reminder 1.

Decimal to binary

So Binary equivalent of 12 is 1100.



Write all reminders bottom to top. It’s binary equivalent of decimal (12)10=(1100)02

Source Code :

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int t, n, r=0, p=1, bin=0;

printf("Enter a number\n");

scanf("%d",&n);

t=n;

while(n>0)

{

r=n%2;

n=n/2;

bin=bin+(r*p);

p=p*10;

}

printf("Binary of decimal %d = %d",t,bin);

getch();

}

YouTube Video :



 

Thursday, September 22, 2022

C Program for whether inputed number is an armstrong number or not by using while loop

 Armstrong Number

It is a magical number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

153 = (1x1x1)+(5x5x5)+(3x3x3)  

where:  

(1x1x1)=1  

(5x5x5)=125  

(3x3x3)=27  

So:  

1+125+27=153

C Program

//C Program to input a number and

//check whether armstrong number or not

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int n, temp=0, r, sum=0;

printf("Enter a number\n");

scanf("%d",&n);

temp=n;

while(n>0)

{

r = n % 10;

sum = sum + (r*r*r);

n = n / 10;

}

if(sum == temp)

printf("%d is an armstrong number",temp);

else

printf("%d is not an armstrong number",temp);

getch();

}

YouTube Video




Sunday, September 18, 2022

Factorial in C Language with youtube video

Factorial in C

The Factorial is the product of all positive descending integers of n. Factorial of n is denoted by n!.

For example:

4! = 4 X 3 x 2 X 1 = 24

5! = 5 X 4 X 3 x 2 X 1 = 120

Example Program for factorial by using "for loop"

#include<conio.h>

#include<stdio.h>

void main()

{

clrscr();

int n, i;

long int f=1;

printf("Enter a number\n");

scanf("%d",&n);

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

                {

                f=f*i;

                }

printf("Factorial of %d = %ld",n,f);

getch();

}

Example Program for factorial by using "Function"

#include<conio.h>

#include<stdio.h>

long int fac(int);

void main()

{

clrscr();

int n;

long int f;

printf("Enter a number\n");

scanf("%d",&n);

f=fac(n);

printf("Factorial of %d = %ld",n,f);

getch();

}

long int fac(int x)

{

long int f=1;

int i;

if(x==1)

                return(1);

else

                for(i=1;i<=x;i++)

                                {

                                f=f*i;

                                }

return(f);

}

Example Program for factorial by using "Recursion"

//C Program to find factorial

//of an inputted number by function

#include<conio.h>

#include<stdio.h>

long int fac(int);

void main()

{

clrscr();

int n;

long int f;

printf("Enter a number\n");

scanf("%d",&n);

f=fac(n);

printf("Factorial of %d = %ld",n,f);

getch();

}

long int fac(int x)

{

if(x==1)

                return(1);

else

                return x * fac(x-1);

}

youtube video



Monday, September 12, 2022

C Program for Prime Number

C Program For Prime Number

 //C Program for Prime Number

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int n, m, i, c=0;

printf("Enter a Number\n");

scanf("%d",&n);

m=n/2;

for(i=2;i<=m;i++)

{

if(n%i==0)

{

printf("%d is not a prime number",n);

c++;

break;

}

}

if(c==0)

{

printf("%d is a Primne number",n);

}

getch();

}

YouTube Video

(Please Subscribe our Channel)


C Program to check whether inputed number is a Prime Number or not by using for loop without using mid value...

//C Program to check wether inputed number //is a prime or not #include<stdio.h> #include<conio.h> void main() { clrscr(); int n, c=0, i; printf("Enter a number\n"); scanf("%d",&n); for(i=1;i<=n;i++) { if(n%i==0) c++; } if(c==2) printf("%d is a prime number",n); else printf("%d is not a prime number",n); getch(); }

YouTube Video

(Please Subscribe our Channel)



Saturday, September 10, 2022

Looping statement in C Language

 Looping Statement in C

Loop is used to execute the block of code several times according to the given condition in loop. It means it executes the same code multiple times.

There are 3 types of loop

01.        while loop

02.        do – while loop

03.        for loop

1. while Loop

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given condition. The while loop is mostly used in the case where the number of iterations is not known in advance.

Syntax of while loop :

while(condition)

{

statements – 1

statements – 2

.

.

statements – n

//increment / decrement operator if required (optional)

}

Flowchart of while loop :

e.g.

//Program for printing 1 to 10 number

//by using while loop

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

while(i<=10)

{

            printf("%d ",i);

            i++;

}

getch();

}

2. do – while loop

It also executes the code until condition is false. In this at least once, code is executed whether condition is true or false but while loop is executed only when the condition is true.

Syntax :

do

{

statements – 1

statements – 2

.

.

statements – n

}

while(condition);

Flowchart of do-while loop :

e.g.

//Program for printing 1 to 10 number

//by using do-while loop

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

do

{

            printf("%d ",i);

            i++;

}

while(i<=10);

getch();

}

3. for Loop

It also executes the code until condition is false. In this three parameters are given to it these are Initialization, Condition & Increment/Decrement operators

Syntax

for(initialization; condition; increment/decrement)

{

statements – 1

statements – 2

.

.

statements – n

}

Flowchart of for loop :



e.g.

//Program for printing 1 to 10 number

//by using for loop

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

for(i=1; i<=10; i++)

{

            printf("%d ",i);

}

getch();

}

Youtube video for program



Switch-Case Statement in C

 Switch-Case Statement in C

Switch-Case statement in C allows us to execute multiple operations for the different possible values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

Syntax :

switch (expression)

{   

case value1:   

            {

Statements of case 1

break;

}

case value2:

{

Statements of case 2

break;

}

case value3:

            {

Statements of case 3

break;

}

.....   

default: 

            {  

 code to be executed if all cases are not matched;

}

}   //End of switch case statement

Rules for switch statement in C language

Ø  The case value must be an integer or character constant.

Ø  The case value can be used only inside the switch statement.

Ø  The switch expression must be of an integer or character type.

Flowchart of switch statement in C

Example Program :

//Example program of swith case statement

//satyamsrig.blogspot.in

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int ch;

printf("Enter the number of month\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

{

printf("January");

break;

}

case 2:

{

printf("February");

break;

}

case 3:

{

printf("March");

break;

}

case 4:

{

printf("April");

break;

}

case 5:

{

printf("May");

break;

}

case 6:

{

printf("June");

break;

}

case 7:

{

printf("July");

break;

}

case 8:

{

printf("August");

break;

}

case 9:

{

printf("September");

break;

}

case 10:

{

printf("October");

break;

}

case 11:

{

printf("November");

break;

}

case 12:

{

printf("December");

break;

}

default :

{

printf("You have entered wrong keyword");

}

}

getch();

}

Example Program Youtube Video :