Showing posts with label Loop. Show all posts
Showing posts with label Loop. Show all posts

C Program to Check Prime Use Loop And Recursive

How to write a C Program to Check Prime Use Loop And Recursive in C Programming Language ?


Solution For C Program :
/*Check Prime Use Loop And Recursive*/

C Program to Calculate Grid Size, Initialized Number onto tile, Initialize Tile Loop and If Grid is Even, Swap The Tiles Numbered 1 and 2

How to write a C Program to Calculate Grid Size, Initialized Number onto tile, Initialize Tile Loop and If Grid is Even, Swap The Tiles Numbered 1 and 2 in C Programming Language ?

Solution For C Program:
/*C Program to Calculate Grid Size, Initialized Number onto tile, Initialize Tile Loop and If Grid is Even, Swap The Tiles Numbered 1 and 2*/

C Program Fraction To Decimal

How to write a C Program Algorithm:
  1. Divide numerator by denominator until remainder = 0
  2. or it makes a loop (a remainder appears twice).
  3. Use array to mark position of a remainder.

One Loop And Two Loops Handles all cases in C Program

One Loop And Two Loops Handles all cases in C Programming Language ?


Solution:

  1. /** option 1: one loop handles all cases */
  2.  
  3. switch = read_switch();
  4.  
  5. for (light = 0; i < 8; light++) {
  6.     if (switch == 0 || light <= switch) {
  7.         light_on(light);
  8.         delay();
  9.         light_off(light);
  10.     }
  11. }
  12.  
  13. /** option 2: two loops */
  14.  
  15. switch = read_switch();
  16.  
  17. if (switch != 0) {
  18.     for (light = 0; light < 8; light++) {
  19.         light_on(light);
  20.         delay();
  21.         light_off(light);
  22.     }
  23. } else {
  24.     for (light = 0; light <= switch; light++) {
  25.         light_on(light);
  26.         delay();
  27.         light_off(light);
  28.     }
  29. }

C Program to Implemention Bubble Sort using array

How to write a C Program to Implement Bubble Sort in C Program Language ?

/* Bubble sort : In bubble sort, we compare first two elements of array; then move one block ahead and compare the elements again until the last element; at the end we have the highest numeric value to the end; This process is iterated n times ( n -> no. of elements in array) and finally the sorted output is attained... We have used (6-i) in the "second for loop" so as to improve the performance as the last element(n) is not required to be computed as the iteration proceeds n times....*/


// bubble sort

#include <stdio.h>
#include <stdlib.h>

int main()
{
int arr[6],i,j,temp,n;

printf("Enter the number of elements in the array\n");
scanf("%d",&n);

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

printf("Enter the number of elements in the array\n");
scanf("%d",&arr[i]);
}


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

for(j=1; j<=n-i; j++)
{

if(arr[j-1] > arr[j])
{
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}

else if(arr[j-1] < arr[j])
{
break;
}
}

}

for(i=0; i<n; i++)
{
printf("The sorted order is: \n");
printf("%d\n",arr[i]);
}
}