0% found this document useful (0 votes)
15 views11 pages

C Programs for Basic Calculations

The document contains 10 C programming examples that demonstrate basic programming concepts like input/output, arithmetic operations, functions, conditional statements, loops, and more. Each example includes the source code, its output, and an aim to write a program to perform a specific task like printing a name, adding numbers, finding factors of a number, or generating a multiplication table. The examples are intended to teach basic C programming skills.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

C Programs for Basic Calculations

The document contains 10 C programming examples that demonstrate basic programming concepts like input/output, arithmetic operations, functions, conditional statements, loops, and more. Each example includes the source code, its output, and an aim to write a program to perform a specific task like printing a name, adding numbers, finding factors of a number, or generating a multiplication table. The examples are intended to teach basic C programming skills.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Aim:- WRITE A PROGRAM to display your name.

Write another program to


print message with inputted name.

#include <stdio.h>
int main()
{ printf("Raju\n");
return 0;
}

OUTPUT:-

Raju
---------------

#include <stdio.h>

int main() {
char name[100];

printf("Enter your name: ");

scanf("%s", name);

printf("Hello, My name is %s.\n", name);

return 0;
}

OUTPUT:-

Enter your name: Raju

Hello, My name is Raju


2. Aim:- WRITE A PROGRAM to add two numbers.

#include <stdio.h>

int main() {
double num1, num2, sum;

printf("Enter first number: ");


scanf("%lf", &num1);

printf("Enter second number: ");


scanf("%lf", &num2);

sum = num1 + num2;

printf("The sum of %.2lf and %.2lf is %.2lf\n", num1, num2, sum);

return 0;
}

OUTPUT:-

Enter first number: 2


Enter second number: 4
The sum of 2.00 and 4.00 is 6.00
---------------
3. Aim:- WRITE A PROGRAM to find the square of a given number.

#include <stdio.h>

int main() {
double number, square;

printf("Enter a number: ");


scanf("%lf", &number);

square = number * number;

printf("The square of %.2lf is %.2lf\n",number,square);

return 0;
}

OUTPUT:-

Enter a number: 5
The square of 5.00 is 25.00
---------------
4. Aim:- WRITE A PROGRAM to calculate the average of three real numbers.

#include <stdio.h>

int main() {
double num1, num2, num3, average;

printf("Enter the first number: ");


scanf("%lf", &num1);

printf("Enter the second number: ");


scanf("%lf", &num2);

printf("Enter the third number: ");


scanf("%lf", &num3);

average = (num1 + num2 + num3) / 3.0;

printf("The average of %.2lf, %.2lf, and %.2lf is %.2lf\n", num1, num2, num3,
average);

return 0;
}

OUTPUT:-

Enter the first number: 2


Enter the second number: 4
Enter the third number: 6
The average of 2.00, 4.00, and 6.00 is 4.00
---------------

5. Aim:- Write a program to Find ASCII Value of a Character.

#include <stdio.h>

int main() {
char ch;

printf("Enter a character: ");


scanf("%c", &ch);

printf("The ASCII value of '%c' is %d\n", ch,ch);

return 0;
}

OUTPUT:-

Enter a character: a
The ASCII value of 'a' is 97
---------------
6. Aim:- WRITE A PROGRAM to Find the Size of int, float, double and char.

#include <stdio.h>

int main() {

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


printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu byte\n", sizeof(char));

return 0;
}

OUTPUT:-

Size of int: 4 bytes


Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
---------------
7. Aim:- WRITE A
PROGRAM using
function to find the
largest of three

numbers.

#include <stdio.h>

int largest(int a, int b, int c) {


int largest = a;
if (b > largest) {
largest = b;
}
if (c > largest) {
largest = c;
}
return largest;
}

int main()
{ int a =
10; int b =
40; int c =
30;

int result = largest(a, b, c); printf("The


largest number is: %d\n", result);

return 0;
}

OUTPUT:-

The largest number is: 40


---------------

[Link]:- WRITE A PROGRAM find whether the given number is a prime


number.

#include <stdio.h>

int main() {
int num, i;

printf("Enter a number: ");


scanf("%d", &num);

if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}

for (i = 2; i<= num / 2; ++i) {


if (num % i == 0) {
printf("%d is not a prime number.\n", num);
return 0;
}
}

printf("%d is a prime number.\n", num);


return 0;
}

OUTPUT:-

Enter a number: 13
13 is a prime number.
---------------

[Link]:- WRITE A PROGRAM Make a Simple Calculator Using switch...case.

#include <stdio.h> int


main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /):");
scanf(" %c", &operator);
printf("Enter two numbers: "); scanf("%lf
%lf", &num1, &num2); switch (operator)
{
case '+':
result = num1 + num2;
printf("Result: %.2lf + %.2lf = %.2lf\n", num1, num2, result); break;
case '-':
result = num1 - num2;
printf("Result: %.2lf - %.2lf %.2lf\n", num1, num2, result); break;
case '*':
result = num1 * num2;
printf("Result: %.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2lf / %.2lf = %.2lf\n", num1, num2,result);
} else {
printf("Error! Division by zero is not allowed.\n");
}
break;
default:
printf("Error! Invalid operator.\n");
}
return 0;
}
OUTPUT:-
Enter an operator (+, -, *, /): *
Enter two numbers: 3 7
Result: 3.00 * 7.00 = 21.00

10. Aim:- WRITE A PROGRAM Generate Multiplication Table Using for


loop.

#include <stdio.h>

int main() {
int number, i;

printf("Enter a number to generate its multiplication table:

"); scanf("%d", &number);

printf("Multiplication Table for %d:\n", number);

for (i = 1; i<= 10; ++i) {


printf("%d × %d = %d\n", number, i, number * i);
}

return 0;
}

OUTPUT:-
Enter a number to generate its multiplication table: 9
Multiplication Table for 9:
9×1=9
9 × 2 = 18
9 × 3 = 27
9 × 4 = 36
9 × 5 = 45
9 × 6 = 54
9 × 7 = 63
9 × 8 = 72
9 × 9 = 81
9 × 10 = 90
---------------

Common questions

Powered by AI

The use of 'for' loops in the multiplication table program offers concise and clear iteration over a fixed number of steps (1 through 10). This loop structure combines initialization, condition checking, and increment operations in a single line, reducing the chance of errors and improving readability compared to 'while' or 'do-while' loops, which separate these components .

Modifying the program to handle negative numbers or zero involves adding condition checks before generating the table. For negative inputs, the logic should decide how to display or store results correctly (e.g., using absolute values or noting negative outcomes). Handling zero is vital as it simplifies output because any number multiplied by zero is zero. These considerations enhance the program's robustness and widen its applicability .

Improvements include providing clearer instructions and feedback if invalid or multiple characters are input, enhancing user understanding of program expectations. Implementing error handling mechanisms to prompt users to retry with correct input can avoid processing unintended characters and improve user experience by guiding them towards successful program execution .

When using division in the simple calculator, it is crucial to check if the divisor is zero to prevent runtime errors. The program handles division by zero by checking if the second number is zero before performing the division. If it is zero, the program outputs an error message stating, 'Error! Division by zero is not allowed' .

The program determines if a number is prime by checking if it is divisible by any number up to half of itself, starting from 2. If no divisors are found, the number is declared prime . However, this approach is inefficient for larger numbers as it unnecessarily checks divisibility for numbers greater than the square root of the number, increasing computational time.

Functions are used to encapsulate the logic of finding the largest number among three. This modular approach improves code readability and reusability, allowing the function to be called multiple times with different inputs without rewriting the logic. The justification for using a function in this context is to isolate specific functionality, which aids in debugging and potential future enhancements .

Input validation is critical in program interaction to prevent errors and ensure data integrity. Without validation, users can provide unintended, erroneous, or malicious input leading to crashes, incorrect calculations, or security vulnerabilities. Ensuring data is of expected type and within valid ranges mitigates risks of undefined behavior or exploitation through buffer overflows and similar attacks .

Calculating the size of different data types using 'sizeof' provides insight into the memory allocation specific to the system's architecture. It reveals how much memory the system allocates for each data type, which can vary depending on whether the system is 32-bit or 64-bit. This knowledge is crucial for efficient memory management and optimization in C programs .

The program finds the largest of three numbers but does not handle equality explicitly. If two or all numbers are equal, it simply returns the first occurrence as the largest by default, which could be misleading if users expect handling of ties or further processing for equal values. This limitation doesn't affect correctness but impacts the program's robustness and user expectations .

The 'scanf' function uses '%lf' to read double values because 'double' is a data type requiring double precision, and '%lf' specifies this precision. Using '%f', which is intended for 'float', could lead to incorrect reading and storage of values, as it might not handle the increased precision requirement of 'double', leading to undefined behavior or incorrect results .

You might also like