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

Friday, June 4, 2021

C Program to Swap Two Numbers

 

Swap Numbers Using Temporary Variable

#include<stdio.h>
int main() {
      double first, second, temp;
      printf("Enter first number: ");
      scanf("%lf", &first);
      printf("Enter second number: ");
      scanf("%lf", &second);

      // Value of first is assigned to temp
      temp = first;

      // Value of second is assigned to first
      first = second;

      // Value of temp (initial value of first) is assigned to second
      second = temp;

      // %.2lf displays number up to 2 decimal points
      printf("\nAfter swapping, firstNumber = %.2lf\n", first);
      printf("After swapping, secondNumber = %.2lf", second);
      return 0;
}

Output

Enter first number: 1.20
Enter second number: 2.45

After swapping, firstNumber = 2.45
After swapping, secondNumber = 1.20

In the above program, the temp variable is assigned the value of the first variable.

Then, the value of the first variable is assigned to the second variable.

Finally, the temp (which holds the initial value of first) is assigned to second. This completes the swapping process.


Swap Numbers Without Using Temporary Variables

#include <stdio.h>
int main() {
    double a, b;
    printf("Enter a: ");
    scanf("%lf", &a);
    printf("Enter b: ");
    scanf("%lf", &b);

    // Swapping

    // a = (initial_a - initial_b)
    a = a - b;   
 
    // b = (initial_a - initial_b) + initial_b = initial_a
    b = a + b;

    // a = initial_a - (initial_a - initial_b) = initial_b
    a = b - a;

​​​​    ​// %.2lf displays number up to 2 decimal points
    printf("After swapping, a = %.2lf\n", a);
    printf("After swapping, b = %.2lf", b);
    return 0;
}

Output

Enter a: 10.25
Enter b: -12.5
After swapping, a = -12.50
After swapping, b = 10.25

C Program to Demonstrate the Working of Keyword long

 

Program Using the long keyword

#include <stdio.h>
int main() {
    int a;
    long b;   // equivalent to long int b;
    long long c;  // equivalent to long long int c;
    double e;
    long double f;

    printf("Size of int = %zu bytes \n", sizeof(a));
    printf("Size of long int = %zu bytes\n", sizeof(b));
    printf("Size of long long int = %zu bytes\n", sizeof(c));
    printf("Size of double = %zu bytes\n", sizeof(e));
    printf("Size of long double = %zu bytes\n", sizeof(f));
    
    return 0;
}

Output

Size of int = 4 bytes 
Size of long int = 8 bytes
Size of long long int = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes

In this program, the sizeof operator is used to find the size of intlonglong longdouble and long double variables.

As you can see, the size of long int and long double variables are larger than int and double variables, respectively.

By the way, the sizeof operator returns size_t (unsigned integral type).

The size_t data type is used to represent the size of an object. The format specifier used for size_t is %zu.

Note: The long keyword cannot be used with float and char types.

C Program to Find the Size of int, float, double and char

 The sizeof(variable)operator computes the size of a variable. And, to print the result returned by sizeof, we use either %lu or %zu format specifier.

Program to Find the Size of Variables

#include<stdio.h>
int main() {
    int intType;
    float floatType;
    double doubleType;
    char charType;

    // sizeof evaluates the size of a variable
    printf("Size of int: %zu bytes\n", sizeof(intType));
    printf("Size of float: %zu bytes\n", sizeof(floatType));
    printf("Size of double: %zu bytes\n", sizeof(doubleType));
    printf("Size of char: %zu byte\n", sizeof(charType));
    
    return 0;
}

Output

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

In this program, 4 variables intTypefloatTypedoubleType and charType are declared.

Then, the size of each variable is computed using the sizeof operator.

C Program to Find ASCII Value of a Character

 In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. This integer value is the ASCII code of the character.

For example, the ASCII value of 'A' is 65.

What this means is that, if you assign 'A' to a character variable, 65 is stored in the variable rather than 'A' itself.

Now, let's see how we can print the ASCII value of characters in C programming.


Program to Print ASCII Value

#include <stdio.h>
int main() {  
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);  
    
    // %d displays the integer value of a character
    // %c displays the actual character
    printf("ASCII value of %c = %d", c, c);
    
    return 0;
}

Output

Enter a character: G
ASCII value of G = 71

In this program, the user is asked to enter a character. The character is stored in variable c.

When %d format string is used, 71 (the ASCII value of G) is displayed.

When %c format string is used, 'G' itself is displayed.

C Program to Multiply Two Floating-Point Numbers

 

Program to Multiply Two Numbers

#include <stdio.h>
int main() {
    double a, b, product;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &a, &b);  
 
    // Calculating product
    product = a * b;

    // %.2lf displays number up to 2 decimal point
    printf("Product = %.2lf", product);
    
    return 0;
}

Output

Enter two numbers: 2.4
1.12
Product = 2.69

In this program, the user is asked to enter two numbers which are stored in variables a and b respectively.

printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b); 

Then, the product of a and b is evaluated and the result is stored in product.

product = a * b;

Finally, product is displayed on the screen using printf().

printf("Product = %.2lf", product);

Notice that, the result is rounded off to the second decimal place using %.2lf conversion character.

Program to Add Two Integers

 

Program to Add Two Integers

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

    int number1, number2, sum;
    
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    // calculating sum
    sum = number1 + number2;      
    
    printf("%d + %d = %d", number1, number2, sum);
    return 0;
}

Output

Enter two integers: 12
11
12 + 11 = 23

In this program, the user is asked to enter two integers. These two integers are stored in variables number1 and number2 respectively.

printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

Then, these two numbers are added using the + operator, and the result is stored in the sum variable.

sum = number1 + number2;
Adding two integers in C programming
Add Two Numbers

Finally, the printf() function is used to display the sum of numbers.

printf("%d + %d = %d", number1, number2, sum);

Paid UDEMY Courses for FREE with Certificate

FREE UDEMY Courses In this article, you will always find the updated list of all Paid UDEMY Courses for FREE with Certificate by using 100% ...