Practical 1

You might also like

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

Q1. Write a program in C to print integer or real numbers.

#include <stdio.h>
int main() {
int integer_num = 10;
float real_num = 3.1415;
printf("Integer Number: %d\n", integer_num);
printf("Real Number: %f\n", real_num);
return 0;
}

Q2. Write a program in C of Type conversion across assignment.

#include <stdio.h>
int main() {
int integer_num = 10;
float real_num;
real_num = integer_num; // Type conversion from int to float
printf("Integer Number: %d\n", integer_num);
printf("Real Number: %f\n", real_num);
return 0;
}

Q3. Write a program in C to find the value of following expressions:

a)
#include <stdio.h>
int main() {
int J = 5;
int K = 10;
int A = 2;
int result = J*K + A;
printf("Result: %d\n", result);
return 0;
}

b)
#include <stdio.h>
int main() {
int K = 5;
int J = 2;
int A = 10;
float I = 2.5;
float result = K/J * (A + 2.3 * I);
printf("Result: %f\n", result);
return 0;
}

Q4. Write a program in C to find value of following expressions:


a)
#include <stdio.h>
int main() {
int x = 10, y = 15
int result = x * ++y;
printf("Result: %d\n", result);
return 0;
}
b)
#include <stdio.h>
int main() {
int x = 10;
int y = 15;
int result;
result = x * y++;
printf("Result: %d\n", result);
printf("x: %d\n", x);
printf("y: %d\n", y);
return 0;
}

Q5. Write a program in C to find larger of two given numbers using Conditional Operator.

#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 20;
int larger_num = num1 > num2 ? num1 : num2;
printf("The larger number is: %d", larger_num);
return 0;
}

Q6. Write a program in C to find area of triangle using Heron's formula.

#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("Enter the sides of the triangle a, b, c: ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) / 2; //Cal Semi-perimeter
area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("The area of the triangle is: %.2f", area);


return 0;
}

Q7. Write a program in C to Calculate HRA according to following conditions, Given the Basic pay of employees:
#include <stdio.h>
int main() {

float basic_pay, hra;


printf("Enter the basic pay of the employee: ");
scanf("%f", &basic_pay);

if (basic_pay <= 2000) {


hra = 200;
} else if (basic_pay <= 4000) {
hra = 350;
} else {
hra = 500;
}

printf("The HRA for the employee is: %.2f", hra);


return 0;
}
Q8. Write a program in C to display grade of the student according to following conditions, given his marks :

#include <stdio.h>

int main() {
int marks;
char grade;

printf("Enter the marks of the student: ");


scanf("%d", &marks);

if (marks >= 80 && marks <= 100) {


grade = 'A';
} else if (marks >= 60 && marks <= 79) {
grade = 'B';
} else if (marks >= 50 && marks <= 59) {
grade = 'C';
} else if (marks >= 40 && marks <= 49) {
grade = 'D';
} else {
grade = 'E';
}

printf("The grade of the student is: %c", grade);

return 0;
}

Q9. Write a program in C to compute the number of days in a month using the SWITCH statement.

#include <stdio.h>
int main() {
int month, year, days;
printf("Enter the month (1-12): ");
scanf("%d", &month);
printf("Enter the year: ");
scanf("%d", &year);
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;

case 2:
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days = 29;
else
days = 28;
break;
default:
printf("Invalid month entered.");
return 0;
}
printf("Number of days in %d/%d: %d", month, year, days);
return 0;
}

Q10. Write a program in C to find square root of a number.

#include <stdio.h>
#include <math.h>
int main() {
double num, squareRoot;
printf("Enter a number: ");
scanf("%lf", &num);
squareRoot = sqrt(num);
printf("Square root of %.2lf is %.2lf", num, squareRoot);
return 0;
}

Q11. Write a program in C to read the user's option and to find any one of the following:
a)
#include <stdio.h>
int main() {
float length, width, area;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
printf("The area of the rectangle is: %.2f", area);
return 0;
}

b)
#include <stdio.h>
int main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("The area of the circle is: %f", area);
return 0;
}

c)
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, s, area;
printf("Enter the sides of the triangle: ");
scanf("%lf %lf %lf", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("The area of the triangle is: %lf", area);
return 0;
}

Q12. Write a program in C to find the sum of the first 10 natural numbers using the WHILE loop.

#include <stdio.h>
int main() {
int i = 1, sum = 0;
while (i <= 10) {
sum += i;
i++;
}
printf("The sum of first 10 natural numbers is %d", sum);
return 0;
}

Q13. Write a program in C to print the first 10 multiples of a given number using the DO WHILE loop.

#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
printf("First 10 multiples of %d are:\n", num);
do {
printf("%d x %d = %d\n", num, i, num * i);
i++;
} while(i <= 10);
return 0;
}

Q14. Write a program in C to find prime numbers between 1 and 50 using FOR loop.

#include <stdio.h>
int main()
{
int i, j, flag;
printf("Prime numbers between 1 and 50 are: ");
for(i=2; i<=50; i++)
{
flag = 0;
for(j=2; j<=i/2; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if(flag == 0 && i != 1)
printf("%d ", i);
}
return 0;
}
Q15. Write a program in C to find H.C.F. and L.C.M. of two given numbers.

#include <stdio.h>
int main() {
int num1, num2, hcf, lcm, temp, gcd;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
// Find H.C.F.
for (int i = 1; i <= num1 && i <= num2; ++i) {
if (num1 % i == 0 && num2 % i == 0) {
hcf = i;
}
}
// Find L.C.M.
temp = (num1 * num2);
while (num2 != 0) {
int t = num2;
num2 = num1 % num2;
num1 = t;
}
gcd = num1;
lcm = temp / gcd;
printf("H.C.F. of %d and %d = %d\n", num1, num2, hcf);
printf("L.C.M. of %d and %d = %d\n", num1, num2, lcm);
return 0;
}

Q16. Write a program in C to read elements of an array and write them.

#include <stdio.h>
int main() {
int arr[100], n, i;
printf("Enter the number of elements you want to store: ");
scanf("%d", &n);
printf("Enter %d elements in the array:\n", n);
for(i=0; i<n; i++) {
scanf("%d", &arr[i]);
}
printf("Elements in the array are: ");
for(i=0; i<n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Q17. Write a program in C to convert a Decimal number to its equivalent Binary number.

#include <stdio.h>
int main() {
int decimal, binary[20], index = 0;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
while (decimal > 0) {
binary[index] = decimal % 2;
decimal /= 2;
index++;
}
printf("The binary equivalent is: ");
for (int i = index - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
return 0;
}

Q18. Write a program in C to find the trace of a square matrix of order N*N where the trace is the sum of the
diagonal elements of the given square matrix.

#include<stdio.h>
int main()
{
int i, j, N, sum = 0;
printf("Enter the order of the matrix: ");
scanf("%d", &N);
int mat[N][N];
// Reading the matrix
printf("Enter the elements of the matrix: ");
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
scanf("%d", &mat[i][j]);
}
}
// Finding the trace
for(i = 0; i < N; i++)
{
sum += mat[i][i];
}
// Printing the trace
printf("The trace of the matrix is: %d", sum);
return 0;
}

Q19. Write a program in C to find the transpose of a matrix.

#include <stdio.h>
void transpose(int matrix[][3], int rows, int cols) {
int i, j, temp;
for(i = 0; i < rows; i++) {
for(j = i+1; j < cols; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
int main() {
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int rows = 3, cols = 3;
int i, j;
printf("Original Matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
transpose(matrix, rows, cols);
printf("\nTranspose Matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}

Q20. Write a program in C to find product of 2 matrices.

#include <stdio.h>
int main() {
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix: ");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix:\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix: ");
scanf("%d%d", &p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other.");
else {
printf("Enter the elements of second matrix:\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}

Q21. Write a program in C to print first 20 fibonacci numbers and print 5 fibonacci along each line.

#include <stdio.h>
int main() {
int n = 20; // number of fibonacci numbers to print
int n1 = 0, n2 = 1, n3; // initialization for the first three numbers
int count = 0; // to keep track of how many numbers have been printed on the current line
printf("The first 20 Fibonacci numbers are:\n");
for (int i = 1; i <= n; i++) {
n3 = n1 + n2; // calculate the next fibonacci number
printf("%d ", n3); // print the number
count++; // increment the count of numbers printed on the current line
if (count == 5) { // if we have printed 5 numbers on the current line
printf("\n"); // start a new line
count = 0; // reset the count
}
n1 = n2; // update the values of n1 and n2 for the next iteration
n2 = n3;
}
return 0;
}

Q22. Write a program in C to differentiate getchar(), getche() and getch() functions.

#include <stdio.h>
#include <conio.h>
int main() {
char c;
printf("Enter a character: ");
c = getchar();
printf("You entered: %c\n", c);
printf("Enter another character: ");
c = getche();
printf("\nYou entered: %c\n", c);
printf("Enter one more character: ");
c = getch();
printf("\nYou entered: %c\n", c);
return 0;
}

Q23. Write a program in C to read and write multiple lines of character using EOF statement.

#include <stdio.h>
int main() {
char text[1000];
int i = 0;
printf("Enter text (press Ctrl-D to stop):\n");
// Read input until EOF is encountered
while ((text[i] = getchar()) != EOF) {
i++;
}
text[i] = '\0';
printf("\nText entered:\n");
printf("%s", text);
return 0;
}

Q24. Write a program in C to reverse a given string.

#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, len;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0, j = len - 1; i < j; i++, j--)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
printf("Reversed string: %s", str);
return 0;
}

Q25. Write a program in C to count the number of characters, words and lines in a text.

#include<stdio.h>
int main(){
int c, nl=0, nw=0, nc=0, inword=0;
while((c=getchar())!=EOF){
nc++;
if(c=='\n')
nl++;
if(c==' ' || c=='\n' || c=='\t')
inword=0;
else if(inword==0){
inword=1;
nw++;
}
}
printf("Number of characters: %d\n",nc);
printf("Number of words: %d\n",nw);
printf("Number of lines: %d\n",nl);
return 0;
}

Q26. Write a program in C to illustrate the input/output of array of strings.

#include <stdio.h>
#define MAX_SIZE 100
int main() {
char strings[MAX_SIZE][MAX_SIZE];
int n;
printf("Enter the number of strings: ");
scanf("%d", &n);
// Input
printf("Enter %d strings:\n", n);
for (int i = 0; i < n; i++) {
scanf("%s", strings[i]);
}
// Output
printf("\nYou entered:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", strings[i]);
}
return 0;
}

Q27. Write a program in C that read a character and display whether the character is an alphabet or digit or any
other special character.

#include<stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
printf("%c is an alphabet.", c);
} else if(c >= '0' && c <= '9') {
printf("%c is a digit.", c);
} else {
printf("%c is a special character.", c);
}
return 0;
}

Q28. Write a program in C to extract a substring from given string.

#include <stdio.h>
#include <string.h>
int main()
{
char str[100], substr[100];
int i, j, len, sublen, flag = 0;
printf("Enter a string: ");
gets(str);
printf("Enter a substring to extract: ");
gets(substr);
len = strlen(str);
sublen = strlen(substr);
for (i = 0; i <= len - sublen; i++)
{
for (j = i; j < i + sublen; j++)
{
if (str[j] != substr[j - i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("Substring found at index %d.\n", i);
break;
}
}
if (flag == 1)
{
printf("Substring not found.\n");
}
return 0;
}

Q29. Write a program in C to show formal parameters and actual parameters.


#include <stdio.h>

// num1 and num2 are formal parameters


void print_numbers(int num1, int num2) {
printf("The numbers are %d and %d\n", num1, num2);
}
int main() {
int a = 5;
int b = 10;
// Call the function with actual parameters
print_numbers(a, b); // a and b are actual parameters

return 0;
}

Q30. Write a program in C to pass arguments to a function by call by value.

#include <stdio.h>
void increment(int num) {
num++;
printf("Inside the function, num = %d\n", num);
}
int main() {
int num = 5;
printf("Before function call, num = %d\n", num);
increment(num);
printf("After function call, num = %d\n", num);
return 0;
}

Output:
Before function call, num = 5
Inside the function, num = 6
After function call, num = 5

Q31. Write a program in C to pass arguments to a function by call by reference.

#include <stdio.h>
// function prototype
void swap(int*, int*);
int main() {
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
// passing arguments by call by reference
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
// function definition
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}

Output:
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10

Q32. Write a program in C to return more than one value by call by reference.

#include <stdio.h>
void calculate(int a, int b, int *sum, int *difference) {
*sum = a + b;
*difference = a - b;
}
int main() {
int a = 10;
int b = 5;
int sum, difference;
calculate(a, b, &sum, &difference);
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
return 0;
}

Output:
Sum : 15
Difference : 5

You might also like