Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Programming in 'C' Lab Manual

Progressive experience in the world and science ?

Uploaded by

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

Programming in 'C' Lab Manual

Progressive experience in the world and science ?

Uploaded by

bygraceofgod2003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

NeelamVidyaVihar, Sijoul, Mailam, Madhubani, BIHAR – 847235

Website:http://www.sandipuniversity.edu.in Email: info@ sandipuniversity.edu.in

School of Computer Science and Engineering


Programming in 'C'
Lab Manual

Sl. No. Name of the Experiment

1 Write a Program to find the root of the given quadratic equation using switch case.

2 Write a C Program to generate and print first N FIBONACCI numbers.

3
Write a Program to find the GCD and LCM of two integer numbers.

4 Write a C Program that reverse a given integer number and check whether the
number is palindrome or not.

5 Write a Program to find the factorial of a number using function.

6 Write a C Program to find the length of a string without using the built – in function.

7 Write a C Program to find if a character is alphabetic or numeric or special


character.

8 Write a C Program to accept a sentence and convert all lowercase characters to


uppercase and vice-versa.
9 Write a C Program to compute the sum of even numbers and the sum of odd
numbers.

Experiment No: 01
Title: Write a Program to find the root of the given quadratic equation using switch case.

Aim: - : To Write a Program to find the root of the given quadratic equation using switch case

Hardware /Software requirement:- Computer, Keyboard, Text Editor or Integrated


Development Environment (IDE) Examples: Visual Studio Code, Dev-C++, Code::Blocks, Xcode (for
macOS), etc, C Compiler: Examples: GCC (GNU Compiler Collection), Clang, Microsoft Visual C++
Compiler, etc

THEORY: The theory behind the program to find the roots of a quadratic equation involves
several key concepts from algebra and programming. Let's break down the theory step by step:

Roots of a Quadratic Equation

The roots of a quadratic equation can be real or complex and are determined by the values of aaa,
bbb, and ccc, as well as the discriminant (DDD) of the equation, given by:

D=b2−4ac

 If D>0: The quadratic equation has two distinct real roots.


 If D=0: The quadratic equation has exactly one real root (a repeated root).
 If D<0: The quadratic equation has two complex roots (conjugate pairs).

Steps in the Program

1. Input Coefficients: The program prompts the user to enter the coefficients a, b, and c of
the quadratic equation.
2. Calculate Discriminant: Using the entered coefficients, the program calculates the
discriminant D using the formula D=b2−4ac
3. Switch-case Statement: Based on the value of the discriminant D, the program uses a
switch-case statement to determine the type of roots and calculate them accordingly:

PROGRAM:

#include <stdio.h>
#include <math.h>

int main() {
float a, b, c;
float d, root1, root2;

// Input coefficients
printf("Enter coefficients (a, b, c) of quadratic equation: ");
scanf("%f %f %f", &a, &b, &c);

// Calculate discriminant
d = b*b - 4*a*c;
int rootType = (d>0)?1:(d==0)?2:3;

// Switch-case to determine roots based on discriminant


switch (rootType) {
case 1:
// Two distinct real roots
root1 = (-b + sqrt(d)) / (2*a);
root2 = (-b - sqrt(d)) / (2*a);
printf("Two distinct real roots: %f and %f\n", root1, root2);
break;
case 2:
printf(“roots are real and the same”);

root1 = root2 = -b / (2*a);


printf("root1=root2=%f\n”, root1);

break;
case 3:

printf("no real roots ");

break;

default:printf(“ end of cases”);


}

return 0;
}

OUTPUT:

CONCLUSION:

Note: avoid comment line,it is written only for understanding purpose.


EXPERIMENT NO.2-
Title:Write a C Program to generate and print first N FIBONACCI numbers.
Aim: To write a C Program to generate and print first N FIBONACCI numbers
THEORY:

Fibonacci Sequence:The Fibonacci sequence is a series of numbers where each number is the
sum of the two preceding ones. It starts with 0 and 1, and the sequence continues indefinitely:
0,1,1,2,3,5,8,13,21,34,…0, 1, 1, 2, 3, 5, 8, 13, 21, 34, \ldots0,1,1,2,3,5,8,13,21,34,…

Objective:-
The objective of this program is to generate and print the first N Fibonacci numbers.

Implementation Steps:

1. Initialization:
o Two variables (fib1 and fib2) are initialized to store the first two Fibonacci
numbers:
 fib1 = 0
 fib2 = 1

2. User Input:
o Prompt the user to enter the value of N, which specifies how many Fibonacci
numbers to generate.

3. Special Cases:
o Print fib1 if N >= 1.
o Print fib2 if N >= 2.

4. Loop for Generating Fibonacci Numbers:


o Use a for loop starting from i = 3 up to N.
o Inside the loop, calculate the next Fibonacci number (nextFib) as the sum of fib1
and fib2.
o Update fib1 to fib2 and fib2 to nextFib for the next iteration.
o Print each Fibonacci number as it is generated.

PROGRAM:
#include <stdio.h>

int main() {
int N;
int fib1 = 0, fib2 = 1, nextFib;
// Input the number of Fibonacci numbers to generate
printf("Enter the number of Fibonacci numbers to generate: ");
scanf("%d", &N);

// Print the first N Fibonacci numbers


printf("First %d Fibonacci numbers:\n", N);

// Special case for first Fibonacci number


if (N >= 1) {
printf("%d ", fib1);
}
// Special case for second Fibonacci number
if (N >= 2) {
printf("%d ", fib2);
}

// Generate and print remaining Fibonacci numbers


for (int i = 3; i <= N; i++) {
nextFib = fib1 + fib2;
printf("%d ", nextFib);
fib1 = fib2;
fib2 = nextFib;
}

printf("\n");

return 0;
}

OUTPUT:

CONCLUSION:

EXPERIMENT NO.3-
Title: Write a Program to find the GCD and LCM of two integer numbers.
Aim:To Write a Program to find the GCD and LCM of two integer numbers

THEORY: Theory: Finding GCD and LCM of Two Integers

Objective:

The objective of this program is to find the Greatest Common Divisor (GCD) and Least
Common Multiple (LCM) of two integers.
Definitions:

 Greatest Common Divisor (GCD): The largest positive integer that divides each of the integers
without leaving a remainder.
 Least Common Multiple (LCM): The smallest positive integer that is divisible by both integers.

Implementation Steps:

 Input: Prompt the user to input two integers num1 and num2.
 GCD Function: Use a while loop to compute the GCD of num1 and num2.
 LCM Calculation: Compute the LCM using the formula (num1 * num2) / gcd.
 Output: Display the computed GCD and LCM.

PROGRAM:
#include <stdio.h>

int main() {
int num1, num2, gcd, lcm;

// Input two integers


printf("Enter first integer: ");
scanf("%d", &num1);
printf("Enter second integer: ");
scanf("%d", &num2);

// Find GCD
int a = num1, b = num2;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
gcd = a;

// Find LCM
lcm = (num1 * num2) / gcd;

// Output results
printf("GCD of %d and %d is: %d\n", num1, num2, gcd);
printf("LCM of %d and %d is: %d\n", num1, num2, lcm);

return 0;
}

OUTPUT:
CONCLUSION:

EXPERIMENT NO.4-
Title: Write a C Program that reverse a given integer number and check whether the number is
palindrome or not
Aim:To Write a C Program that reverse a given integer number and check whether the number is
palindrome or not
THEORY:  This program efficiently reverses an integer and checks if it is a palindrome using basic
arithmetic operations and a loop.

 It handles positive integers and assumes valid input.

 For negative numbers, the program can be modified to handle them by considering their absolute
values or specific requirements.

 Edge cases like single-digit numbers (which are inherently palindromes) are naturally handled by the
logic.

PROGRAM:

#include <stdio.h>

int main() {
int num, reversedNum = 0, originalNum, remainder;

// Input an integer from user


printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num; // Store the original number

// Reverse the number


while (num != 0) {
remainder = num % 10; // Get the last digit
reversedNum = reversedNum * 10 + remainder; // Append it to reversedNum
num = num / 10; // Remove the last digit from num
}

// Check if the original number is equal to its reversed version


if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}

return 0;
}

OUTPUT:

CONCLUSION:

EXPERIMENT NO.5-
Title: Write a Program to find the factorial of a number using function.

Aim: to write a Program to find the factorial of a number using function.

THEORY:  The program prompts the user to enter a non-negative integer.

 It reads the input and calculates the factorial using a for loop.
 It initializes factorial to 1 and multiplies it by each integer from 1 to num.
 Finally, it prints the computed factorial.

PROGRAM:
#include <stdio.h>

int main() {
int num, factorial = 1;

// Input an integer from user


printf("Enter a non-negative integer: ");
scanf("%d", &num);
// Check if the number is non-negative
if (num < 0) {
printf("Factorial of negative numbers is not defined.\n");
} else {
// Calculate factorial iteratively
for (int i = 1; i <= num; ++i) {
factorial *= i;
}

// Output the factorial


printf("Factorial of %d is: %d\n", num, factorial);
}

return 0;
}

OUTPUT:

CONCLUSION:

EXPERIMENT NO.6-
Title: Write a C Program to find the length of a string without using the built – in function
Aim: To write a C Program to find the length of a string without using the built – in function

THEORY:  This program provides a straightforward approach to calculate the length of a


string without using the strlen() function.

 It efficiently counts characters until the null character is encountered.


 Edge cases such as an empty string or strings with spaces are handled correctly.
 Ensure the input string does not exceed the allocated size (100 in this example) to avoid
buffer overflow. Adjust the size accordingly based on your specific requirements.

PROGRAM:

#include <stdio.h>
int main() {
char str[100]; // Assuming maximum string length is 100 characters
int length = 0;

// Input a string from user


printf("Enter a string: ");
scanf("%s", str); // Reading string from user input

// Calculate length of the string


while (str[length] != '\0') {
length++;
}

// Output the length of the string


printf("Length of the string '%s' is: %d\n", str, length);

return 0;
}

OUTPUT:

CONCLUSION:

EXPERIMENT NO.7-
Title: Write a C Program to find if a character is alphabetic or numeric or special character.

Aim: To Write a C Program to find if a character is alphabetic or numeric or special character.

THEORY:  This program efficiently determines the type of character based on its ASCII value
ranges.

 It handles single characters and categorizes them as alphabetic, numeric, or special.


 Adjustments can be made to include additional checks or handle specific character sets based
on requirements.

PROGRAM:
#include <stdio.h>
int main() {
char ch;

// Input a character from user


printf("Enter a character: ");
scanf("%c", &ch);

// Check if the character is alphabetic


if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("%c is an alphabetic character.\n", ch);
}
// Check if the character is numeric
else if (ch >= '0' && ch <= '9') {
printf("%c is a numeric character.\n", ch);
}
// If neither alphabetic nor numeric, it's a special character
else {
printf("%c is a special character.\n", ch);
}

return 0;
}

OUTPUT:

CONCLUSION:

EXPERIMENT NO.8-
Title: Write a C Program to accept a sentence and convert all lowercase characters to uppercase
and vice-versa.
Aim:To Write a C Program to accept a sentence and convert all lowercase characters to
uppercase and vice-versa.
THEORY:
 This program efficiently converts the case of characters in a sentence based on their current
case.
 It uses standard library functions islower(), toupper(), isupper(), and tolower() for
character case checking and conversion.
 Adjustments can be made to handle special characters or specific requirements based on the
input sentence format

PROGRAM:
#include <stdio.h>
int main() {
char sentence[1000];
int i = 0;

// Input a sentence from user


printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);

// Toggle case of each character in the sentence


while (sentence[i] != '\0') {
if (sentence[i] >= 'a' && sentence[i] <= 'z') {
sentence[i] = sentence[i] - 'a' + 'A'; // Convert lowercase to uppercase
} else if (sentence[i] >= 'A' && sentence[i] <= 'Z') {
sentence[i] = sentence[i] - 'A' + 'a'; // Convert uppercase to lowercase
}
i++;
}

// Output the converted sentence


printf("Converted sentence: %s\n", sentence);

return 0;
}

EXPERIMENT NO.9-
Title: Write a C Program to compute the sum of even numbers and the sum of odd numbers.
Aim:To write a C Program to compute the sum of even numbers and the sum of odd numbers

1. THEORY: Array in C:
o An array is a data structure that stores a fixed-size sequential collection of
elements of the same type.
o Arrays in C are zero-indexed, meaning the first element is accessed using index 0.
2. Even and Odd Numbers:
o Even numbers are integers divisible by 2 without a remainder (num % 2 == 0).
o Odd numbers are integers not divisible by 2 (num % 2 != 0).
3. Modulus Operator (%):
o In C, the modulus operator % returns the remainder of a division operation.
o It is used to determine if a number is even or odd (num % 2).
4. Function Definition and Usage:
o Functions in C are blocks of code that perform a specific task.
o The findSum function is defined to compute the sum of even and odd numbers in
an array.
o It takes parameters arr[] (the array of integers) and size (the number of
elements in the array).
5. Looping Constructs (for loop):
o The for loop is used to iterate over each element of the array.
o It initializes an index (i) to zero, checks a condition (i < size), and increments
the index after each iteration (i++).
6. Conditional Statements (if-else):
o Conditional statements like if-else are used to make decisions based on
conditions.
o Inside the loop, each element of the array is checked:
 If the element is even (arr[i] % 2 == 0), it is added to sumEven.
 If the element is odd (arr[i] % 2 != 0), it is added to sumOdd.
7. Output Using printf:
o The printf function is used to print formatted output to the console.
o It prints the sums of even and odd numbers computed by the findSum function.

Example Execution:

Given the array { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }:

 Sum of even numbers: 2 + 4 + 6 + 8 + 10 = 30


 Sum of odd numbers: 1 + 3 + 5 + 7 + 9 = 25

Program:

#include <stdio.h>

void findSum(int arr[], int size) {


int sumEven = 0, sumOdd = 0;

for (int i = 0; i < size; i++) {


if (arr[i] % 2 == 0) {
sumEven += arr[i];
} else {
sumOdd += arr[i];
}
}

printf("Sum of even numbers: %d\n", sumEven);


printf("Sum of odd numbers: %d\n", sumOdd);
}

int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int size = sizeof(arr) / sizeof(arr[0]);

findSum(arr, size);

return 0;
}

OUTPUT:

CONCLUSION:

You might also like