Home » programming » c programming

# Category Archives: c programming

## Shorting of elements in c programing using bubble short.

firstly , we need to know about bubble short . what is happening during bubble shorting.

>>> In Bubble short elements of one position is compare to the next position and arrange the elements whatever we want.

Here is the source code example for bubble short.

```
void main()
{
int array[10];
int i, j, num, temp;
printf("Enter number of elements \n");
scanf("%d", &num);
printf("Enter the elements\n");
for(i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("required shorted array is\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
```

Output:

Explanation:

1.firstly , program ask user to input number of elements and ask for enter elements.

2.After getting the elements program start to compare all the elements inside the for loop.

using: if (array[j] > array[j + 1])

{

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

}

} this block.

3.And lastly print the required shorting elements.

## C program to check the give number is prime or not?

Before know the source code we need to understand the concept about primat number .

what is prime number?

>>The number which is only divisible by 1 or itself.

or

>>The number which do not come in others numbers multiplication table .

To check the given number is prime ,firstly user must input the number .After getting number from the user ,we need to divide that number by each 1 to n (input number) by using for loop .then we need to count how many times input number is perfectly divided.

If count is equal to 2 ,that means the input number is divided by 1 or itself,that means input number is a prime number.

The source code is given below and output also:

main()

{

int a,i,d;

printf(“enter a number to check prime\n “);

scanf(“%d”,&a);

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

{

if(a%i==0)

{

d++;

}

}

if(d==2)

{

printf(“prime number”);

}

else

{

printf(“not a prime number”);

}

}

output:

enter a number to check prime 7 prime number

## C program to check the number is even or odd.

There are many ways to check the given number is even or odd in c program .the simple way to check is by if condition and switch statement.

Here we will check by both the process.

process 1 by using simple if statement:

main()

{

int a,b;

printf(“enter a number\n”);

scanf(“%d”,&a);

if(a%2==0)

{

printf(“even number”);

}

else

{

printf(“odd number”);

}

}

process 2 by using switch statement:

main()

{

int a,b;

printf(“enter a number\n”);

scanf(“%d”,&a);

b=a%2;

switch(b)

{

case 0:printf(“even number”);

break;

case 1:printf(“odd number”);

break;

}

}

Output:

Explanation:

1)first program will take input from the user .

2) program calculate the given expression.and check the condition.

3)if the condition is true or match found .the program show the result even no,

if the condition is false or no match found .the program show the result odd no .

## c program to find sum of the digits

To find sum of the digits firstly,we have to separate all the digits of the number and the converted that digit into number.

In ordered to separate the digits of a number ,we use ( number%10 ) expression and converted these digit into number we use (num=s*num+num%10) expression.but the value on num have to update in each step for that updation we use (num=num/10) expression.

So,The source code is given below

main()

{

int a,b,c,sum=0;

printf(“enter an number:”);

scanf(“%d”,&a);

while(a>0)

{

b=a%10;

sum=sum+b;

a=a/10;

}

printf(“sum of the digits=%d”,sum);

}

Output:

## C program to check string palindrome.

Firstly let us understand what is palindrome?

>>The order of number or string after reverse is in same order ,then we can say the number or string is palindrome.

let understand by a simple example, x=mom after reversing these stringr we get mom,then we can say the given string is palindrome

To check the given string is palindrome ,The code is given below

main()

{

char a[100],x[100];

printf(“enter an string to cheak palindrome\n”);

gets(a);

strcpy(x,a);

strrev(a);

printf(“\nreverse string=%s”,a);

if(strcmp(x,a)==0)

{

printf(“\n\npalindrome”);

}

else

{

printf(“\n\nnot palindrome”);

}

}

Output:

Explanation:

1) program ask user to input the string.

2) Then,The program copy the given string to the another variable x by the function

strcpy(x,a); for further use.

3) Again ,program will reverse the given string by the function

strrev(a);

4)The program check the reverse string and copy string.if these two are equal then it will print the result it is palindrome.else it will print it is not palindrome.

## c program to calculate factorial of given number.

**firstly ,let us understand the concept of factorial number.**

The product (multiplication) of all positive integers less than or equal to n is called factorial number.and it is denoted by n!.

eg. factorial of 5=5*4*3*

2*1=120

To find factorial number .The code is given below.

main()

{

int a,i,fac;

fac=1;

printf(“\t\twrite a no. to calulate factorial\n “);

scanf(“%d”,&a);

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

{

fac=fac*i;

}

printf(“factorial of %d=%d”,a,fac)

}

Output:

Explanation:

1)First program ask you to input the number.

2)Then program will calculate fac=fac*i,inside the for loop and here i increment in each time by 1 until the given condition get false.

3)lastly print the result (fac).

## C programm to calculate fabonacci series

**what is fabonacci series?**

The series of number obtained by adding previous two preceding number is called fabonicci series.

To calculate these series of number .The code is given below.

main()

{

int a=0,b=1,c;

while(b<20)

{

printf(“%d\t”,b);

c=a+b;

a=b;

b=c;

}

}

Output

Explanation:

1.first,The initial value of a=0,b=1 is already given when declaring the var.

2.Then program will calculate c inside while loop then convert b=c.and it will print b.

3.Again increment of b takes place ,and calculate same thing and print b till the condition get false.