Sie sind auf Seite 1von 28

PR-1

Aim-: The length, breadth of a rectangle and radius of a circle are


input through the keyboard. Write a
program to calculate the area of the rectangle and circle.

Hardware Requirement-: intel dual core 3.1GHz


processor, 1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:
Printf()-: printf() and scanf() functions are inbuilt library functions in C which
are available in C library by default.These functions are declared and related
macros are defined in “stdio.h” which is a header file.
We have to include “stdio.h” file as shown in below C program to
make use of these printf() and scanf() library functions.

1. C printf() function:
printf() function is used to print the “character, string, float, integer,
octal and hexadecimal values” onto the output screen.
We use printf() function with %d format specifier to display the value
of an integer variable.
Similarly %c is used to display character, %ffor float variable, %sfor
string variable, %lffor double and %x for hexadecimal variable.
To generate a newline,we use “\n” in C printf()
statement. The general form of printf( ) function is,
printf ( "<format string>", <list of variables> ) ;
<format string> can contain,
%f for printing real values
%d for printing integer values
%c for printing character values

Note:
C language is case sensitive.For example, printf() and scanf() are
different from Printf() and Scanf(). All characters in printf() and
scanf() functions must be in lower case.
2. C scanf() function:
scanf() function is used to read character, string, numeric data
from keyboard
Consider below example program where user enters a character.
This value is assigned to the variable “ch” and then
displayed.Then, user enters a string and this value is assigned to
the variable “str”
and then displayed.

Note that the ampersand (&) before the variables in the


scanf( ) function is a must. & is an ‘Address of’ operator. It
gives the location number used by the variable in memory.
When we say
&a, we are telling scanf( ) at which memory location should
it store the value supplied by the user from the keyboard.

Syntax of scanf function is

scanf (“format string”, argument list);


The format string must be a text enclosed in double quotes. It contains
the information for interpreting the entire data for connecting it into
internal representation in memory.
Example: integer (%d) , float (%f) , character (%c) or string (%s).
The argument list contains a list of variables each preceded by the address
list and separated by comma. The number of argument is not fixed; however
corresponding to each argument there should be a format specifier. Inside
the format string the number of argument should tally with the number of
format specifier.
Example: if i is an integer and j is a floating point number, to input these
two numbers we may use
scanf (“%d%f”, &i, &j);
Program-:
Area of a circle-:

Area of a rectangle-:

#include<stdio.h>
{

float length, breadth, area;


printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;

printf("Area of a Reactangle = %f\n", area);

Output-:

Enter the breadth and length


20 30
Area of a Reactangle = 600.000000

Conclusion-: Hence we have studies how to write a program to find


area of rectangle and circle.
PR-2
Aim-: Any integer is input through the keyboard. Write a program to find
out whether it is an odd number or even number.

Hardware Requirement-: intel dual core 3.1GHz


processor, 1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:
The if Statement

Like most languages, C uses the keyword if to implement the


decision control instruction. The general form of if statement looks like this:

if ( this condition is true )


execute this statement ;

The keyword if tells the compiler that what follows is a decision


control instruction. The condition following the keyword if is
always enclosed within a pair of parentheses. If the condition,
whatever it is, is true, then the statement is executed. If the
condition is not true then the statement is not executed; instead the
program skips past it.

Syntax for each C decision control statements are given in below table with
description.
Decision control
Syntax/Description
statements

Syntax:
if (condition)
{Statements;}
if
Description:
In these type of statements, if condition is true, then respective
block of code is executed.
Syntax:
if (condition)
{Statement1; Statement2; }
else
if…else {Statement3; Statement4; }

Description:
In these type of statements, group of statements are executed
when condition is true. If condition is false, then else part
statements are executed.
Syntax:
if (condition1){Statement1;}
else if(condition2)
{Statement2;}
else
Nested if
Statement 3;

Description:
If condition 1 is false, then condition 2 is checked and statements
are executed if it is true.If condition 2 also gets failure, then else
part is executed.

If...else if...else Statement


An if statement can be followed by an optional else if...else statement, which
is very useful to test various conditions using single if...else if statement.
When using if...else if..else statements, there are few points to keep in mind −
An if can have zero or one else's and it must come after any else if's.

An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be
tested.

Program-:
#include<stdio.h>

void main()

int number;

printf("Enter the number to check whether it is even or odd:");

scanf ("%d", &number);

if (number%2==0)
{

printf("The entered number is an EVEN number.");

else

printf("The entered number is an ODD number.");

Output-:

Concusion: Hence we have studied how to write a program to find the number is
even or odd using mod operator.
PR-3
Aim-:
Write a program to print the multiplication table of the number entered by the user. The table
should get displayed in the following form.
11 * 1 =11
11 * 2 = 22

Hardware Requirement-: intel dual core 3.1GHz processor,


1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:

We can create Multiplication table using for loop.

For Loop-:
for loop is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times.

Syntax

The syntax of a for loop in C programming language is −


for ( init; condition; increment )

{
statement(s);
}

Here is the flow of control in a 'for' loop −


The init step is executed first, and only once. This step allows you to
declare and initialize any loop control variables. You are not required to
put a statement here, as long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and the flow
of control jumps to the next statement just after the 'for' loop.
After the body of the 'for' loop executes, the flow of control jumps back
up to the increment statement. This statement allows you to update any
loop control variables. This statement can be left blank, as long as a
semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes and
the process repeats itself (body of loop, then increment step, and then
again condition). After the condition becomes false, the 'for' loop
terminates.
Program-:

#include <stdio.h>
int main()
{
int n, i;

printf("Enter an integer: ");


scanf("%d",&n);

for(i=1; i<=10; ++i)


{
printf("%d * %d = %d \n", n, i, n*i);
}

return 0;
}

Output-:

Concusion: Hence we have studied how to write a program to print a table of given
number.
PR-4

Aim-: Any year is entered through the keyboard. Write a program to


determine whether the year is leap or not using the logical operators.

Hardware Requirement-: intel dual core 3.1GHz processor,


1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC Compiler,


Ubuntu OS.

Theory-:
Logical operators in C:
These operators are used to perform logical operations on the given
expressions.
There are 3 logical operators in C language. They are, logical AND (&&),
logical OR (||) and logical NOT (!).
Operators Example/Description
&& (logical (x>5)&&(y<5)
AND) It returns true when both conditions are true
(x>=10)||(y>=10)
|| (logical OR)
It returns true when at-least one of the condition is true
!((x>5)&&(y<5))
! (logical NOT) It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false

&& Operator : Both conditions are true


|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
Program-:

#include<stdio.h>
main()
{

int year;

printf("Enter the year to check for a leap year:");


scanf ("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

{
printf("The entered year is a leap year.");
}
else
{

printf("The entered year is not a leap year.");


}

}
Output-:

Concusion: Hence we have studied how to write a program to print a table of given
number.
Pr-5
Aim-:
Write a menu driven program which has the following options:
1. Addition of two integers
2. Subtraction
3. Multiplication
4. Division
Anything Exit
Make use of switch statement.
Hardware Requirement-: intel dual core 3.1GHz processor,
1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:
Decisions Using switch

The control statement that allows us to make a decision from the


number of choices is called a switch, or more correctly a switch-
case-default, since these three keywords go together to make up
the control statement. They most often appear as follows:
switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
The integer expression following the keyword switch is any C expression that
will yield an integer value. The keyword case is followed by an integer or a
character constant. Each constant in each case must be different from all the
others. The “do this” lines in the above form of switch represent
any valid C statement.
What happens when we run a program containing a switch?
First, the integer expression following the keyword switch is evaluated.
The value it gives is then matched, one by one, against the constant values
that follow the case statements. When a match is found, the program executes
the statements following that case, and all subsequent case and default
statements as well. If no match is found with any of the case statements, only
the statements following the default are executed.

The break statement when used in a switch takes the control


outside the switch. However, use of continue will not take the control to the
beginning of switch as one is likely to believe. The switch statement is very
useful while writing menu
driven programs.
Program-:

#include<stdio.h>
void main()
{
int option,a,b,c; printf("\n 1.
Addition"); printf("\n 2.
Subtraction"); printf("\n 3.
Multiplication"); printf("\n 4.
Division"); printf("\n
Anything. Exit ");
printf("\nEnter option as 1,2,3 or 4");
scanf("%d",&option);
printf("\nEnter first and second number");
scanf("\n%d%d",&a,&b);
switch(option)
{
case 1:
c=a+b;
printf("The addition is %d\n",c);
break;
case 2:
c=a-b;
printf("The subtraction is %d\n",c);
break;
case 3:
c=a*b;
printf("The multiplication is %d\n",c);
break;
case 4:
c=a/b;
printf("The division is %d\n",c);
break;
default:
printf("Exit");
}
}
Output-:

Concusion: Hence we have studied how to write a program to print a table of given
number.
Pr-6
Aim-: Write a program for the addition of two matrices using a 2-D
array.

Hardware Requirement-: intel dual core 3.1GHz processor,


1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:
C programming language allows multidimensional arrays. Here
is the general form of a multidimensional array declaration −
type name[size1][size2]...[sizeN];

For example, the following declaration creates a three dimensional integer


array −
int threedim[5][10][4];

Two-dimensional Arrays
The simplest form of multidimensional array is the two-dimensional array. A
two-dimensional array is, in essence, a list of one-dimensional arrays. To
declare a two-dimensional integer array of size [x][y], you would write
something as follows −
type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C
identifier. A two-dimensional array can be considered as a table which will have
x number of rows and y number of columns.
Thus, every element in the array a is identified by an element name of the
form a[ i ][ j ], where 'a' is the name of the array, and 'i' and 'j' are the
subscripts that uniquely identify each element in 'a'.

Initializing Two-Dimensional Arrays


Multidimensional arrays may be initialized by specifying bracketed values for
each row. Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};

The nested braces, which indicate the intended row, are optional. The following
initialization is equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Program-:

#include <stdio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


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 elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

return 0;
}
Output-:

Concusion: Hence we have studied how to write a program to print a table of given
number.
PR -7

Aim-: Write a program to demonstrate the following string handling


functions strlen(), strcpy(), strcmp(), strcat(), strrev().

Hardware Requirement-: intel dual core 3.1GHz


processor, 1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:

String Functions:

Function Work of Function

strlen() Calculates the length of string

strcpy() Copies a string to another

string strcat() Concatenates(joins) two

strings strcmp() Compares two string

strrev() Reverses the given string

1) strlen()
In C, strlen() function calculates the length of string. It takes only
one argument, i.e, string name.
Syntax of strlen()
temp_variable = strlen(string_name);

Function strlen() returns the value of type integer.

2) strcpy()

Function strcpy() copies the content of one string to the content of


another string.It takes two arguments.
Syntax of strcpy()
strcpy(destination,source);

Here, source and destination are both the name of the string. This
statement, copies the content of string source to the content of string
destination.
3) strcat()
In C programming, strcat() concatenates(joins) two strings. It takes two
arguments, i.e, two strings and resultant string is stored in the first
string specified in the argument.

Syntax of strcat()
strcat(first_string,second_string);

4) strcmp() In C programming, strcmp() compares two string and returns


value 0, if the two strings are equal. Function strcmp() takes two arguments,
i.e, name of two string to compare.
Syntax of strcmp()
temp_varaible=strcmp(string1,string2);

5) strrev()-: strrev( ) function reverses a given string in C language.


strrev( )function is non standard function which may not available in
standard library in C.

Program-:

strlen()
#include <stdio.h>
#include <string.h>
int main(){
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
char c[20];
printf("Enter string: ");
gets(c);
printf("Length of string a=%d \n",strlen(a));
//calculates the length of string before null charcter.
printf("Length of string b=%d \n",strlen(b));
printf("Length of string c=%d \n",strlen(c));
return 0;
}

strcpy()
#include <stdio.h>
#include <string.h>
int main(){
char a[10],b[10];
printf("Enter string: ");
gets(a);
strcpy(b,a); //Content of string a is copied to string b.
printf("Copied string: ");
puts(b);
return 0;
}
strcat()

#include <stdio.h>
#include <string.h>
int main(){
char str1[]="This is ", str2[]="programiz.com";
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}

strcmp()

#include<stdio.h>
#include<string.h>
int main()
{
int i;
char a[255]="abc";
char b[255]="abc";
i=strcmp(a,b);
printf("%d",i);
return 0;
}

strrev()
#include<stdio.h>
#include<string.h>

int main()
{
char name[30] = "Hello";

printf("String before strrev( ) : %s\n",name);

printf("String after strrev( ) : %s",strrev(name));

return 0;
}
strlen()

strcpy()

strcat()
This is programiz.com

strcmp()
strrev()

Concusion: Hence we have studied how to write a program to print a table of given
number.
PR -8
Aim-: Write a program to define a function for finding the factorial of a
number.

Hardware Requirement-: intel dual core 3.1GHz processor,


1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and all the most trivial
programs can define additional functions. You can divide up your code into
separate functions.

A function is a group of statements that together perform a task. Every C


program has at least one function, which is main(), and all the most trivial
programs can define additional functions.
You can divide up your code into separate functions. How you divide up your
code among different functions is up to you, but logically the division is such
that each function performs a specific task.
A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the
function.
The C standard library provides numerous built-in functions that your program
can call. For example, strcat() to concatenate two strings, memcpy() to copy
one memory location to another location, and many more functions.
A function can also be referred as a method or a sub-routine or a procedure,
etc.

A function definition in C programming consists of a function header and a


function body. Here are all the parts of a function −
Return Type − A function may return a value. The return_type is the
data type of the value the function returns. Some functions perform the
desired operations without returning a value. In this case, the return_type
is the keyword void.
Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument. The parameter list refers to the type,
order, and number of the parameters of a function. Parameters are
optional; that is, a function may contain no parameters.
Function Body − The function body contains a collection of statements
that define what the function does.
Program-:

#include <stdio.h>

int main()
{

int num;
printf("\n >> PROGRAM TO FIND FACTORIAL OF GIVEN NUMBER using Function
<<\n");
printf("\n Enter the Number whose Factorial you want: ");
scanf("%d",&num);
printf("\n The factorial of %d
is%d.\n\n", num,factorial(num));
return 0;

factorial(num1)
{
int i,fact=1;
for(i=num1; i>=2 ; i--)
{
fact = fact * i;
}
return fact;
}
Output-:

Concusion: Hence we have studied how to write a program to print a table


of given number.
PR 9-:
AIM: Write a program to increment and decrement the values of a variables
using call by reference.
Hardware Requirement-: intel dual core 3.1GHz processor,
1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:
The call by reference method of passing arguments to a function copies the
address of an argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call. It means the
changes made to the parameter affect the passed argument.
Unlike “call by value”, in this method the value of num1 got changed because
the address of num1 is passed as an argument so the increment operation is
performed on the value stored at the address.

Program 1 (Increment)-:
int increment(int *var)
{
*var = *var+1;
return *var;
}
int main()
{
int num1=20;
int num2 = increment(&num1);
printf("num1 value is: %d", num1);
printf("num2 value is: %d", num2);
return 0;
}

Output:
num1 value is: 21
num2 value is: 21
Program 2 (Increment)-:

int decrement(int *var)


{
*var = *var-1;
return *var;
}
int main()
{
int num1=20;
int num2 = decrement(&num1);
printf("num1 value is: %d", num1);
printf("num2 value is: %d", num2);
return 0;
}

Output:
num1 value is: 19
num2 value is: 19

Concusion: Hence we have studied how to write a program to print a table of given
number.
PR 10-:
Aim-: Create a structure to read and display the following information of a
student:
Roll number, Name, Department, Course, Year of joining.

Hardware Requirement-: intel dual core 3.1GHz processor,


1GB RAM, 500GT SATA HDD, KBD, Mouse, Monitor.

Software Requirement-: Text Editor, Terminal, GCC


Compiler, Ubuntu OS.

Theory-:
A struct in the C programming language (and many derivatives) is a complex
data type declaration that defines a physically grouped list of variables to be
placed under one name in a block of memory, allowing the different variables
to be accessed via a single pointer, or the struct declared name which returns
the same.

Arrays allow to define type of variables that can hold several data items of the
same kind. Similarly structure is another user defined data type available in C
that allows to combine data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of
your books in a library. You might want to track the following attributes about
each book −
Title
Author
Subject
Book ID

Defining a Structure
To define a structure, you must use the struct statement. The struct
statement defines a new data type, with more than one member. The format of
the struct statement is as follows −

struct [structure tag] {

member definition;
member definition;
...
member definition;
} [one or more structure variables];

The structure tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At the
end of the structure's definition, before the final semicolon, you can specify one
or more structure variables but it is optional. Here is the way you would declare
the Book structure −
struct Books {

char title[50]; char


author[50]; char
subject[100]; int
book_id;
} book;

Program-:

#include <stdio.h>
#include <string.h>

struct student { char


name[50]; char
course[50];
char department[100];
int rollnumber;
int yoj;
};

int main( ) {

struct student s1;


struct student s2;

strcpy(s1.name, "Namrata");
strcpy( s1.course, "MBBS");
strcpy( s1.department, "Medical");
s1.rollnumber=1;
s1.yoj = 2016;
printf( "Name : %s\n",s1.name );
printf( "Course : %s\n",s1.course );
printf( "Department : %s\n",s1.department );
printf( "Roll Number : %d\n",s1.rollnumber );
printf( "Year of Joining : %d\n",s1.yoj );
strcpy(s2.name, "Sachin");
strcpy( s2.course, "B.TechCivil");
strcpy( s2.department, "Civil");
s2.rollnumber=2;
s2.yoj = 2016;

printf( "Name : %s\n",s2.name );


printf( "Course : %s\n",s2.course );
printf( "Department : %s\n",s2.department );
printf( "Roll Number : %d\n",s2.rollnumber );
printf( "Year of Joining : %d\n",s2.yoj );return 0;
}
Output-:

Concusion: Hence we have studied how to write a program to print a table of given
number.

Das könnte Ihnen auch gefallen