C Programming Basics - Learn C Programming & C programs


C Programming 


1-Example: Program to Check Prime Number
2-Factors of a Positive Integer

3-Reverse an Integer

 4-Sum of Natural Numbers Using for Loop

5-Sum of Natural Numbers Using while Loop

6-Program to Check Odd or Even Using Conditional Operator

7-Program to Swap Numbers Using Temporary Variable

8-Program to Multiply Two Numbers

9-Find Largest Element Using Dynamic Memory Allocation - calloc()

10-Calculate Length of String without Using strlen() Function







1-Example: Program to Check Prime Number


#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i<=n/2; ++i)
    {
        // condition for nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
 
    return 0;
}

2-Factors of a Positive Integer

 #include <stdio.h>
int main()
{
    int number, i;

    printf("Enter a positive integer: ");
    scanf("%d",&number);

    printf("Factors of %d are: ", number);
    for(i=1; i <= number; ++i)
    {
        if (number%i == 0)
        {
            printf("%d ",i);
        }
    }

    return 0;
}

3-Reverse an Integer

#include <stdio.h>
int main()
{
    int n, reversedNumber = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &n);

    while(n != 0)
    {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }

    printf("Reversed Number = %d", reversedNumber);

    return 0;}


4-Sum of Natural Numbers Using for Loop


#include <stdio.h>

int main()

{

    int n, i, sum = 0;

    printf("Enter a positive integer: ");

    scanf("%d",&n);

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

    {

        sum += i;   // sum = sum+i;

    }
    printf("Sum = %d",sum);
    return 0;

}



5-Sum of Natural Numbers Using while Loop


#include <stdio.h>

int main()

{

    int n, i, sum = 0;

    printf("Enter a positive integer: ");

    scanf("%d",&n);

    i = 1;

    while ( i <=n )

    {

        sum += i;

        ++i;

    }
    printf("Sum = %d",sum);
    return 0;

}





6-Program to Check Odd or Even Using Conditional Operator


#include <stdio.h>
int main()
{
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    (number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number);

    return 0;
}


 7-Program to Swap Numbers Using Temporary Variable

 #include <stdio.h>
int main()
{
      double firstNumber, secondNumber, temporaryVariable;

      printf("Enter first number: ");
      scanf("%lf", &firstNumber);

      printf("Enter second number: ");
      scanf("%lf",&secondNumber);

      // Value of firstNumber is assigned to temporaryVariable
      temporaryVariable = firstNumber;

      // Value of secondNumber is assigned to firstNumber
      firstNumber = secondNumber;

      // Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
      secondNumber = temporaryVariable;

      printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
      printf("After swapping, secondNumber = %.2lf", secondNumber);

      return 0;

}


8-Program to Multiply Two Numbers

 #include <stdio.h>
int main()
{
    double firstNumber, secondNumber, product;
    printf("Enter two numbers: ");

    // Stores two floating point numbers in variable firstNumber and secondNumber respectively
    scanf("%lf %lf", &firstNumber, &secondNumber);

    // Performs multiplication and stores the result in variable productOfTwoNumbers
    product = firstNumber * secondNumber;

    // Result up to 2 decimal point is displayed using %.2lf
    printf("Product = %.2lf", product);
 
    return 0;
}


9-Find Largest Element Using Dynamic Memory Allocation - calloc()

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

int main()
{
    int i, num;
    float *data;

    printf("Enter total number of elements(1 to 100): ");
    scanf("%d", &num);

    // Allocates the memory for 'num' elements.
    data = (float*) calloc(num, sizeof(float));

    if(data == NULL)
    {
        printf("Error!!! memory not allocated.");
        exit(0);
    }

    printf("\n");

    // Stores the number entered by the user.
    for(i = 0; i < num; ++i)
    {
       printf("Enter Number %d: ", i + 1);
       scanf("%f", data + i);
    }

    // Loop to store largest number at address data
    for(i = 1; i < num; ++i)
    {
       // Change < to > if you want to find the smallest number
       if(*data < *(data + i))
           *data = *(data + i);
    }

    printf("Largest element = %.2f", *data);

    return 0;
}


10-Calculate Length of String without Using strlen() Function

 #include <stdio.h>
int main()
{
    char s[1000], i;

    printf("Enter a string: ");
    scanf("%s", s);

    for(i = 0; s[i] != '\0'; ++i);

    printf("Length of string: %d", i);
    return 0;
}


Online Video Tutorial In Hindi Free





Download Software For C programming






Powered by Blogger.