Introduction to C Programming LAB Assignment 1
Q1.Write a program to print “Hello” in C language
#include <stdio.h>
int main() {
printf("Hello");return 0;
}
```
Q2.Write a program to add 2 constant numbers in C language
```#include <stdio.h>
int main() {
int num1 = 5;
int num2 = 7;int sum = num1 + num2;
printf("The sum of %d and %d is: %d", num1, num2, sum);
return 0;
}```
Q.3Write a program to perform all the arithmetic operations in c languages :
#include <stdio.h>
int main() {
int a = 10, b = 5;
int add = a + b;
int sub = a - b;
int mul = a * b;
int div = a / b;int rem = a % b;
printf("Sum of a and b is: %d\n", add);
printf("Difference between a and b is: %d\n", sub);
printf("Product of a and b is: %d\n", mul);
printf("Quotient when a is divided by b is: %d\n", div);printf("Remainder when a is divided by b is: %d\n", rem);
return 0;
}
```
Q4. Write a program to find area of circle in c language
#include<stdio.h>#define PI 3.14
int main()
{
float radius, area;
printf("Enter the radius of circle: ");
scanf("%f", &radius);area = PI * radius * radius;
printf("Area of circle with radius %.2f is: %.2f", radius, area);
return 0;
}
// Output:
// Enter the radius of circle: 5
// Area of circle with radius 5.00 is: 78.50
Q5.Write a program to find area of triangle in c language
#include<stdio.h>
int main()
{
float base, height, area;
printf("Enter the base of triangle: ");
scanf("%f", &base);
printf("Enter the height of triangle: ");
scanf("%f", &height);area = 0.5 * base * height;
printf("Area of triangle with base %.2f and height %.2f is: %.2f", base, height, area);
return 0;
}
// Output:
// Enter the base of triangle: 6
// Enter the height of triangle: 4
// Area of triangle with base 6.00 and height 4.00 is: 12.00Q.6 Write a program to calculate simple interest in c language
#include<stdio.h>
int main()
{
float principal, rate, time, interest;
printf("Enter the principal amount: ");
scanf("%f", &principal);printf("Enter the rate of interest: ");
scanf("%f", &rate);printf("Enter the time period in years: ");
scanf("%f", &time);
interest = (principal * rate * time) / 100;
printf("Simple interest for principal amount %.2f, rate %.2f and time period %.2f is: %.2f", principal, rate, time, interest);
return 0;
}
// Output:
// Enter the principal amount: 5000
// Enter the rate of interest: 5
// Enter the time period in years: 2// Simple interest for principal amount 5000.00, rate 5.00 and time period 2.00 is: 500.00
Q.7 Write a program to swap two numbers using three variables in c language
#include<stdio.h>
int main()
{
int num1, num2, temp;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);// swapping logic using a temporary variable
temp = num1;num1 = num2;
num2 = temp;
printf("After swapping, the first number is %d and the second number is %d", num1, num2);
return 0;
}
// Output:
// Enter the first number: 10
// Enter the second number: 20
// After swapping, the first number is 20 and the second number is 10Q8. Write a program to find largest of two numbers using if-else statement in c language
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);printf("Enter the second number: ");
scanf("%d", &num2);if(num1 > num2)
{
printf("%d is the largest number.", num1);
}else
{
printf("%d is the largest number.", num2);
}
return 0;
}
// Output:
// Enter the first number: 10
// Enter the second number: 20
// 20 is the largest number.
Q.9 Write a program to find smallest of two numbers using if-else statement in c language
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);printf("Enter the second number: ");
scanf("%d", &num2);
if(num1 < num2)
{
printf("%d is the smallest number.", num1);
}
else
{
printf("%d is the smallest number.", num2);
}return 0;
}
// Output:
// Enter the first number: 10
// Enter the second number: 20
// 10 is the smallest number.
Q.10 Write a program to find even/odd number using if-else statement in c language
#include<stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);if(num % 2 == 0)
{
printf("%d is an even number.", num);
}
else
{
printf("%d is an odd number.", num);
}return 0;
}
// Output:
// Enter a number: 7
// 7 is an odd number.
Q11. Write a program to find largest of three numbers using nested if-else statement in c language
#include<stdio.h>
int main()
{
int num1, num2, num3, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
{
largest = num1;
}
else
{
largest = num3;
}}
else
{
if(num2 > num3)
{
largest = num2;
}
else
{
largest = num3;
}
}
printf("Largest number is %d.", largest);
return 0;
}
// Output:
// Enter three numbers: 10 15 5
// Largest number is 15.
Q.12 Write a program to print the series of n-numbers using for loop structure in c language
#include<stdio.h>
int main()
{
int n, i;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Series of %d numbers: ", n);
for(i=1; i<=n; i++)
{
printf("%d ", i);
}
return 0;
}// Output:
// Enter the value of n: 5
// Series of 5 numbers: 1 2 3 4 5
Q13. Write a program to calculate factorial of a given number using for loop in c language
#include<stdio.h>
int main()
{
int n, i, fact=1;
printf("Enter a number: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
fact = fact * i;
}
printf("Factorial of %d is %d", n, fact);
return 0;
}
// Output:
// Enter a number: 5
// Factorial of 5 is 120