0% found this document useful (0 votes)
3 views7 pages

Basic C Programs

The document contains a series of C programming exercises that demonstrate basic operations such as printing text, performing arithmetic operations, and converting units. Each exercise includes a brief description of the task, followed by the corresponding C code. The programs typically print results to the console and wait for a key press before exiting.

Uploaded by

Nagaimani
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)
3 views7 pages

Basic C Programs

The document contains a series of C programming exercises that demonstrate basic operations such as printing text, performing arithmetic operations, and converting units. Each exercise includes a brief description of the task, followed by the corresponding C code. The programs typically print results to the console and wait for a key press before exiting.

Uploaded by

Nagaimani
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.

Print Hello World


Q: Write a program to print “Hello, World!” and then wait for a key press.

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

void main()
{
printf("Hello, World!\n");
getch();
}

2. Print a Number
Q: Print the integer 42, then wait for a key press.

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

void main()
{
int x = 42;
printf("%d\n", x);
getch();
}

3. Add Two Numbers


Q: Add 5 and 7, print result, then wait.

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

void main()
{
int a = 5, b = 7;
int sum = a + b;
printf("%d\n", sum);
getch();
}

4. Multiply Floats
Q: Multiply 2.5 and 4.0, print product, then wait.

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

void main()
{
float a = 2.5f, b = 4.0f;
float prod = a * b;
printf("%f\n", prod);
getch();
}

5. Fahrenheit to Celsius
Q: Convert 77 °F to °C (C = (F–32)×5/9), print, then wait.

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

void main()
{
double f = 77.0;
double c = (f - 32.0) * 5.0 / 9.0;
printf("%.2f\n", c);
getch();
}
6. Swap Two Numbers

Q: Swap a=10 and b=20, print swapped values, then wait.

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

void main()
{
int a = 10, b = 20, temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
getch();
}

7. ASCII Value

Q: Print the ASCII code of 'A', then wait.

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

void main()
{
char ch = 'A';
int ascii = ch;
printf("%d\n", ascii);
getch();
}

8. Data Type Sizes

Q: Display sizes of int, float, double, char, then wait.

#include <stdio.h>
#include <conio.h>
void main()
{
printf("int: %zu\n", sizeof(int));
printf("float: %zu\n", sizeof(float));
printf("double: %zu\n", sizeof(double));
printf("char: %zu\n", sizeof(char));
getch();
}

9. Complex Number Addition


Q: Compute (3+2i) + (1+4i), print result, then wait.

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

void main()
{
int a1 = 3, b1 = 2;
int a2 = 1, b2 = 4;
int real = a1 + a2;
int imag = b1 + b2;
printf("%d + %di\n", real, imag);
getch();
}

10. Simple Interest


Q: Calculate simple interest (P=1000, R=5%, T=2 years), print, then wait.

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

void main() {
double P = 1000.0, R = 5.0, T = 2.0;
double SI = (P * R * T) / 100.0;
printf("%.2f\n", SI);
getch();
}

1. Addition and Subtraction


Q: Write a program that prints the sum and difference of 9 and 4, then waits for a key press.

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

void main()
{
int a = 9, b = 4;
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
getch();
}

2. Multiplication
Q: Write a program that prints the product of 9 and 4, then waits.

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

void main()
{
int a = 9, b = 4;
printf("Product = %d\n", a * b);
getch();
}

3. Division
Q: Write a program that prints quotient when dividing 9 by 4 (integer division), then pause.
#include <stdio.h>
#include <conio.h>

void main()
{
int a = 9, b = 4;
printf("Quotient = %d\n", a / b);
getch();
}

4. Modulus (Remainder)
Q: Write a program that prints the remainder when dividing 9 by 4, then waits.

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

void main()
{
int a = 9, b = 4;
printf("Remainder = %d\n", a % b);
getch();
}

5. Mixed Arithmetic on Both Integers and Floats


Q: Write a program that prints both integer and floating-point results of dividing 9 by 4, then
waits.

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

void main()
{
int a = 9, b = 4;
float fa = 9.0f, fb = 4.0f;
printf("Integer division: %d\n", a / b);
printf("Floating-point division: %.2f\n", fa / fb);
getch();
}

You might also like