C Prog

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

INDEX

Course Name : C Programming Course Code :VSEC102 SEM: I

Sr. Expt Course LAB


NO. Expt.
. Name of the Experiment Outcom Outcom PO
No.
No. e e
1 Write a C program to demonstrate Operators
Data Input and Output – getchar( ), putchar( ),
A 
scanf( ), printf( ), gets( ), puts( ).
1
2 Program to demonstrate Operators-Arithmetic,
B Relational, Logical and Assignment operators. 

3
Write a C program to find greatest of 3 numbers
A 
using if-else statement.
2
4
Write a C program to check whether the number
B 
is prime or not using while loop.
5 Write a C Program to create a simple
3 calculator using switch statement. 

8
Write a C program to find GCD of 2 numbers by
A 
demonstrating a function using call by value.
4
9 Write a C program to swap 2 numbers by
B demonstrating a function using call by 
reference.
10 Write a C program to find the factorial of a given
A integer using recursive function. 
5
11 Write a C program to find the Fibonnaci series
B using recursive function. 

12 Write a C program to find out the Smallest


A Element in 1D Array 
6 Write a C program to perform addition of two
B matrices using 2D Array. 

Department of Humanities and Basic science `


13

7
14


15

8
16


17

9
18


19

10
20


21
11  

Introduction
C is a programming language developed at AT&T’s BELL Laboratory of USA in 1972.
Dennis Ritchie designed it. Because of its reliability. C is very popular. C is highly

Department of Humanities and Basic science `


portable & it is well suited for structured programming. C program consists of collection
of functions.
Hardware Requirement: Desktop Computer / laptop computer
Software Requirement: Linux Operating System with GCC/ TURBOC IN WINDOWS OS
GCC
It is released by the Free Software Foundation. GCC is a Linux-based C compiler usually
operated via the command line. It often comes distributed with a linux installation, so if
you are running Unix or a Linux variant you likely have it on your system. You can invoke
gcc on a source code file simply by typing :-
Gcc filename
The default executable output of gcc is "a.out", which can be run by typing “. /a. Out”. It
is also possible to specify a name for the executable file at the command line by using
the syntax.
-o output file, as shown in the following example : -
Gcc filename -o output file
Again, you can run your program with "./output file". (The. / is there to ensure you run
the program for the current working directory.)
Note: If you need to use functions from the math library (generally functions from
math.h such as sin or sqrt), then you need to explicitly ask it to link with that library with
the -l flag and the
library 'm':
gcc filename -o outputfile–lm
Turbo C/C++
Open Turbo C from your Desktop or Programs menu. Select “File” from Menu bar and
select
option “New” and Save C program in filename .C extension.
To do compiling – Select -> Compile from menu and click-> compile.
If the compilation is success – you will see a “success” message. Else you will see the
number of errors.
To RUN the program – you may select ->Run from menu and click -> Run
Now you will see the output screen.

Department of Humanities and Basic science `


Subject: Course Code:
Semester: Course:
Laboratory No: Name of Subject Teacher:
Name of Student: Roll Id:

Experiment No.1 A

Aim: Write a C program to demonstrate Operators Data Input and Output – getchar( ),
putchar( ), scanf( ), printf( ), gets( ), puts( ).

Program Description:
Functions Used for Input and Output
C language offers us several built-in functions for performing input/output operations.
The following are the functions used for standard input and output:
● printf() function - Show Output
● scanf() function - Take Input
● getchar() and putchar() function
● gets() and puts() function
1. The printf() function
● The printf() function is the most used function in the C language.
● This function is defined in the stdio.h header file and is used to show output on
the console (standard output).
2. The scanf() function
When we want to take input from the user, we use the scanf() function and store the
input value into a variable.
scanf() function is defined in the C stdio.h library.
3. getchar() & putchar() functions
The getchar and putchar functions are used for taking character input from the user and
printing the character as output.
The getchar() function

Department of Humanities and Basic science `


● The getchar() function reads a character from the terminal and returns it as
an integer.
● This function reads only a single character at a time.
The putchar() function
● The putchar() function displays the character passed to it on the screen and
returns the same character.
● This function too displays only a single character at a time.
4. gets() & puts() functions
The gets and puts functions are used for taking string input and giving string output.
The gets() function
The gets() function reads a line of text from stdin(standard input) into the buffer
pointed to by str pointer, until either a terminating newline or EOF (end of file) occurs.
The puts() function
The puts() function writes the string str with a newline character ('\n') at the end
to stdout. On success, a non-negative value is returned.
Algorithm:
Step 1: Start
Step 2: Declare variables used in program(a,b,c,sum,avegare,d &str).
Step 3: Take input as 3 integer numbers, a,b,&c. Take input as a single character.
Take input as string of character.
Step 4: Caluculate sum as, sum=a+b+c and average as average=sum/3.
Step 5: Display sum, average, a single character & string of character.
Step 6: Stop
Flow Chart:

Department of Humanities and Basic science `


Program:
#include<stdio.h>
int main( )
{
int a,b,c;
float sum, average;
char d;
char str[30];
printf("Enter any three integers:");
scanf("%d%d%d", &a, &b, &c);
printf("Enter a character and string\n");
d=getchar();
//printf("Enter a string\n");
gets(str);

sum = a+b+c;
average = sum/3;

Department of Humanities and Basic science `


printf("Sum and average of three integers: %f %f\n",sum,average);
printf("Single character and string of character is: ");
putchar(d);
puts(str);
return 0;
}
OUTPUT:
Enter any three integers:10 20 5
Enter a character and string
S Seema
Sum and average of three integers: 35.000000 11.666667
Single character and string of character is:
S Seema
Conclusion: The program is compiled, executed and the output is verified

Department of Humanities and Basic science `


Subject: Course Code:
Semester: Course:
Laboratory No: Name of Subject Teacher:
Name of Student: Roll Id:

Experiment No.1 B
Aim: Program to demonstrate Operators-Arithmetic, Relational, Logical and Assignment
operators.
Objectives: Execute a program using Arithmetic, Relational and logical, Assignment,
Unary, Conditional, Bitwise, Comma, other operators.
Algorithm
Step1: Start
Step2: Declare and assign values to variable a and b as a=25, b=5;
Step3: Use arithmetic operators and print results.
Step4: Use Relational operators and print results.
Step5: Use Logical operators and print results.
Step 6: Use Assignment operators and print results.
Step 7: Stop.
Flowchart

Department of Humanities and Basic science `


Program: /*Program to use Arithmetic, Relational, Logical and Assignment, operators*/
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using arithmetic operators and printing results
printf("a+b=%d,a-b=%d,a*b=%d ",a+b,a-b,a*b);
printf("a/b=%d,a%b=%d ",a/b,a%b);
printf("a=%d b=%d\n",a,b);
++a;
b--;
// using relational operators and printing results
printf("a=%d b=%d\n",a,b);
printf("a<b:%d,a>b:%d,a<=b:%d,a>=b:%d, a==b:%d, a!=b=%d", a<b, a>b,a<=b,a>=b, a==b,
a!=b);

Department of Humanities and Basic science `


// using logical operators and printing results
printf("\na&&b=%d, a||b=%d, !a=%d ", a&&b, a||b, !a);
// using assignment operators and printing results
printf("\na=b:%d",a=b);
printf(" a+=b:%d",a+=b);
return 0;
}
EXPECTED OUTPUT:
a+b=30,a-b=20,a*b=125 a/b=5,a%b=0 a=25 b=5
a=26 b=4
a<b:0,a>b:1,a<=b:0,a>=b:1, a==b:0, a!=b=1
a&&b=1, a||b=1, !a=0
a=b:4 a+=b:8
Conclusion: The program is compiled, executed and the output is verified

Department of Humanities and Basic science `


Subject: Course Code:
Semester: Course:
Laboratory No: Name of Subject Teacher:
Name of Student: Roll Id:

Experiment No. 2A


Aim: Write a C program to find greatest of 3 numbers using if-else statement.
Theory: Comparison Operators
Comparison operators are used to compare two values (or variables). This is important
in programming, because it helps us to find answers and make decisions.
The return value of a comparison is either 1 or 0, which means true (1) or false (0).
These values are known as Boolean values.
logical conditions from mathematics:
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
● Equal to a == b
● Not Equal to: a != b
C has the following conditional statements:
● Use if to specify a block of code to be executed, if a specified condition is true
● Use else to specify a block of code to be executed, if the same condition is false
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed

The if Statement
Use the if statement to specify a block of code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}

Department of Humanities and Basic science `


Use the else statement to specify a block of code to be executed if the condition is false.
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
If...Else (Ternary Operator)
There is also a short-hand if else, which is known as the ternary operator because it
consists of three operands. It can be used to replace multiple lines of code with a single
line. It is often used to replace simple if else statements:
Syntax
variable = (condition) ? expressionTrue : expressionFalse;

Algorithm:
Step 1: Start
Step 2: Input 3 numbers as n1, n2, n3
Step 3: Check if n1 >= n2 && n1 >= n3 then go to step 6.
Step 4: Check if n2 >= n1 && n2 >= n3 then go to step 7.
Step 5: Check if n3 >= n1 && n3 >= n2 then go to step 8.
Step 6: Greatest number is n1. Go to step 9.
Step 7: Greatest number is n2. Go to step 9.
Step 8: Greatest number is n3. Go to step 9.

Department of Humanities and Basic science `


Step 9: stop
Flow Chart:

Program:
#include <stdio.h>

int main() {

int n1, n2, n3;


printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);

Department of Humanities and Basic science `


// if n2 is greater than both n1 and n3, n2 is the largest
else if (n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
else if (n3 >= n1 && n3 >= n2)
printf("%d is the largest number.", n3);
return 0;
}
Output:
Enter three numbers: 20 50 90
90 is the largest number.
Conclusion: The program is compiled, executed and the output is verified

Department of Humanities and Basic science `


Subject: Course Code:
Semester: Course:
Laboratory No: Name of Subject Teacher:
Name of Student: Roll Id:

Experiment No.2 B
Aim: Write a C program to check whether the number is prime or not using while loop.
Theory: Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more
readable.
While Loop
The while loop loops through a block of code as long as a specified condition is true:
Syntax
while (condition) {
// code block to be executed
}
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at least
once, even if the condition is false, because the code block is executed before the
condition is tested.
Algorithm

Department of Humanities and Basic science `


Step 1: Start
Step 2: Declare variables n, i, flag and c as integer.
Step 3: Take a number n as input.
Step 4: flag=0;
Step 5: if (n==0) || (n==1) then flag=1;
Step 6: i=2;
Step 7: if (i<=n-1) go to step 8 else go to step 9.
Step 8: if (n % i == 0) then flag = 1 and go to step ; else i++ and go to step 7;
Step 9: if (flag ==1) then n is prime number else n is not prime number.
Step 10: If you want to continue then go to step 3 else go to step 11.
Step 11: Stop

Flowchart:

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

Department of Humanities and Basic science `


int n, i, flag = 0, c;
do
{
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
i=2;
while (i<n-1)
{
if (n % i == 0) {
flag = 1;
break;
}
i++;
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
printf("\nDo you want to continue(1:yes/0:no)\n");
scanf("%d", &c);
} while (c==1);
return 0;
}

Department of Humanities and Basic science `


Output:
Enter a positive integer: 90
90 is not a prime number.
Do you want to continue(1:yes/0:no)
1
Enter a positive integer: 78
78 is not a prime number.
Do you want to continue(1:yes/0:no)
1
Enter a positive integer: 12347
12347 is not a prime number.
Do you want to continue(1:yes/0:no)
0
Conclusion: The program is compiled, executed and the output is verified

Department of Humanities and Basic science `


Subject: Course Code:
Semester: Course:
Laboratory No: Name of Subject Teacher:
Name of Student: Roll Id:

Experiment No.3
Aim: Write a C Program to create a simple calculator using switch statement.
Theory: Switch Statement
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
Syntax
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
● The switch expression is evaluated once
● The value of the expression is compared with the values of each case
● If there is a match, the associated block of code is executed
● The break statement breaks out of the switch block and stops the execution
● The default statement is optional, and specifies some code to run if there is no
case match
The example below uses the weekday number to calculate the weekday name:

Department of Humanities and Basic science `


Example

int day = 4;

switch (day) {
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;
}

// Outputs "Thursday" (day 4)

The break Keyword


When C reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for
more testing.
A break can save a lot of execution time because it "ignores" the execution of all the rest
of the code in the switch block.

Department of Humanities and Basic science `


Algorithm:
1. While true go to step 2.
2. Take choice as the Operator (+,-,*,/) & enter x to exit.
3. if choice == x then go to step 10
4. Enter 2 numbers a &b as input.
5. If choice== ‘+’ then print a+b & go to step 10.
6. If choice== ‘-’ then print a-b & go to step 10.
7. If choice== ‘*’ then print a*b & go to step 10.
8. If choice== ‘/’ then print a/b & go to step 10.
9. if choice not between +,-,*,/ &x then print Invalid Operator Input & go to step 10.
10. stop.
Flow Chart:

Program:

Department of Humanities and Basic science `


// C Program to create a simple calculator using switch
// statement
#include <stdio.h>
#include <stdlib.h>
// driver code
int main()
{
// switch variable
char choice;
// operands
int x, y;
while (1)
{
printf("Enter the Operator (+,-,*,/) Enter x to exit\n");
scanf(" %c", &choice);
// for exit
if (choice == 'x')
{
exit(0);
}
printf("Enter the two numbers: ");
scanf("%d %d", &x, &y);
// switch case with operation for each operator
switch (choice)
{
case '+':
printf("%d + %d = %d\n", x, y, x + y);
break;

Department of Humanities and Basic science `


case '-':
printf("%d - %d = %d\n", x, y, x - y);
break;
case '*':
printf("%d * %d = %d\n", x, y, x * y);
break;
case '/':
printf("%d / %d = %d\n", x, y, x / y);
break;
default:
printf("Invalid Operator Input\n");
}
}
return 0;
}
Output:
Enter the Operator (+,-,*,/)
Enter x to exit
+
Enter the two numbers: 10 20
10 + 20 = 30
Enter the Operator (+,-,*,/)
Enter x to exit
-
Enter the two numbers: 20 9
20 - 9 = 11
Enter the Operator (+,-,*,/)
Enter x to exit

Department of Humanities and Basic science `


x
Conclusion: The program is compiled, executed and the output is verified

Department of Humanities and Basic science `


Department of Humanities and Basic science `

You might also like