0% found this document useful (0 votes)
53 views

1) WAP To Print Number

The document contains 6 code snippets that demonstrate C programming concepts like input/output, arithmetic operations, data types, and variable swapping. Each code snippet includes comments explaining what the code is doing and uses functions like printf, scanf, and sizeof. The snippets show how to: 1) take integer input and print it, 2) take two integers as input, add them and print the sum, 3) take two floating point numbers as input, multiply them and print the product to two decimal places.

Uploaded by

Yogesh Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

1) WAP To Print Number

The document contains 6 code snippets that demonstrate C programming concepts like input/output, arithmetic operations, data types, and variable swapping. Each code snippet includes comments explaining what the code is doing and uses functions like printf, scanf, and sizeof. The snippets show how to: 1) take integer input and print it, 2) take two integers as input, add them and print the sum, 3) take two floating point numbers as input, multiply them and print the product to two decimal places.

Uploaded by

Yogesh Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1)WAP to print number.

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

printf("Enter an integer: "); // printf() dislpays the formatted output

scanf("%d", &number); // scanf() reads the formatted input and stores them

printf("You entered: %d", number); // printf() displays the formatted output

return 0;
}

2)WAP to take integer input from user and add them.

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

printf("Enter two integers: ");

scanf("%d %d", &firstNumber, &secondNumber); // Two integers entered by user is stored using
scanf() function

sumOfTwoNumbers = firstNumber + secondNumber; // sum of two numbers in stored in variable


sumOfTwoNumbers

printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers); // Displays sum

return 0;
}

3) WAP to take float input from user and multiply them.

#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;
}

4) WAP to take integer input from user and divide them and print remainder and quotient.

#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");


scanf("%d", &dividend);

printf("Enter divisor: ");


scanf("%d", &divisor);

quotient = dividend / divisor; // Computes quotient

remainder = dividend % divisor; // Computes remainder

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);

return 0;
}

5)WAP to find size of datatypes.

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

// Sizeof operator is used to evaluate the size of a variable

printf("Size of int: %ld bytes\n",sizeof(integerType));


printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));
return 0;
}

6)WAP to take input from user and swap the values of the 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;
}

You might also like