Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CORE PRACTICAL I: C PROGRAMMING - 24AIU1CP

Sample Programs

1.C Program to Print “Hello World”.


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

printf("Hello World"); //Print Hello World


return 0;
}

2.C Program to Print the Integer value (User Input)


#include <stdio.h>
int main()
{
int num; // Declare the variables
printf("Enter the integer: "); // Input the integer
scanf("%d", &num);
printf("Entered integer is: %d", num); // Display the integer

return 0;
}

3. C Program to Print Your Own Name (User Input)


#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name); // user input will be taken here
return 0;
}
1|Page
4. C Program to Add Two Numbers
#include <stdio.h>
int main()
{
int A, B, sum = 0;
printf("Enter two numbers A and B : \n"); // Ask user to enter the two numbers
scanf("%d%d", &A, &B); // Read two numbers from the user
sum = A + B; // Calculate the addition of A and B '+' operator
printf("Sum of A and B is: %d", sum); // Print the sum
return 0;
}

5. C Program to Multiply Two Numbers


#include <stdio.h>
int main() {
float a, b, product; // Using float instead of double
printf("Enter two numbers: ");
scanf("%f %f", &a, &b); // %f for float, &a and &b for addresses of variables
product = a * b; // Calculating product
printf("Product = %.2f\n", product); // %.2f displays number up to 2 decimal points
return 0;
}

6. C Program to calculate the area of a circle based on user input of the radius
#include <stdio.h>
int main() {
double pi = 3.14;
printf("Value of pi: %lf\n", pi); // Printing a double with %lf
double radius;
printf("Enter the radius of a circle: ");
scanf("%lf", &radius); // Reading a double with %lf
double area = pi * radius * radius; // Calculation using the entered radius
printf("Area of the circle with radius %.2lf is: %.2lf\n", radius, area);
return 0;
}
2|Page
7. C Program for arithmetic calculation
#include <stdio.h>
int main() {
int num1, num2; //Declaration of Variables
printf("Enter the first number: "); // Prompt the user to enter the first number
scanf("%d", &num1);
printf("Enter the second number: "); // Prompt the user to enter the second number
scanf("%d", &num2);

// Perform arithmetic operations


int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
double quotient = (double) num1 / num2; // Cast num1 to double for correct division

// Display the results


printf("Sum of %d and %d is: %d\n", num1, num2, sum);
printf("Difference of %d and %d is: %d\n", num1, num2, difference);
printf("Product of %d and %d is: %d\n", num1, num2, product);
printf("Quotient of %d divided by %d is: %.2lf\n", num1, num2, quotient);
return 0;
}

8.C Program to check whether a given number is even or odd.


#include <stdio.h>
void main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(n % 2 == 0) // Logical calculation for finding even or even
printf("%d is even number.", n);
else
printf("%d is odd number.", n);
}
3|Page

You might also like