Beruflich Dokumente
Kultur Dokumente
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.
Area of a rectangle-:
#include<stdio.h>
{
Output-:
Theory-:
The if Statement
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.
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;
if (number%2==0)
{
else
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
Theory-:
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
{
statement(s);
}
#include <stdio.h>
int main()
{
int n, i;
return 0;
}
Output-:
Concusion: Hence we have studied how to write a program to print a table of given
number.
PR-4
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
#include<stdio.h>
main()
{
int year;
{
printf("The entered year is a leap year.");
}
else
{
}
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.
Theory-:
Decisions Using switch
#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.
Theory-:
C programming language allows multidimensional arrays. Here
is the general form of a multidimensional array declaration −
type name[size1][size2]...[sizeN];
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'.
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];
return 0;
}
Output-:
Concusion: Hence we have studied how to write a program to print a table of given
number.
PR -7
Theory-:
String Functions:
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);
2) strcpy()
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);
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";
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.
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.
#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-:
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)-:
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.
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 −
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 {
Program-:
#include <stdio.h>
#include <string.h>
int main( ) {
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;
Concusion: Hence we have studied how to write a program to print a table of given
number.