Showing posts with label C PROGRAMMING. Show all posts
Showing posts with label C PROGRAMMING. Show all posts

Sunday, 7 September 2014

Multiply two positive numbers without using * operator

# Multiply two positive numbers without using   *  operator /


CODE

#include<stdio.h>
#include<conio.h>
int main(){
    int a,b,i;
    int result=0;
    printf("Enter two number to be multiply : ");
    scanf("%d%d",&a,&b);
    for(i=1;i<=b;i++)
        result=result+a;
    printf("%d*%d=%d\n",a,b,result);
}


RESULT

 Enter two number to be multiply : 54

5*4=20

 


Pascal triangle Program

#Write a program in c for the Pascal Triangle .

CODE

#include<stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");
for( c = 0 ; c <= i ; c++ )
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}


OUTPUT



Monday, 25 August 2014

PROGRAM TO CALCULATE THE SUM ,AVERAGE,PERCENTAGE OF 5 SUBJECT IN C

#include<stdio.h>
#include<conio.h>

int main()
{
   int s1, s2, s3, s4, s5, sum, total = 500;
   float per,avg;

   printf("\nEnter marks of 5 subjects : ");
   scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);

   sum = s1 + s2 + s3 + s4 + s5;
   printf("\nSum : %d", sum);

   per = (sum * 100) / total;
   printf("\nPercentage : %f", per);

   avg=sum/5;
   printf(""\n Average:%f, avg);
   return (0);
}

Wednesday, 20 August 2014

Functionality Difference Between for loop & while loop

The “for loop” loops from one number to another number and increases by a specified value each time.
It uses the following Structure:-

for(start value; continue or end condition; increase value statement)


let we will take an example:-






We first start by setting the variable i to 0. This is  where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++).It return zero 


The while loop can be used if you don’t know how many times a loop must run.

let we will take an example:-




First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.




Tuesday, 19 August 2014

What are the Difference Between while & for Loop?

Difference No 1

for Loop

In for loop, initialization, condition and adjustment statements are all put together in one line which make loop easier to understand and implement. 

while Loop

In the while loop, initialization is done prior to the beginning of the loop. Conditional statement is always put at the start of the loop. While adjustment can be either combined with condition or embedded into the body of the loop. 

let we see with example:-




Difference No 2

for Loop

When using “continue;” statement in for loop, control transfers to adjustment statement 

while Loop

 In while loop control transfers to the condition statement.


let we see with example:-




Saturday, 26 July 2014

Quick Sort Alogorithm With C Programming

ALGORITHM FOR QUICK SORT


1.) Make a list of items that need to be sorted, lets apply in an array.

2.) Choose any element as pivot element from the array list.

3.) Rearrange the array list so that all the elements with value less than the pivot will come before the pivot and the element with value greater will come after the pivot with in the same array, which make pivot element in the sorted position.

4.) Apply recursively the 3rd step to the sub array of the element with smaller values and separate the sub array of the elements with the greater values.


C PROGRAM FOR QUICK SORT

#include<stdio.h>
#include<conio.h>

//quick Sort function to Sort Integer array list
void quicksort(int array[], int firstIndex, int lastIndex)
{
    //declaaring index variables
    int pivotIndex, temp, index1, index2;

    if(firstIndex < lastIndex)
    {
        //assigninh first element index as pivot element
        pivotIndex = firstIndex;
        index1 = firstIndex;
        index2 = lastIndex;

        //Sorting in Ascending order with quick sort
        while(index1 < index2)
        {
            while(array[index1] <= array[pivotIndex] && index1 < lastIndex)
            {
                index1++;
            }
            while(array[index2]>array[pivotIndex])
            {
                index2--;
            }

            if(index1<index2)
            {
                //Swapping opertation
                temp = array[index1];
                array[index1] = array[index2];
                array[index2] = temp;
            }
        }

        //At the end of first iteration, swap pivot element with index2 element
        temp = array[pivotIndex];
        array[pivotIndex] = array[index2];
        array[index2] = temp;

        //Recursive call for quick sort, with partiontioning
        quicksort(array, firstIndex, index2-1);
        quicksort(array, index2+1, lastIndex);
    }
}

int main()
{
    //Declaring variables
    int array[100],n,i;

    //Number of elements in array form user input
    printf("Enter the number of element you want to Sort : ");
    scanf("%d",&n);

    //code to ask to enter elements from user equal to n
    printf("Enter Elements in the list : ");
    for(i = 0; i < n; i++)
    {
        scanf("%d",&array[i]);
    }

    //calling quickSort function defined above
    quicksort(array,0,n-1);

    //print sorted array
    printf("Sorted elements: ");
    for(i=0;i<n;i++)
    printf(" %d",array[i]);

    getch();
    return 0;
}

Tuesday, 22 July 2014

REVERSE PROGRAM IN C


PROGRAM TO FIND THE REVERSE OF THE NUMBER


#include<stdio.h>
int main()
{
    int num,rem,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    while(num)
{
         rem=num%10;
         reverse=reverse*10+rem;
         num=num/10;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Output

Enter any number: 254
Reverse of number: 452

CODE FOR PRIME NUMBER IN C

Check given number is prime number or not using c program


#include<stdio.h>

int main()
{

    int num,i,count=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++)
{
        if(num%i==0)
{
         count++;
            break;
        }
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}

Logic 

We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the numbers then we will print it as prime number.

Swapping Of Two Numbers In C

C program for swapping of two numbers using pointers


#include<stdio.h>

int main()
{

    int x,y;
    int *ptrx,*ptry;
    int *temp;

    printf("Enter any two integers: ");
    scanf("%d%d",&x,&y);

    printf("Before swapping: x = %d, y=%d",x,y);

    ptrx = &x;
    ptry = &y;

     temp = ptrx;
    *ptrx = *ptry;
    *ptry = *temp;

    printf("\nAfter swapping: x = %d, y=%d",x,y);
    return 0;
}



Code for swapping in c


#include<stdio.h>
int main()
{
    int a,b,c;
 
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);
    printf("Before swapping: a = %d, b=%d",a,b);

    c = a;
    a = b;
    b = c;
    printf("\nAfter swapping: a = %d, b=%d",a,b);

    return 0;
}