0% found this document useful (0 votes)
6 views

Practicals

The document contains multiple C programming examples covering various topics such as calculating area and circumference of a circle, performing arithmetic operations, finding the largest of three numbers, and calculating simple and compound interest. It also includes programs for handling arrays, structures, and various mathematical computations like finding roots of quadratic equations and generating Fibonacci series. Additionally, it features programs for input/output operations, data type sizes, and control structures like loops and conditionals.

Uploaded by

Geisha P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Practicals

The document contains multiple C programming examples covering various topics such as calculating area and circumference of a circle, performing arithmetic operations, finding the largest of three numbers, and calculating simple and compound interest. It also includes programs for handling arrays, structures, and various mathematical computations like finding roots of quadratic equations and generating Fibonacci series. Additionally, it features programs for input/output operations, data type sizes, and control structures like loops and conditionals.

Uploaded by

Geisha P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

I.

Write a C program to calculate area and circumference of a circle


#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
circumference = 2 * PI * radius;
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
return 0;
}
I.Write a C program to perform addition, subtraction, division and multiplication of two numbers using function.

#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}

I.Write a C program to find the largest of three numbers using ternary operators.
#include<stdio.h>
void main()
{
int a, b, c, larg;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Enter third number: ");
scanf("%d", &c);
larg = (a>b)?((a>c)?a:c):((b>c)?b:c);
printf("Largest number is: %d", larg);
}
I.Write a C program to take input of name, rollno and marks obtained by a student in 4 subjects of 100 marks each
and display the name, rollno with percentage score secured.
#include <stdio.h>
struct student {
char name[50];
int roll;
int mark1,mark2,mark3,mark4;
int total;
float percent;
} s;

int main() {
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s",&s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter mark1: ");
scanf("%f", &s.mark1);
printf("Enter mark2: ");
scanf("%f", &s.mark2);
printf("Enter mark3: ");
scanf("%f", &s.mark3);
printf("Enter mark4: ");
scanf("%f", &s.mark4);
s.total=s.mark1+s.mark2+s.mark3+s.mark4;
s.percent=s.total*100/400;

printf("Displaying Information:\n");
printf("Name:%s \n",s.name);
printf("Roll number: %d\n", s.roll);
printf("Percentage: %.1f %%\n", s.percent);
return 0;
}
II.Write a C program using function to find the average of ‘n’ numbers using variable length arguments.
#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1) {
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
I.Write a C program to swap values of two variables with and without using third variable.
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
I.Write a C program to display the size of every data type using “sizeof” operator.
#include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;

// sizeof evaluates the size of a variable


printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));

return 0;
}
II.Write a C program to generate pay roll of employees using structures and pointers. Ref pg no 58

I.Write a C program to input two numbers and display the maximum number.Ref pg 2

II.Write a C Program to perform addition of all elements in Array


#include<stdio.h>
int main()
{.
int arr[100], size, i, sum = 0;
printf("Enter array size\n");
scanf("%d",&size);
printf("Enter array elements\n");
for(i = 0; i < size; i++)
scanf("%d",&arr[i]);
for(i = 0; i < size; i++)
sum = sum + arr[i];
printf("Sum of the array = %d\n",sum);
return 0;
}

I.Write a C program to print whether a given number is even or odd Ref pg no 8

I.Write a C program to calculate simple and compound interest. Ref pg no 7


II.Write a C Program to Search an element in array.
#include<stdio.h>
void main()
{
int i,flag=0,pos,n,a[5];
printf("Enter elements in array \t");
for(i=0;i<=4;i++) {
printf("\nEnter element number \t",i+1);
scanf("%d",&a[i]); }
printf("\nEnter the element to be searched in array");
scanf("%d",&n);
for(i=0;i<=4;i++) {
if(a[i]==n) {
printf("\nSearch Successful");
printf("\nElement %d is found at %d position",n,i+1);
flag=1;
break;
} }
if(flag==0) {
printf("\nElement does not found");
} }

I.Write a C program to find the roots of quadratic equation.


#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// condition for real and different roots


if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}

// condition for real and equal roots


else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}

// if roots are not real


else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}

return 0; }
II.Write a C Program to find the largest, smallest , second largest, second smallest, middle element in an Array.

#include <stdio.h>
#include <limits.h>

void find_elements(int arr[], int n) {


if (n < 3) {
printf("Array should have at least 3 elements to find second largest and second smallest.\n");
return;
}

int largest = INT_MIN, second_largest = INT_MIN;


int smallest = INT_MAX, second_smallest = INT_MAX;
int middle_element;
for (int i = 0; i < n; i++) {
if (arr[i] > largest) {
second_largest = largest;
largest = arr[i];
} else if (arr[i] > second_largest && arr[i] != largest) {
second_largest = arr[i];
}

if (arr[i] < smallest) {


second_smallest = smallest;
smallest = arr[i];
} else if (arr[i] < second_smallest && arr[i] != smallest) {
second_smallest = arr[i];
}
}
middle_element = arr[n / 2];
printf("Largest Element: %d\n", largest);
printf("Second Largest Element: %d\n", second_largest);
printf("Smallest Element: %d\n", smallest);
printf("Second Smallest Element: %d\n", second_smallest);
printf("Middle Element: %d\n", middle_element);
}

int main() {
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements: \n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
find_elements(arr, n);

return 0;
}
I.Write a C program to input name, marks of 5 subjects of a student and display the name of the student, the total
marks scored, percentage scored and the class of result.(similar to above program)

II.Write a C Program to reverse the array elements in C Programming.


#include <stdio.h>
void reverseArray(int arr[], int n) {
int temp;
int start = 0;
int end = n - 1;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: \n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
reverseArray(arr, n);
printf("Reversed array: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
I.Write a C Program to Check Whether a Number is Prime or not. Ref pg no18

II.Write a C Program to multiply two 3 X 3 Matrices. Ref pg no 74

I.Write a C program to check whether the entered year is leap year or not (a year is leap if it is divisible by 4 and
divisible by 100 or 400.) ref pg no 14

I.Write a C program to print even number and odd numbers from 1 to 20. Ref pg no 8

II.Write a C Program to perform transpose of a 3 X 3 Matrices. Ref pg no 73

I.Write a C program to count number of digits in a given integer.


#include <stdio.h>
int main()
{
int n; // variable declaration
int count=0; // variable declaration
printf("Enter a number");
scanf("%d",&n);
while(n!=0)
{
n=n/10;
count++;
}

printf("\nThe number of digits in an integer is : %d",count);


return 0;
}
I.Write a C program to find the factorial of a number.Ref pg no 15

II.Write a C Program to find the determinant value of a 3 X 3 Matrices.


#include <stdio.h>
int determinant(int matrix[3][3]) {
int det;
// Determinant = a*(ei - fh) - b*(di - fg) + c*(dh - eg)
det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])
- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])
+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
return det;
}
int main() {
int matrix[3][3];
int i, j, det;
printf("Enter the elements of the 3x3 matrix:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
det = determinant(matrix);
printf("The determinant of the matrix is: %d\n", det);
return 0; }
I.Write a C program to print the reverse of a given integer. (above)

II.Write a C program to convert the Celsius into Fahrenheit and Fahrenheit to Celsius.(Ref pg no 8)

II.Write a C program to generate the prime numbers between given two limits. Ref 18

I.Write a C program to check the given integer is prime or not. Ref 18

II.Write a C program to generate Fibonacci series using recursion. Ref 19

I.Write a C program to check whether a number is positive, negative or zero using switch case. Ref pg 9

II.Write a C program to generate numbers divisible by 2 not by 3 and 5. ref 22

I.Write a C program to compute grade of students using if else adder. The grades are assigned as followed:

Marks Grade marks<50 F 50≤marks< 60 C 60≤marks<70 B 70≤marks<80 B+ 80≤marks<90 A 90≤marks≤ 100 A+

ref pg 12

I.Write a C program to check number is Armstrong or not.


#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
II.Write a C program to find a given number is positive or negative or zero. Ref pg no 9

I.Write a C program to find the factorial of a number using recursion. Ref pg no 15

II.Write a C program to swap values of two numbers using pass by value and pass by reference. Ref pg no 37

I.Write a C program to print day name using switch case.


#include<stdio.h>
int main (){
int week =3;
switch(week){
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3 :
printf("wednesday");
break;
case 4 :
printf("Thursday");
break;
case 5 :
printf("Friday");
break;
case 6 :
printf("Saturday");
break;
case 7 :
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7."); ) )

II.Write a C program to display the number in words. Ref pg 23

I.Write a C program to print the sum of digits of a number using for loop. above

II.Write a C program to perform arithmetic operation using switch case. Ref pg 25

I.Write a C program to check whether a number is Palindrome or not. Ref pg 21

II.Write a C program to find the average of N Numbers using iterative statements.


#include <stdio.h>
int main() {
int n;
float sum = 0.0, num, average;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%f", &num);
sum += num; // Add each number to the sum
}
average = sum / n;
printf("The average of the %d numbers is: %.2f\n", n, average);
return 0; }
I.Write a C program to generate Fibonacci series ref pg 19
II.Write a C program to perform addition, subtraction, division and multiplication of two numbers using function
above
Couldn’t find answers for
II.Write a C program to find the sum of three numbers using command line arguments.

II.Write a C program to find the division of two numbers using command line arguments.

II.Write a C program to add n numbers using variable length arguments.

II.Write a C program to find the multiplication of two numbers using command line arguments.

II.Write a C program to find the subtraction of two numbers using command line arguments

II.Write a C Program to perform addition and subtraction of an element in 2- D Array.

II.Write a C Program to find the sum of the diagonal element of a 3-D Array.

i.Binary search

You might also like