Computer Programming Basics in C
Computer Programming Basics in C
Computer Programming
COMPUTER PROGRAMMING
BEG175CO
Year: I Semester: I
* Continuous
* Duration: 3 hours
6. Control Statements
6.1 Loops: For, While, Do-While
6.2 Decisions: IF , IF ELSE, Nested IF…ELSE
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
9. Pointers
9.1 Fundamentals
9.2 Pointer Declarations
9.3 Passing Pointers to Functions
9.4 Relationship between Arrays & Pointers
9.5 Dynamic Memory Allocation
10. Structures and Unions
10.1 Defining a Structure, Arrays of Structures, Structures within Structures
10.2 Processing a Structure
10.3 Structures & Pointers
10.4 Passing Structures to Functions
10.5 Union & its importance
11. Data Files
11.1 Opening & Closing a Data File
11.2 Creating a Data File
11.3 Processing a Data File
12. Graphics
12.1 Initialization
12.2 Graphical mode
12.3 Simple program using built in graphical function.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Chapter-1
Problem Definition
Problem Analysis
- Input(s) to the problem, their form and the input media to be used,
- Output(s) expected from the problem, their form and the output media to be used
- Special constraints or conditions (if any)
- Any formulas or equations to be used
i.e. A = T/N
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Algorithm Development
Algorithm
A pseudocode is:
Flowcharting
Advantage of Flowcharting
- Communication
- Effective Analysis
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- Proper Documentation
- Efficient Coding
Flowcharts show all major parts of a program. A programmer can code the
programming instruction in a computer anguage more easily with a comprephensive
flowchart as a guide.
- Complex Logic
Flowchart Symbols
Flowcharta are usually drawn using standard symbols. Some standard symbols
frequently required for flowcharting many computer programs are given below,
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Coding
A written program may have errors, some errors can be detected by the language
compilers and some errors can not be identified by the compiler and occured during
the program run. Common types of errors are given below,
Logical Errors : Not identified by the compiler at compile time and identified
at the execution time. e.g. misuse of operators .
Run-time Errors : The errors exist during the execution time even after the
successfully compilation known as the Run-time Errors.
So testing is the process of checking the program for its correct functionality by
executing the program with some input data set and observing the output of the
program.
Documententions
From the start of the problem solving to the end of the implementation of
the program, all the tasks should be documented i.e. kept for future reference. It is
also the important part of the problem solving or program development.
Documentation may be two types
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Chapter 2
C is one of the most widely used programming languages of all time and there are
very few computer architectures for which a C compiler does not exist. C has greatly
influenced many other popular programming languages, most notably C++, which
began as an extension to C. The origin of C is closely tied to the development of the
UNIX operating system, originally implemented in assembly language on a PDP-7
by Ritchie and Thompson, incorporating several ideas from colleagues. Eventually
they decided to port the operating system to a PDP-11. B's inability to take
advantage of some of the PDP-11's features, notably byte addressability, led to the
development of an early version of C.
The original PDP-11 version of the UNIX system was developed in assembly
language. By 1973, with the addition of struct types, the C language had become
powerful enough that most of the UNIX kernel was rewritten in C. This was one of
the first operating system kernels implemented in a language other than assembly.
(Earlier instances include the Multics system (written in PL/I), and MCP (Master
Control Program) for the Burroughs B5000 written in ALGOL in 1961.) In 1978,
Brian Kernighan and Dennis Ritchie published the first edition of The C
Programming Language. This book, known to C programmers as "K&R", served for
many years as an informal specification of the language. The version of C that it
describes is commonly referred to as K&R C. The second edition of the book covers
the later ANSI C standard.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- C is robust language and has rich set of built-in functions, data types and
operators which can be used to write any complex program.
- Program written in C are efficient due to availability of several data types
and operators
- C has the capabilities of an assembly language (low level features) with the
feature of high level language so it is well suited for writing both system
software and application software.
- C is highly portable language i.e. code written in one machine can be moved
to other which is very important and powerful feature.
- C supports low level features like bit level programming and direct access to
memory using pointer which is very useful for managing resource
efficiently.
- C has high level constructs and it is more user friendly as its syntaxes
approaches to English like language.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
All C programs will consist of at least one function, but we can write a
C programs that have several functions body. The only function that has to be
present is the function main. For more advanced programs the main function will
act as a controling function calling other functions in their turn to do the dirty
work! The main function is the first function that is called when your program
executes. So main is the starting of execution of program.
- Documentation Section
This setion contains a set of comment lines giving the name of program,
the author, algrothms, methods used and other details. The documentation acts as a
communication medium between members of the development team when a number
of developers are working in the same project.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- Link Section
- Definiction Section
In this section, all symbolic constants are defined. Symbols play a crucial
role as they represent constants, variables, functions, and other entities in your code.
The variables which are used in more than one functions or blocks are
called global variables. These variables are defined or declared in this section.
This section is also declareds all the user-defined functions.
- Sub-program Section
This section contains all the user-defined functions that are called in the
main function. All the sections except the main() function section may ne absent
when they are not required.
- C Programming code:
When you first write the code in the C language, that source code is
sent to the processing section.
- Preprocessing
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- Compiler
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- Assembler
This part usually generates the Object code, after taking the assembly-level
code from the compiler. This object code is quite similar to the machine code or the
set of binary digits. After this assembler part, The Linker continues the process,
producing an [Link] file at the end.
- Linker
Before getting started with this, we should know that the library functions
are a part of the C software but not of any C program. Hence, the compiler has no
idea about the working of the function, whether it is a printf function or scanf
function. The information for each of these functions is kept in the corresponding
library, which the compiler ought to be able to connect. The linker does this task.
So, when the #include is written, it includes the stdio.h library, which is basically
used for giving access to the Standard Output and Input. The basic goal of the linker
is to link the object file to the library functions so that the programme may be run as
an executable file (.exe). In this Linker process, the [Link] file will be created and
this file is in an executable format. After this process, the next step is the loader
process.
- Loader
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Machine Language
Merits
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Demerits
Assembly Language
The assembly language comes above the machine language means that it has
less abstraction level from the hardware. The assembler language is used to convert the
assembly code into the machine code. The assembly language is written in simple
English language, so it is easily understandable by the user.
Merits
Demerits
Language Translator
Assembler
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Compiler
Interpreter
Programming Errors
Errors are the problems or the faults that occur in the program. The error
resulting from bad code program involved in producing the erroneous result. These
error are detected either during the time of compilation or execution. There are three
types of errors, such as
- Syntax Error
- Run-time Error
- Logic Error (Semantic Error)
Syntax Error
Syntax errors are also known as the compilation errors or format error as they
occurred at the compilation time. These errors are mainly occurred due to the mistakes
while typing or do not follow the syntax of the specified programming language.
Run-time Errors
The errors exist during the execution time even after the successfully
compilation known as the Run-time Errors. When the program is running, and it is not
able to perform the operation is the main cause of the run-time errors.
The logic error is an error that leads to an undesired output. These errors
produce the incorrect output. The occurrence of these errors mainly depends upon the
logic thinking of the developer.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Source code
Object code
Object code is the code produced by a compiler. Object code is often the same
as or similar to a computer’s machine language. The final step in producing an
executable program is to transform the object code into machine languages .
Introduction to C Programming
C is the one of the most powerful language. C is one of the oldest and finest
programming languages. C was developed by Dennis Ritchie in 1972. It has both the
properties of high-level language and low-level language. It is very powerful
programming language because it is used to prepare system software as well as
application software.
All C programs have to follow a basic structure. A C program starts with the
main function and executes instructions presents inside it. Each instruction terminated
with a semi-colon( ; ).
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Main Function
The main function is a part of every C program. We can represent the m,ain
function in various forms, such as
- main()
- int main()
- void main()
- main(void)
- void main(void)
- int main(void)
- int
- float
- char
int
int refers to the Integers data type. It is used to represent the integer number or
whole number but without fractions numbers. Its requires 4 bytes memory space and
identified by “ %d ”.
float
float refers to the floating data type. It is used to represents the real number’s
or fractional number’s. It requires 4 bytes memory space and identified by “ %f ”.
char
char refers to the character type. It is used to represent the single character,
symbol or multiple characters. It requires 1 bytes memory for the single character and
identified by “ %c “ or “ %s “.
Variables
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Constants
Keywords
All keyword have fixed meaning and these meaning cannot be changed. These
are reserved words whose meaning is already known to the compiler. Keywords in C
language cannot be used as a variable name. They are specifically used by the compiler
for its own purpose. There are 32 keywords available in C
do static if while
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Comments
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Conditional Operators
- Bitwise Operator
Arithmetic Operators
Arithmetic operators are the operators that are used for manipulating
arithmetic data and perform arithmetic operators. C programming language are
provided five basic arithmetic operators. They are
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Remainder a%b
Relational Operators
== Equal to a == b
< Less than a<b
> Greater than a>b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b
!= Not equal to a != b
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Logical Operators
Assignment Operators
= Assignment a=b
+= Add to a += b
-+ Subtracted from a -= b
*= Multiply by a *= b
/= Divided by a /= b
%= Modulo by a %= b
Unary Operators
Conditional Operators
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
consist of the question-mark (?) and the colon (;). The general format are given
below,
expression ? expression : expression ; ------- (x > b) ? a : b ;
Bitwise Operators
The bitwise operators are the operators used to perform the operations
on the data at the bit-level. When we perform the bitwise operations, then it is
also known as bit-level programming
Comma Operator
- Precedence
- Associativity
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
‘left - to - right’ or from ‘right - to - left’, depending on the level. This is known
as the associativity property of an operator.
Library Functions
Printf() Function
The printf() function is used to print the values on the screen (output). It
prints the given statement to the console. The syntax of printf() function is given
below,
printf(“ format_string “, agreement_list);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Scanf() Function
The scanf() function is used for receiving values (input). It reads the
input data from the console. The syntax of scanf() function is given below,
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“ Hello Wrold “);
getch();
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Control Statement
The flow of control jumps from one part of the program to another
depending on the calculations performed in the program. Program statements that
cause jumps are called control statement. The conditional statements of C
programming language are two categories,
- Selection Statements
- Looping Statements
- Unconditional Statements
Selection Statements
- If – statement ( )
- If – else statement ( : - All are the If - Statement )
- If – else - if statement ( )
- Switch statement ( Switch Statement are not If – Statement )
If – Statement
The computer to evaluate the expression first and then, depending on
whether the value of the expression ( relation or condition ) is ‘TRUE( non-zero )’
or ‘FALSE( zero )’, it transfer the control to a particular statement. The syntax of
the if – statement is given below,
If ( condition )
{
Statement ;
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is even number",n);
}
}
Nested if – Statement
if ( condition )
{
If ( condition )
{
// body
}
}
If – else Statement
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
#include<coni.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is even number",n);
}
else
{
printf("%d is odd number",n);
}
}
If – else – if statement
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
If ( condition )
{
Statement 1;
}
else if ( condition )
{
Statement 2;
}
else if ( condition )
{
Statement 3;
}
……………….
else
{
Statement n;
}
#include <stdio.h>
int main()
{
int num;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
if (num == 1)
{
printf(" Your number is 1\n");
}
else if (num == 2)
{
printf(" Your number is 2\n");
}
else if (num == 3)
{
printf(" Your number is 3\n");
}
else
{
printf(" Its not 1,2 or 3! ");
}
}
Switch statement
switch(expression)
{
case value 1:
Statement 1;
Break ;
case value 2:
Statement 2;
break;
------------------
default:
Statement n;
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
void main()
{
int Day;
printf(" Enter the day code\n");
scanf("%d", &Day);
switch (Day)
{
case 1:
printf(" First day is Sunday\n");
break;
case 2:
printf(" Second day is Monday\n");
break;
case 3:
printf(" Third day is Tuesday\n");
break;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
case 4:
printf(" Forth day is Wednesday\n");
break;
case 5:
printf(" Fifth day is Thruesday\n");
break;
case 6:
printf(" Sixth day is Friday\n");
break;
case 7:
printf(" Seventh day is Saturday\n");
break;
default:
printf(" Invalid Day \n");
}
getch();
}
Lopping Statement
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
while ( condition )
{
Statements ;
Increment / Decrement ;
}
#include <stdio.h>
void main()
{
int a;
printf("Enter the number ");
scanf("%d", &a);
while(a<=1000)
{
printf("%d\n",a);
a++;
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
do
{
Statements ;
Increment / Decrement ;
} while ( condition );
#include <stdio.h>
void main()
{
int i = 0;
do
{
printf(" The value of i is %d\n", i);
i++;
} while (i <= 10);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a program to print first n natural numbers by using "for loop", Increment
Operator.
#include<stdio.h>
void main()
{
int a,n;
printf(" Enter any number :");
scanf("%d", &n);
for(a=0;a<=n;a++)
{
printf(" The value of a is %d\n", a);
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
A loop which never terminates is called infinite loop that is it executes the
program statements repeatedly which doesn’t meet any ending point. It is also
called an indefinite loop or an endless loop. We can create an infinite loop
through various loop structure.
#include <stdio.h>
void main()
{
infinite
{
printf("hello");
}
getch();
}
Any number of the loops can be defined inside another loop structure.
So, loop inside another loop is called nested loop statement. The syntax of the
nested – loop statement is given below,
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
void main()
{
int outloop = 1;
while (outloop <= 5)
{
int inloop = 1;
while (inloop <= outloop)
{
printf(" %d", outloop);
inloop++;
}
outloop++;
printf(“ \n ”);
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Jumping Statement
- break Statement
- continue Statement
- goto statement
break Statement
break ;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a program to print the any number in increment order by using “ break
statement ”
#include<stdio.h>
void main()
{
int n,a;
printf(" Enter any number : ");
scanf("%d", &n);
for(a=1;a<=n;a++)
{
if(a==5)
break;
printf("%d", a);
}
}
Continue Statement
continue ;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void main()
{
int i=0,n;
printf(" Enter the any number ");
scanf(" %d", &n);
while(i<n)
{
i++;
if(i!=n)
{
continue;
}
else
{
printf("The value is %d", i);
}
}
}
goto Statement
level ;
goto level ;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a program to print the multiplication table of any number by using “goto
Statement”
#include <stdio.h>
void main()
{
int num,i;
printf("Enter the any number ");
scanf("%d",&num);
table:
printf("%d X %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Function
Advantage of Function
- Function increases code reusability,
- The program development will be faster,
- The program debugging will be easier,
- Large number of the programmer can be involved,
- The program can be developed in short period of time,
- The program can be developed in different places,
Function Prototype
Function Definition
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- Example of the function returning value and passing arguments is given below,
#include<stdio.h>
int sum(int a, int b); // Function prototype
void main() // Calling Function
{
int a,b,n;
printf(" Enter the first number : ");
scanf(" %d", &a);
printf(" Enter the second number : ");
scanf(" %d", &b);
n = sum(a,b); // Function Call
printf(" The sum of a and b is %d : ", n);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
An argument from the calling function to the called function but there is
no need of return statement in the called function.
- Example of the function returning no value but passing arguments is given below,
#include<stdio.h>
void sum(int a , int b);
void main()
{
int a,b;
printf(" Enter the first number : ");
scanf(" %d", &a);
printf(" Enter the second number : ");
scanf(" %d", &b);
sum(a,b);
}
- Example of the function returning value and passing arguments is given below,
#include<stdio.h>
int sum(int a, int b); // Function prototype
void main() // Calling Function
{
int n;
clrscr();
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- Example of the function returning value and passing arguments is given below,
#include<stdio.h>
void sum(); // Function prototype
void main() // Calling Function
{
sum(); // Function Call
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
The list of mostly used header files is given in the following table.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- // Write a program to calculate the sum of first 'n' natural number by using
"Recusive Function".
#include<stdio.h>
int sum( int n);
void main()
{
int n;
printf(" Enter the any number ");
scanf("%d",&n);
printf(" \n The sum of the given number is %d", sum(n));
}
#include<stdio.h>
int fibo ( int n);
void main()
{
int i,n;
printf(" Enter any number : ");
scanf("%d", &n);
for (i=1;i<=n;i++)
printf(" The fibo is %d\n", fibo(i));
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void average(int a, int b, int c);
void main()
{
int a,b,c;
printf(" Enetr the first number ");
scanf("%d",&a);
average(a,b,c);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Storage Classes
- Automatic Variable
- External Variable
- Static Variable
- Register Variable
Automatic Variable
- Write a program the variables are allocated in the run-time by using “ Automatic
Variable ”.
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic
variables a, b, and c.
return 0;
}
External Variable
The keyword extern is used to define external type variable. The external
storage class is used to transmit information across the blocks and functions.
When a variable is declared outside function storage is permanently assigned.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
int main()
{
extern int a;
printf("%d",a);
return 0;
}
Static Variable
The keyword static is used to define static type variable. A value is said to
be static if it is allocated storage at the beginning of the segment and the storage
remains allocated until the program execution terminates.
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%c %d %f %s",c,i,f,s); // The initial default value of c,i,f and s will
} will be printed
Register Variable
The keyword register is used to define register type variable. The use of
the register storage class is an attempt to improve execution speed. It is used
only for frequently used variables only ( that is limited either two or three ).
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Arrays
An array is defined as the collection of similar type of the data items stored
at contiguous memory locations. Arrays are the derived data type in C
Programming Language which can store the primitive type of data such as int,
char, double, float, etc. They are two types of array are given below,
- Each elements of an array is of the same data type and carries the same size,
- Elements of the array are stored at contiguous memory location where the first
element is stored at the smallest memory locations,
- Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of
the data element.
- Note : - It is very important to note that the array index starts with ‘o’.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
void main()
{
int a[] = {23, 45, 56, 78};
printf(" The value of a is %d\n ", a[0]);
printf(" The value of a is %d\n ", a[1]);
printf(" The value of a is %d\n ", a[2]); --------
printf(" The value of a is %d\n ", a[3]); // |
// |
float a[] = {23, 45, 56, 78}; // |>> int or float only one use
printf(" The value of a is %f\n ", a[0]); // |
printf(" The value of a is %f\n ", a[1]); // |
printf(" The value of a is %f\n ", a[2]); ---------
printf(" The value of a is %f\n ", a[3]);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a program to print the any number by using “ Arrays with Functions "
#include<stdio.h>
void main()
{
int arr[] = {1,43,644,7456,8545,6756,6786,3556,4556};
printArray(arr,9);
}
#include <stdio.h>
void main()
{
int n_students, n_subjects;
int Marks[n_students][n_subjects];
printf(" How many Students are : ");
scanf("%d", &n_students);
printf(" How many Subjects are : ");
scanf("%d", &n_subjects);
for (int i = 0; i < n_students; i++)
{
for (int j = 0; j < n_subjects; j++)
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
int n[4][3]; Here, 4 is the number of rows and 3 is the number of columns
#include<stdio.h>
void main()
{
int i=0, j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
// traversing 2D array
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
{
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}
}
}
Strings
- The initialization of the string is an array of the characters are given below,
char s[] = {'N', 'A', 'G', 'E', 'N', 'D', 'R', 'A', '\0'};
or
char s[] = “ NAGENDRA ”
Array of String
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char name[5][10], temp[10];
int i, j;
for (i = 0; i < 5; i++)
{
printf("\nEnter the name of student:");
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
scanf("%s", name[i]);
}
for (i = 0; i < (5 - 1); i++)
{
for (j = i + 1; j < 5; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("\n the name of student is:");
for (i = 0; i < 5; i++)
{
printf("\n%s", name[i]);
}
getch();
}
The gets() and puts() are declared in the header file stdio.h and both the
functions are involved in the input / output operations of the strings.
gets() Function
The gets() function enables the user to enter some characters followed by
the enter key. All the characters entered by the user get stored in a character array.
The null character is added to the array to make it a string. The gets() allows the
user to enter the space-separated strings.
char str[100];
// Source code
gets ( str );
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a program to print any milti-character supplied by the user by using "gets()
Strings function ".
#include <stdio.h>
void main()
{
char str[10];
printf(" Enter any Character : ");
gets(str); // Multi-Words string
{
printf("Your Name is : %s", str);
}
}
puts() Function
The puts() function is very much similar to printf() function. The puts()
function is used to print the string on the console which is previously read by using
gets() or scanf() function. The puts() function returns an integer value representing
the number of characters being printed on the console. Since, it prints an additional
newline character with the string, which moves the cursor to the new line on the
console. The syntax declaration of the puts() function is given below,
#include <stdio.h>
#include <string.h>
void main()
{
char name[50];
printf("Enter any character : ");
gets(name); // reads string from user
printf("Your character is: ");
puts(name); // displays string
getch();
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- There are many important string functions defined in ‘ string.h ’ library are given
below,
// Write a program to count the number of any character supplied by the user by
using "Strings".
#include<stdio.h>
#include<string.h>
void main()
{
char str[] = "Nagendra";
int a;
a = strlen(str);
printf(" The length of the string in str is : %d",a);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
This function is used to copy the content of second string into first string
passed to it.
/* write a program to copy the content of second strings into first string
then the print in Display by using "strings".*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[100] = "Nagendra";
char str2[100];
strcpy(str2, str1);
printf(" Now the str2 is %s", str1);
}
#include<stdio.h>
#include<string.h>
void main()
{
char str1[100] = "Nagendra";
char str2[100] = "Mukesh";
strcat(str1, str2);
printf("%s",str1,str2);
}
This function is used to compare two strings. It returns: 0 if strings are equal.
The negative value if first strings mismatching character's ASCII value is not
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
#include<string.h>
void main()
{
char str1[100] = "Nagendra";
char str2[100] = "Nagendra";
int val = strcmp(str1, str2);
printf("%d",val);
}
#include <stdio.h>
#include <string.h>
void main()
{
char str[20];
printf("Enter string: ");
gets(str); // reads string from console
printf("String is: %s", str);
printf("\nLower String is: %s", strlwr(str));
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
include <stdio.h>
#include <string.h>
void main()
{
char str[20];
printf("Enter string: ");
gets(str); // reads string from console
printf("String is: %s", str);
printf("\nUpper String is: %s", strupr(str));
}
The strstr() function returns pointer to the first occurrence of the matched
string in the given string. It is used to return substring from first match till the last
character.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "Our country name is Nepal. Our country is landlocked country.";
char *sub;
sub = strstr(str, "country");
printf("\nSubstring is: %s", sub);
return 0;
}
The function strrev() is used to reverse all characters in string except null
character at the end of string. The reverse of string “ abc ” is “ cba ” . The syntax
of the strrev() is given below,
Strrev ( string )
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Pointers
The ( & ) is the address operator, it represents the address of the variable and
the ( * ) operator is the value at address operator. It represents the value at the
specified address.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a program to print the integer address of a,b,c,d and the integer value of
a,b,c,d by using “ pointer ”.
#include <stdio.h>
void main()
{
int a;
int *b = &a;
int **c = &b;
int ***d = &c;
Call by values
The values of the variables are passed. In this, values of the variables are not
affected by changing the value of the formal parameter. When an argument is passed
by the value, the data item is copied to the function.
#include <stdio.h>
void swap(int a, int b);
void main()
{
int a = 10;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
int b = 12;
swap(a, b); // call by values
printf(" The after swapping the value of a and b is %d %d", a, b);
}
Call by reference
The address of the variable is passed. The contents of that address can be
accessed freely, either within the function or within the calling routine. The use of
painter as a function argument permits the corresponding data item to be altered
globally from within the function.
#include <stdio.h>
void swap(int *a, int *b);
void main()
{
int a = 10;
int b = 12;
swap(&a, &b); // call by values
printf(" The after awapping the value of a and b is %d %d", a, b);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
NULL Pointer
Array of Pointers
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
program and allows the program to utilize the memory. When the allocated memory
is no more in use, it returns the allocated memory to the system. The deallocated
memory now is free and any other program can use it. Dynamic memory allocation
in c language is possible by 4 functions of stdlib.h header file.
- malloc() Function
- calloc() Function
- realloc() Function
realloc() is the standard library function used to modify the size of memory
blocks that were previously allocated using malloc() or calloc(). realloc() function
returns void pointer. If calloc() function unable to allocate memory due to any reason
it returns NULL pointer. The syntax of the realloc() is given below,
- free() Function
free(ptr);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Structure
struct employee
{
int Id;
float Salary; this declares a new user-defined datatype
char Name[50];
};
( Semi-colon is important )
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Array of structure
// Write a program to store the information of five (5) students ant print them.
#include <stdio.h>
#include <string.h>
struct student
{
int rollno;
char name[10];
};
void main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for (i = 0; i < 5; i++)
{
printf("\nEnter Rollno:");
scanf("%d", &st[i].rollno);
printf("\nEnter Name:");
scanf("%s", &st[i].name);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Structure in memory
The actual memory size of a variable in terms of bytes may vary from machine
to machine. Therefore, the sizeof operator helps us to determine memory size of a
variable.
/* Write a program that takes Id, Name, Salary, Address and supplied by the user
then the print on screen by using "Pointer to structure". */
#include <stdio.h>
#include <string.h>
struct employee
{
int Id;
char Name[100];
float Salary;
char Address[100];
};
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
void main()
{
struct employee Emp1;
struct employee *ptr;
ptr = &Emp1;
(*ptr).Id;
(*ptr).Name; Pointer operator
(*ptr).Salary;
(*ptr).Address;
// ptr->Id;
// ptr->Name; Arrow Operator
// ptr->Salary;
// ptr->Address;
printf("\n****************************************************\n\n");
printf("\n\n******************************************************");
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
/* Write a program that takes Id, Name, Salary, Address and supplied by the user
then the print on screen by using "Passing structure of a Function". */
#include <stdio.h>
#include <string.h>
struct employee
{
int Id;
char Name[100];
float Salary;
char Address[100];
};
void main()
{
struct employee Emp1;
struct employee *ptr;
ptr = &Emp1;
// (*ptr).Id;
// (*ptr).Name;
// (*ptr).Salary;
// (*ptr).Address;
ptr->Id;
ptr->Name;
ptr->Salary;
ptr->Address;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
show(Emp1);
}
Typedef keyword
The typedef keyword allows us to define new data types that are equivalent to
existing data types. This feature also can be implemented to structure to define user
define data types.
/* Write a program that takes Id, Name, Salary, Address and supplied by the user
then the print on screen by using "Typedef Structure". */
#include <stdio.h>
#include <string.h>
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
void main()
{
Emp Emp1;
Emp *ptr;
ptr = &Emp1;
ptr->Id;
ptr->Name;
ptr->Salary;
ptr->Address;
show(Emp1);
}
// Wriet a program that takes Roll_No, Name, Address and Date of Birth and
output the information on screen.
#include <stdio.h>
#include <string.h>
struct date
{
int year;
int month;
int day;
};
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
struct student
{
int Roll_No;
char Name[100];
char Address[100];
struct date d;
};
void main()
{
struct student a;
printf(" Enter The Roll No. : ");
scanf("%d", &a.Roll_No);
printf(" Enter the Name : ");
scanf("%s", &[Link]);
printf(" ENter the Address : ");
scanf("%s", &[Link]);
printf(" Enter the Date of Birth YYYY/MM/DD : ");
scanf("%d %d %d", &[Link], &[Link], &[Link]);
printf("\n****************************************************\n\n");
printf("Id = %d\n", a.Roll_No);
printf("Name = %s\n", [Link]);
printf("Address = %s\n", [Link]);
printf("Date of Birth = %d/%d/%d", [Link], [Link], [Link]);
printf("\n\n**************************************************\n\n");
}
Union
union tag_name
{
data_type member1;
data_type member2;
................
data_type membern;
};
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Importance of Union
- Unions allow you to store different data types in the same memory location, saving
memory by sharing the space among members.
- Unions provide flexibility in handling multiple data types within the same memory
space, making it easier to represent dynamic or polymorphic data.
- Unions are useful for bit-level operations and manipulations, allowing access to
individual bits within a byte or a word.
- Unions are employed in scenarios where a single memory location needs to
represent different aspects of data efficiently.
- Unions can simplify the representation of complex data structures by allowing the
use of a single memory location for different types of data.
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
The data is sequentially accessed from/tpo data filr. It is very time consuming
technique. If we need to access the last record of a file, then we need to access all
the records before that record.
The data is randomly accessed anywhere from/to data file. It is more faster
technique than the previous one. We can read and write data in particular part of a
file with the help of the function ftell(), fseek() and rewind().
- ftell()
It takes file pointer as parameter and returns lng data type that represent the
current position of the file in terms of bytes.
n = ftell(fp);
- rewind()
It takes file pointer as parameter and reset the position to the start of the file.
rewind(fp);
- fseek()
It helps to move the file position to the desire location within the file. It takes
three parameter fp file pointer, offset position of the file in terms of bytes and
position specifies three values: 0 – beginning, 1-current position and 2 end of the
file.
Fseek(fp, offset, position);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
We must open a file before it can be read, write, or update. The fopen()
function is used to open a file. The syntax of the fopen() is given below,
- The use one of the following modes in fopen() function is given below,
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
fputc() Fuctions
The fputc() function is used to write a single character into file. It outputs
a character to a stream. The syntax of the fputc() function is given below,
#include <stdio.h>
void main()
{
FILE *fp;
fp = fopen("[Link]", "w"); // opening file
fputc('a', fp); // writing single character into file
fclose(fp); // closing file
}
fgetc() Function
The fgetc() function returns a single character from the file. It gets a
character from the stream. It returns EOF at the end of file. The syntax of the
fgetc() function is given below,
#include<stdio.h>
void main()
{
FILE *ptr;
char ch;
ptr = fopen("[Link]","r");
ch = fgetc(ptr);
while(ch!=EOF)
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
printf("%c",ch);
ch = fgetc(ptr);
}
}
fprintf() Function
The fprintf() function is used to write set of characters into file. It sends
formatted output to a stream. The syntax of the fprintf() is given below,
#include <stdio.h>
void main()
{
FILE *ptr;
int Roll_No;
char Name[100];
float per;
int i = 1;
ptr = fopen("[Link]", " w");
do
{
printf(" Enter the Roll_No : ");
scanf(" %d", &Roll_No);
printf(" ENter the Name : ");
scanf(" %s", &Name);
printf(" Enter the percentage : ");
scanf(" %f", &per);
fprintf(ptr, "\n %d\t %s\t %f", Roll_No, Name, per);
i++;
} while (i <= 5);
fclose(ptr);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
fscanf() Function
The fscanf() function is used to read set of characters from file. It reads a
word from the file and returns EOF at the end of file. The syntax of the fscanf()
function is given below,
// Erite a program to read Roll_No, Name and Percentage of 5 students from the
file .
#include <stdio.h>
void main()
{
FILE *ptr;
int Roll_No;
char Name[100];
float per;
int i = 1;
ptr = fopen("[Link]", " r");
printf(" \n Data from the file\n");
while ((fscanf(ptr, " %d\t %s\t %f ", &Roll_No, Name, &per)) != EOF)
printf(" \n %d\t %s\t %f", Roll_No, Name, per);
fclose(ptr);
}
- foef()
C feof() function is used to determine if the end of the file (stream) specified
has been reached or not. This function keeps on searching the end of the file (EOF)
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
in your file program. This tutorial guides you on how to use the feof() function in the
C program. The syntax of the feof() function is given below,
- ferror()
The ferror() function check for an error in reading from or writing to the given
[Link] an error occurs, the error indicator remains set until you close stream,
call the clearerr() function. The syntax of the ferror() function is given below,
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- // Find the volume of the rectangle by using inputs supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int l,b,h,V;
printf("Enter the lentgh of the rectangular");
scanf("%d",&l);
printf("Enter the breadth of the rectangular");
scanf("%d",&b);
printf("Enter the height of the rectangular");
scanf("%d",&h);
V=l*b*h;
printf("\n Volume is=%d", V);
getch();
}
#include <stdio.h>
int main()
{
int a = 16;
int d = 8;
float b = 8.5;
char c = 'N';
printf("\nThe value of a is %d", a);
printf("\nThe value of b is %f", b);
printf("\nThe value of c is %c", c);
printf("\nThe value of d is %d", d);
printf("\nThe sum of a and b is %f", a + b);
printf("\nThe substract of a and d is %d", a - d);
printf("\nThe Remainder of a and d is %d", a % d);
return 0;
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- // Find the sum of two number by using inputs supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s;
printf("\nEnter the first number= ");
scanf("%d",&a);
printf("\nEnter the first number= ");
scanf("%d",&b);
s=a+b;
printf(" The sum of a and b is %d",s);
getch();
}
- // Find the area of the rectangle by using inputs supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int l,b,a;
printf("\n Enter the length of the rectangle ");
scanf("%d",&l);
printf("\n Enter the breadth of the rectangle ");
scanf("%d",&b);
a=l*b;
printf("The area of the rectangle is %d",a);
getch();
}
- // Find the area of the circle by using inputs supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int radius, Area;
float pi = 3.14;
printf("Enter the radius of the circle ");
scanf("%d", &radius);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Area = pi*radius*radius;
printf(" The area of the circle is %d", Area);
getch();
}
- // Find the volume of the cylinder by using inputs supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int radius,height, Volume;
float pi = 3.14;
printf("Enter the radius of the cylinder ");
scanf("%d", &radius);
printf("Enter the height of the cylinder ");
scanf("%d", &height);
Volume = pi*radius*radius*height;
printf(" The Volume of the circle is %d", Volume);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
float C,F;
printf(" Enter the celsius temperature ");
scanf("%f",&C);
F = ((C*(9.0/5.0))+32);
printf(" The value of the celsius into fahrenheit temperature is %f", F);
getch();
}
- // Write a prgram to find the value of the SimpleInterest by using inputs supplied
by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
int principal,time,rate,SimpleInterest;
printf(" Enter the principal value ");
scanf("%d", &principal);
printf(" Enter the time ");
scanf("%d", &time);
printf(" Enter the rate value ");
scanf("%d", &rate);
SimpleInterest = (principal*time*rate)/100;
printf(" The value of the SimpleInterest is %d", SimpleInterest);
getch();
}
- // Write a program to find the value of equation by using inputs supplied by the
user.
#include <stdio.h>
#include <stdio.h>
int main()
{
int x, y, z, k, V;
printf(" Enter the value of x = ");
scanf("%d", &x);
printf(" Enter the value of y = ");
scanf("%d", &y);
printf(" Enter the value of z = ");
scanf("%d", &z);
printf(" Enter the value of k = ");
scanf("%d", &k);
V = (((3 * x) / y) - z + k);
printf(" The value of V is %d", V);
return 0;
}
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
printf(" Enter the any number ");
scanf("%d",&a);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Conditional Instructions
- // Write a program to check the number is odd and even by using inputs supplied
by the user by using "If-else statement".
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
printf(" Enter the any number ");
scanf("%d", &a);
if(a%2==0)
{
printf("%d is an Even number", a);
}
else
{
printf("%d is a Odd number", a);
}
getch();
}
- /* Write a program to check your age and you can drive or not ?
By using "If - else Statement" */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(" Enter the Age ");
scanf("%d", &n);
if(n<=70 && n>=18)
{
printf(" You are above 18 and below 70, You can drive\n");
}
else
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
printf("You are below 18 and above 70, You cannot drive \n");
}
if(n==50)
{
printf(" half Century\n");
}
getch();
}
- /* Write a program to check your age and you can drive or not ?
By using "If - else statement" */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int Emergency = 0;
Emergency= 1;
printf(" Enter the Age ");
scanf("%d", &n);
if(n<=70 && n>=18 || Emergency==1)
{
printf(" You are above 18 and below 70, You can drive\n");
}
else
{
printf("You are below 18 and above 70, You cannot drive \n");
}
if(n==50)
{
printf(" half Century\n");
}
#include <stdio.h>
int main()
{
int num;
printf(" Enter the any number");
scanf("%d", &num);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
if (num == 1)
{
printf(" Your number is 1\n");
}
else if (num == 2)
{
printf(" Your number is 2\n");
}
else if (num == 3)
{
printf(" Your number is 3\n");
}
else
{
printf(" Its not 1,2 or 3! ");
}
return 0;
}
- // Write a program to check the any number is greater than or Less than 5
#include <stdio.h>
int main()
{
int num;
printf(" Enter the number ");
scanf("%d", &num);
(num < 5) ? printf(" Number is less than 5 ") : printf(" Number is greater than
5 ");
return 0;
}
- // Write a program to enter the code and print the day of week.
#include <stdio.h>
int main()
{
int Day;
printf(" Enter the day code\n");
scanf("%d", &Day);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
switch (Day)
{
case 1:
printf(" First day is Sunday\n");
break;
case 2:
printf(" Second day is Monday\n");
break;
case 3:
printf(" Third day is Tuesday\n");
break;
case 4:
printf(" Forth day is Wednesday\n");
break;
case 5:
printf(" Fifth day is Thruesday\n");
break;
case 6:
printf(" Sixth day is Friday\n");
break;
case 7:
printf(" Seventh day is Saturday\n");
break;
default:
printf(" Invalid Day \n");
}
return 0;
}
- // Write a program to enter the percentage and print the your grade.
#include <stdio.h>
int main()
{
int Percentage;
printf(" Enter your percentage:\n");
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
scanf("%d", &Percentage);
- // Write a program to enter the six (6) subject marks then check you are failed or
passed ?
#include <stdio.h>
int main()
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
{
int Physics, Chemistry, Math, Computer, English, Nepali;
float total;
printf("Enter the Physics Marks \n");
scanf("%d", &Physics);
printf("Enter the Chemistry Marks \n");
scanf("%d", &Chemistry);
printf(" Enter the Math Marks \n");
scanf("%d", &Math);
printf("Enter the Computer Marks\n");
scanf("%d", &Computer);
printf(" Enter the English Marks \n");
scanf("%d", &English);
printf("Enter the Nepali Marks\n");
scanf("%d", &Nepali);
total = ((Physics + Chemistry + Math + Computer + English + Nepali) / 6);
if ((total < 35) || Physics < 35 || Chemistry < 35 || Math < 35 || Computer < 35
|| English < 35 || Nepali < 35)
{
printf("Total percentage is %f and you are Failed\n", total);
}
else
{
printf("Total percentage is %f and you are Passed\n ", total);
}
return 0;
}
- // Calculate income tax paid by an employee to the goverment as per the salary
mention below,
#include <stdio.h>
int main()
{
float Tax = 0, Income;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- // Write a program to find the Greatest of four numbers entered by the user
#include<stdio.h>
void main()
{
int a,b,c,d;
printf(" Enter( the first number\n");
scanf("%d", &a);
printf(" Enter( the second number\n");
scanf("%d", &b);
printf(" Enter( the third number\n");
scanf("%d", &c);
printf(" Enter( the forth number\n");
scanf("%d", &d);
if(a>b && a>d)
{
printf(" %d is the Greatest Number ", a);
}
else if(b>c && b>d )
{
printf(" %d is the Greatest Number ", b);
}
else if(c>a && c>d)
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
void main()
{
// 97-122 = a-z ( LowerCase )
char ch;
printf(" Enter the Character \n");
scanf("%c", &ch);
#include <stdio.h>
void main()
{
int a;
printf("Enter the number ");
scanf("%d", &a);
while(a<=1000)
{
printf("%d\n",a);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
a++;
}
}
#include <stdio.h>
void main()
{
int i = 0;
while (i <= 50)
{
if (i >= 10)
{
printf(" The value of i is %d\n", i);
}
i++;
}
}
#include <stdio.h>
void main()
{
int i = 0;
do
{
printf(" The value of i is %d\n", i);
i++;
} while (i <= 10);
}
#include <stdio.h>
void main()
{
int i = 0;
int n;
printf(" Enter any number: ");
scanf("%d", &n);
do
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
{
printf(" The value of i is %d\n", i+1);
i++;
} while (i < n);
}
- // Write a program to print first n natural numbers by using "for loop", Increment
Operator.
#include<stdio.h>
void main()
{
int a,n;
printf(" Enter any number :");
scanf("%d", &n);
for(a=0;a<=n;a++)
{
printf(" The value of a is %d\n", a);
}
}
#include<stdio.h>
void main()
{
int a,n;
printf(" Enter the any number :");
scanf("%d", &n);
for(a=n;a;a--)
{
printf("The value of a is %d\n", a);
}
}
#include<stdio.h>
void main()
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
int i=0,n;
printf(" Enter the any number ");
scanf(" %d", &n);
while(i<n)
{
i++;
if(i!=n)
{
continue;
}
else
{
printf("The value is %d", i);
}
}
}
#include <stdio.h>
void main()
{
int i,n;
printf(" Enter the any number ");
scanf("%d", &n);
for(i=1;i<=10;i++)
{
printf("%d X %d = %d\n",n,i,10*i);
}
}
- // write a program to sum of first ten(10) natural numberby using "While Loop".
#include <stdio.h>
void main()
{
int n=0,sum,a;
printf("How many numbers are added ? : ");
scanf("%d", &a);
while (n <= a)
{
sum +=n;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
n++;
}
printf(" The sum of total number is %d\n", sum);
}
- // write a program to sum of first ten(10) natural numberby using "For Loop".
#include<stdio.h>
void main()
{
int sum=0,a,n;
printf(" How many numbers is added : ");
scanf("%d",&n);
for(a=1;a<=n;a++)
{
sum +=a;
}
printf(" The sum of the total number is %d", sum);
}
#include <stdio.h>
void main()
{
int a = 0, n, sum;
printf(" How many number is added ? ");
scanf("%d", &n);
do
{
sum += a;
a++;
} while (a <= n);
{
printf(" The sum of the total number is = %d", sum);
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void main()
{
int a=0,n,factorial=1;
printf(" Enter the any number ");
scanf(" %d",&n);
for(a=1;a<=n;a++)
{
factorial = factorial * a;
}
{
printf(" The Factorial is given by the number %d is %d\n", n,factorial);
}
}
#include <stdio.h>
void main()
{
int a = 1, n, factorial=1;
printf(" Enter any number ");
scanf("%d", &n);
while (a <= n)
{
factorial = factorial * a;
a++;
}
{
printf(" The Factorial is given by the number %d is %d\n", n,factorial);
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a program to check the wheather the given number is rime or not by
using "Loops"
#include<stdio.h>
void main()
{
int a,n;
printf(" Enter the any number ");
scanf("%d", &n);
for(a=2;a<=n;a++)
{
if(n%a==0)
break;
}
if(a==n)
{
printf(" This is a Prime number ");
}
else
{
printf(" This is not a prime number ");
}
}
Function
#include<stdio.h>
void display(); // Function Prototype
void main()
{
int a;
printf(" Initializing dispaly function \n");
display(); // Function Call
printf(" Display function finished its work.\n");
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void goodmorning();
void goodafternoon();
void goodnight();
void main()
{
goodmorning();
goodafternoon();
goodnight();
}
void goodmorning()
{
printf(" Good Morning Nagendra\n");
}
void goodafternoon()
{
printf(" Good AfterNoon Nagendra\n");
}
void goodnight()
{
printf(" Good Night Nagendra\n");
}
#include <stdio.h>
#include <stdio.h>
void goodmorning();
void goodafternoon();
void goodnight();
void main()
{
goodmorning(); // Only one function call
}
void goodmorning()
{
printf(" Good Morning Nagendra\n");
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
goodafternoon();
}
void goodafternoon()
{
printf(" Good AfterNoon Nagendra\n");
goodnight();
}
void goodnight()
{
printf(" Good Night Nagendra\n");
}
#include<stdio.h>
int sum(int a,int b);
void main()
{
int a,b,s;
printf("Enter the first number\n");
scanf("%d",&a);
printf("Enter the second number\n");
scanf("%d",&b);
sum(a,b);
}
- // write a program to check the value of before change and after change by using
"Function"
#include<stdio.h>
void change(int b);
void main()
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
int a = 300;
printf("The value f a before change is %d\n",a);
change(a);
printf("The value f a after change is %d\n",a);
}
void change(int a)
{
a = 77;
}
#include<stdio.h>
void area(int a);
void main()
{
int a;
printf(" Enter the side");
scanf(" %d",&a);
area(a);
}
void area(int a)
{
int area;
area = a*a;
printf(" The area of the square is %d",area);
}
Recursive Function
#include <stdio.h>
int factorial(int a);
void main()
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
int a;
printf(" Enter the any number ");
scanf("%d", &a);
printf(" The value of the given factorial number is %d", factorial(a));
}
int factorial(int a)
{
if (a == 0 || a == 1)
{
return 1;
}
else
{
return a * factorial(a - 1);
}
}
#include<stdio.h>
void average(int a, int b, int c);
void main()
{
int a,b,c;
printf(" Enetr the first number ");
scanf("%d",&a);
printf(" Enter the second number ");
scanf("%d",&b);
printf(" Enter the third number");
scanf("%d",&c);
average(a,b,c);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
void temperature(int c);
void main()
{
float c;
printf(" Enter the celcius temperature value ");
scanf("%f", &c);
temperature(c);
}
void temperature(int c)
{
float temperature;
temperature = ((c * (9.0/5.0)) + 32);
printf(" The value of the farenheit temperature is %f",temperature);
}
#include <stdio.h>
float force(float mass, float acceleration);
void main()
{
float mass, acceleration;
printf(" Enter the mass of the body ");
scanf("%f", &mass);
printf(" Enter the acceleration due to Gravity ");
scanf("%f", &acceleration);
force(mass, acceleration);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
int fibo ( int n);
void main()
{
int i,n;
printf(" Enter any number : ");
scanf("%d", &n);
for (i=1;i<=n;i++)
printf(" The fibo is %d\n", fibo(i));
}
- // Write a program to calculate the sum of first 'n' natural number by using
"Recusive Function".
#include<stdio.h>
int sum( int n);
void main()
{
int n;
printf(" Enter the any number ");
scanf("%d",&n);
printf(" \n The sum of the given number is %d", sum(n));
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void printpattern(int n);
void main()
{
int n;
printf(" Enter any number ");
scanf("%d",&n);
printpattern(n);
}
void printpattern(int n)
{
int a;
if(n == 1)
{
printf("*\n");
return;
}
printpattern(n-1);
for(a=0;a<(2*n-1);a++)
{
printf("*");
}
printf("\n");
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Pointers
- /* Write a program to print the integer address of a, b, c, d and the integer value of
a, b, c, d by using “ pointer ” */.
#include <stdio.h>
void main()
{
int a;
int *b = &a;
int **c = &b;
int ***d = &c;
printf(" Enter any number ");
scanf("%d", &a);
printf("\n The value of A is %d\n", a);
printf(" The value of A is %d\n", *b);
printf(" The address of A is %u\n", &a);
printf(" The address of A is %u\n", b);
printf(" The value of B is %u\n", *(&b));
printf(" The address of B %u\n", &b);
printf(" The value of A is %u\n", *(&a));
printf("The address of Pointer B is %u\n", &c);
printf("The address of Pointer C is %u\n", &d);
printf(" The value of pointer to pointer C is %d", **c);
}
#include<stdio.h>
void main()
{
int a,b,*x,*y,sum;
printf(" Enter the first number ");
scanf("%d",&a);
printf(" Enter the second number ");
scanf("%d",&b);
x = &a;
y = &b;
sum = *x+*y;
printf(" The sum of the value of x and y is %d",sum);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void main()
{
int a;
int *b = &a;
printf(" The address of variable A is %u\n",&a);
printf(" The address of variable B is %u\n",&b);
}
- /* Write a program having a variable "i" then the print the address of "i", Pass this
variable to a function and print the address That will be the address are same?. */
#include<stdio.h>
void address( int a)
{
printf(" The address of A is %u\n",&a);
}
void main()
{
int i;
address(i);
printf(" The address of i is %u",&i);
}
- /* Write a program to calculate the sum and average of any two number by using
"function", then the use of pointer and print the values of sum and average in main */
#include <stdio.h>
void SumandAvg(int a, int b, int *sum, float *avg)
{
*sum = a + b;
*avg = (float)*sum / 2;
}
void main()
{
int m, n, sum;
float avg;
printf(" Enter the first number ");
scanf("%d", &m);
printf(" Enter the second number ");
scanf("%d", &n);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Arrays
- // Write a program to print the five (5) subjects Marks for a student by using Arrays.
#include<stdio.h>
void main()
{
int Marks[5];
printf(" Enter the Mark of Physics : ");
scanf("%d", &Marks[0]);
printf(" Enter the Mark of Chemistry : ");
scanf("%d", &Marks[1]);
printf(" Enter the Mark of Math : ");
scanf("%d", &Marks[2]);
printf(" Enter the Mark of Computer : ");
scanf("%d", &Marks[3]);
printf(" Enter the Mark of English : ");
scanf("%d", &Marks[4]);
printf(" The value of All Subjects are :\n\n %d\n %d\n %d\n %d\n %d\n
",Marks[0],Marks[1],Marks[2],Marks[3],Marks[4]);
}
- // Write a program to print the Marks for five students by using " Arrays "
#include<stdio.h>
void main()
{
int Marks[5];
for(int i=0;i<5;i++)
{
printf(" Enter the value of the Marks for Students %d : ", i+1);
scanf(" %d",&Marks[i]);
}
for(int i=0;i<5;i++)
{
printf("The value of the Marks for Students %d is : %d\n", i+1,Marks[i]);
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
void main()
{
int a[] = {23, 45, 56, 78};
printf(" The value of a is %d\n ", a[0]);
printf(" The value of a is %d\n ", a[1]);
printf(" The value of a is %d\n ", a[2]); // --------
printf(" The value of a is %d\n ", a[3]); // |
// |
float a[] = {23, 45, 56, 78}; // | >>>> int or float only one use
printf(" The value of a is %f\n ", a[0]); // |
printf(" The value of a is %f\n ", a[1]); // |
printf(" The value of a is %f\n ", a[2]); // --------
printf(" The value of a is %f\n ", a[3]);
}
#include<stdio.h>
void main()
{
int a ;
int*b = &a;
printf(" Enter the any number ");
scanf("%d", &a);
printf(" The address of b is %u\n", b);
b++;
b++;
printf(" The address of b is %u\n", b);
}
- // Write a program to print the Marks of the Students by using "Arrays with
Pointers"
#include <stdio.h>
void main()
{
int Marks[5];
int *ptr;
ptr = &Marks[0];
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void main()
{
int arr[10];
// int *ptr = &arr[0];
int *ptr = arr;
ptr = ptr+2;
if (ptr==&arr[2])
{
printf(" These Point to the same Locatin in Memory\n ");
}
else
{
printf(" These do not Point to the same Locatin in Memory\n ");
}
}
#include<stdio.h>
void main()
{
int mul[10];
for( int i=0;i<10;i++)
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
mul[i] = 5*(i+1);
}
#include<stdio.h>
void main()
{
int mul[10],n;
printf("Enter the any number ");
scanf("%d",&n);
for( int i=0;i<10;i++)
{
mul[i] = n*(i+1);
}
- // Write a program containing a function which reverse the array passed to it.
#include<stdio.h>
void reverse(int *arr, int n)
{
int temp;
for(int i=0;i<(n/2);i++)
{
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
void main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
reverse(arr,10);
for(int i=0;i<10;i++)
{
printf(" The value of %d elements is : %d\n", i, arr[i]);
}
}
#include <stdio.h>
void main()
{
int mulTable[3][10];
{
for (int i = 0; i < 10; i++)
{
mulTable[0][i] = 2 * (i + 1);
}
for (int i = 0; i < 10; i++)
{
printf(" 2 X %d = %d\n", i + 1, mulTable[0][i]);
}
printf(" ************************************************\n\n");
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- /* Create a three - Dimensional arrays and print the address of its elements in
increasing order. */
#include <stdio.h>
void main()
{
int arr[2][3][4];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 4; k++)
{
printf(" The address of arr[%d][%d][%d] is : %u\n", i, j, k, &arr[i][j][k]);
}
}
}
}
Strings
- // Create a strings using " " and print its contant using a loop.
#include<stdio.h>
void main()
{
char str[] = "Nagendra"; or // char str[] = {'N','a','g','e','n','d','r','a','\0'};
char *ptr = str;
while(*ptr!='\0')
{
printf(" %c",*ptr);
ptr++;
}
}
- // Write a program to print any character ( single word ) supplied by the user by
using "Strings".
#include <stdio.h>
void main()
{
char str[10];
printf(" Enter any Character : ");
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
scanf("%s", str);
{
printf("Your Name is : %s", str);
}
}
- // Write a program to take string as an input from the user using %c and %s.
Conform that the strings are equal.
#include<stdio.h>
void main()
{
char str1[100];
char str2[100];
printf(" Enter first strings ");
scanf("%s", str1);
printf(" Enter second strings ");
scanf("%c", str2);
printf("The first string is %s",str1);
printf("The first string is %c",str1);
}
#include<stdio.h>
#include<string.h>
int strlen(char *str)
{
char *ptr = str;
int len = 0;
while(*ptr!='\0')
{
len++;
ptr++;
}
return len;
}
void main()
{
char str[] = "Nagendra";
int l = strlen(str);
printf(" The length of this strings is %d",l);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- /* Write a function slice() to slice a string . It should change the original string
such that it is now the slice string .Take m and n as the start and ending
position for slice.*/
#include<stdio.h>
void slice(char *str,int m, int n)
{
int i = 0;
while((m+i)<n)
{
str[i] = str[i+m];
i++;
}
str[i] = '\0';
}
void main()
{
char str[] = "Nagendra";
slice(str,1,8);
printf("%s",str);
}
#include <stdio.h>
void encrypt(char *c)
{
char *ptr = c;
while (*ptr != '\0')
{
*ptr = *ptr + 1;
ptr++;
}
}
void main()
{
char c[] = "Nagendra";
encrypt(c);
printf("Encrypted string is : %s", c);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
void decrypt(char *c)
{
char *ptr = c;
while (*ptr != '\0')
{
*ptr = *ptr - 1;
ptr++;
}
}
void main()
{
char c[] = "Obhfoesb"; // Obhfoesb is the output of the question number 85.c
decrypt(c);
printf("Decrypted string is : %s", c);
}
#include<stdio.h>
int occurance(char str[], char c)
{
char *ptr = str;
int count = 0;
while (*ptr!='\0')
{
if(*ptr==c)
{
count++;
}
ptr++;
}
return count;
}
void main()
{
char str[] = "Nagendra";
int count = occurance(str,'a');
printf(" The occurance is : %d", count);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
Structure
- // Write a program that takes Id, Salary, Name and print the record by using
"Structure ".
#include<stdio.h>
struct employee
{
int Id;
float Salary;
char Name[50];
};
void main()
{
struct employee a;
printf("Enter the Id : ");
scanf("%d",&[Link]);
printf("Enter the Salary : ");
scanf("%f",&[Link]);
printf("Enter the Name : ");
scanf("%s",[Link]);
printf("Id = %d\n Salary = %.3f\n Name = %s\n ", [Link],[Link],[Link]);
}
- // Write a program to store the details of three employees from user defined data
by using "Structure".
#include <stdio.h>
struct employee
{
int Id[10];
char Name[100];
float Salary[50];
};
void main()
{
struct employee Emp1, Emp2, Emp3;
printf("Enter the Id of Emp1 : ");
scanf("%d", &[Link]);
printf("Enter the Name of Emp1 : ");
scanf("%s", &[Link]);
printf("Enter the Salary of Emp1 : ");
scanf("%f", [Link]);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
struct employee
{
int Id;
char Name[100];
float Salary;
};
void main()
{
struct employee Company = {100, "Nagendra", 500000};
#include <stdio.h>
struct vector
{
int x;
int y;
};
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
void main()
{
struct vector v1, v2;
printf("Enter the value of v1.x : ");
scanf("%d", &v1.x);
printf("Enter the value of v1.y : ");
scanf("%d", &v1.y);
printf("Enter the value of v2.x : ");
scanf("%d", &v2.x);
printf("Enter the value of v2.y : ");
scanf("%d", &v2.y);
printf(" X dimensional is : %d\n Y dimensional is : %d\n", v1.x, v1.y);
printf(" X dimensional is : %d\n Y dimensional is : %d\n", v2.x, v2.y);
}
- // Write a function sumvector which returns the sum of two vectors to it .the
vectors must be two-Dimensional.
#include <stdio.h>
struct vector
{
int x;
int y;
};
struct vector sumvector(struct vector v1, struct vector v2)
{
struct vector sum;
sum.x = v1.x + v2.x;
sum.y = v1.y + v2.y;
return sum;
}
void main()
{
struct vector v1, v2, sum;
{
printf("Enter the value of v1.x : ");
scanf("%d", &v1.x);
printf("Enter the value of v1.y : ");
scanf("%d", &v1.y);
printf("Enter the value of v2.x : ");
scanf("%d", &v2.x);
printf("Enter the value of v2.y : ");
scanf("%d", &v2.y);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
}
sum = sumvector(v1, v2);
printf(" The sum of X dimensional is : %d\n The sum of Y dimensional is :
%d\n", sum.x, sum.y);
}
#include <stdio.h>
typedef struct complex
{
int real;
int Complex;
} comp;
void display(comp c)
{
printf("The value of the real parts is : %d\n", [Link]);
printf(" The value of the complex part is : %d\n", [Link]);
}
void main()
{
comp cnumbs[5];
for (int i = 0; i < 5; i++)
{
printf("Enter the real value %d numb\n", i + 1);
scanf("%d", &cnumbs[i].real);
printf("Enter the complex value %d numb\n", i + 1);
scanf("%d", &cnumbs[i].Complex);
}
for (int i = 0; i < 5; i++)
{
display(cnumbs[i]);
}
}
#include <stdio.h>
struct complex
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
int real;
int Complex;
};
void main()
{
struct complex cnumbs[5];
{
for (int i = 0; i < 5; i++)
{
printf("Enter the real value %d numb\n", i + 1);
scanf("%d", &cnumbs[i].real);
printf("Enter the complex value %d numb\n", i + 1);
scanf("%d", &cnumbs[i].Complex);
}
}
for (int i = 0; i < 5; i++)
{
printf("The value of the real parts is : %d\n", cnumbs[i].real);
printf(" The value of the compex part is : %d\n", cnumbs[i].Complex);
}
}
#include <stdio.h>
typedef struct complex
{
int real;
int Complex;
} comp;
void main()
{
comp cnumbs[5];
{
for (int i = 0; i < 5; i++)
{
printf("Enter the real value %d numb\n", i + 1);
scanf("%d", &cnumbs[i].real);
printf("Enter the complex value %d numb\n", i + 1);
scanf("%d", &cnumbs[i].Complex);
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- // Create a structure representing a bank account of a customer. What fields did you use.
#include <stdio.h>
struct bankaccount
{
int Account_number;
char Name[100];
int balance;
int Opening_date;
char Address[100];
};
void main()
{
struct bankaccount a;
{
printf("Enter the Account anumber : ");
scanf("%d", &a.Account_number);
printf("Enter the Name : ");
scanf("%s", &[Link]);
printf("Enter the Balance : ");
scanf("%d", &[Link]);
printf("Enter the Opening Date : ");
scanf("%d",&a.Opening_date);
printf("Enter the Address : ");
scanf("%s",&[Link]);
}
printf("****************************************************\n\n");
printf("\n******************************************************");
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
// Write a structure capable of storing date .Write a function to compare those times.
#include<stdio.h>
struct time
{
int Hour;
int Minute;
int Second;
};
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
void main()
{
struct time t1,t2;
int a;
printf("Enter the Hours : ");
scanf("%d", &[Link]);
printf("Enter the Minutes : ");
scanf("%d", &[Link]);
printf("Enter the Secondrs : ");
scanf("%d", &[Link]);
- // Write a structure capable of storing date. Write a function to compare those dates.
#include<stdio.h>
struct date
{
int year;
int month;
int day;
};
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
{
return -1;
}
if ([Link]>[Link])
{
return 1;
}
if([Link]<[Link])
{
return -1;
}
if ([Link]>[Link])
{
return 1;
}
if([Link]<[Link])
{
return -1;
}
return 0;
}
void main()
{
struct date d1,d2;
int a;
printf("Enter the Year : ");
scanf("%d", &[Link]);
printf("Enter the month : ");
scanf("%d", &[Link]);
printf("Enter the day : ");
scanf("%d", &[Link]);
a = datecmp(d1,d2);
printf("Time comparision function returns : %d ",a);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
typedef struct time
{
int Hour;
int Minute;
int Second;
}time;
void display(time t)
{
printf("The time is : %d:%d:%d\n", [Link],[Link],[Link]);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
}
void main()
{
time t1,t2;
int a;
printf("Enter the Hours : ");
scanf("%d", &[Link]);
printf("Enter the Minutes : ");
scanf("%d", &[Link]);
printf("Enter the Secondrs : ");
scanf("%d", &[Link]);
#include<stdio.h>
struct timestamp
{
int Year;
int Month;
int Day;
int Hour;
int Minute;
int Second;
};
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
{
if ([Link]>[Link])
{
return 1;
}
if([Link]<[Link])
{
return -1;
}
if ([Link]>[Link])
{
return 1;
}
if([Link]<[Link])
{
return -1;
}
if ([Link]>[Link])
{
return 1;
}
if([Link]<[Link])
{
return -1;
}
if ([Link]>[Link])
{
return 1;
}
if([Link]<[Link])
{
return -1;
}
if ([Link]>[Link])
{
return 1;
}
if([Link]<[Link])
{
return -1;
}
if ([Link]>[Link])
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
return 1;
}
if([Link]<[Link])
{
return -1;
}
return 0;
}
void main()
{
struct timestamp t1,t2;
int a;
printf("Enter the Year : ");
scanf("%d", &[Link]);
printf("Enter the month : ");
scanf("%d", &[Link]);
printf("Enter the day : ");
scanf("%d", &[Link]);
printf("Enter the Hours : ");
scanf("%d", &[Link]);
printf("Enter the Minutes : ");
scanf("%d", &[Link]);
printf("Enter the Secondrs : ");
scanf("%d", &[Link]);
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- // Write a program to read two integers from '[Link]' file by using "FILE
Pointer".
#include<stdio.h>
void main()
{
FILE *ptr;
int num1;
int num2;
ptr = fopen("[Link]", " r");
fscanf(ptr,"%d", &num1);
fscanf(ptr,"%d", &num2);
fclose(ptr);
printf(" The value of num1 is %d\n",num1);
printf(" The value of num2 is %d\n",num2);
}
#include<stdio.h>
void main()
{
FILE *ptr;
int num1;
int num2;
ptr = fopen("[Link]","r");
if (ptr == NULL)
{
printf(" This file does not exit \n");
}
else
{
fscanf(ptr,"%d", &num1);
fscanf(ptr, "%d", &num2);
fclose(ptr);
printf(" The value of the num1 is %d\n",num1 );
printf(" The value of the num2 is %d\n", num2);
}
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include<stdio.h>
void main()
{
FILE *ptr;
ptr = fopen("[Link]","r");
printf(" The valu of my character is %c\n", fgetc(ptr));
printf(" The valu of my character is %c\n", fgetc(ptr));
printf(" The valu of my character is %c\n", fgetc(ptr));
printf(" The valu of my character is %c\n", fgetc(ptr));
printf(" The valu of my character is %c\n", fgetc(ptr));
printf(" The valu of my character is %c\n", fgetc(ptr));
printf(" The valu of my character is %c\n", fgetc(ptr));
fclose(ptr);
}
#include<stdio.h>
void main()
{
FILE *ptr;
ptr = fopen("[Link]","w");
fputc('N', ptr);
fclose(ptr);
}
#include<stdio.h>
void main()
{
FILE *ptr;
char ch;
ptr = fopen("[Link]","r");
ch = fgetc(ptr);
while(ch!=EOF)
{
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
printf("%c",ch);
ch = fgetc(ptr);
}
}
[Link] file 23 45 67
#include <stdio.h>
void main()
{
int a, b, c;
FILE *ptr;
ptr = fopen("[Link]", "r");
fscanf(ptr, "%d", &a);
fscanf(ptr, "%d", &b);
fscanf(ptr, "%d", &c);
printf(" The value of A : %d\n", a);
printf(" The value of B : %d\n", b);
printf(" The value of C : %d\n", c);
}
#include<stdio.h>
void main()
{
FILE *ptr;
int n;
printf(" Enter the any number ");
scanf(" %d", &n);
ptr = fopen("[Link]","w");
for( int a=0;a<10;a++)
{
fprintf(ptr, "%d X %d = %d\n",n,a+1,n*(a+1));
}
fclose(ptr);
printf("Successfully generated table of %d to [Link]\n",n);
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- // Write a program to read a text file character by character and write its content
twice in a seperate file .
#include<stdio.h>
void main()
{
FILE *ptr1;
FILE *ptr2;
ptr1 = fopen("[Link]","r");
ptr2 = fopen("[Link]","w");
char c = fgetc(ptr1);
while (c!=EOF)
{
fputc(c,ptr2);
fputc(c,ptr2);
c = fgetc(ptr1);
}
fclose(ptr1);
fclose(ptr2);
}
- /* Take name and salary of two employees as input from the user and write
them to a text file in the following formate
name1,3300
name2,7700 */
#include <stdio.h>
void main()
{
FILE *ptr;
char Name1[100];
int Salary1;
char Name2[100];
int Salary2;
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
#include <stdio.h>
#include<conio.h>
int main()
{
int num;
FILE *ptr1;
FILE *ptr2;
ptr1 = fopen("[Link]", "r");
ptr2 = fopen("[Link]", "w");
// if(ptr1 == NULL)
// {
// printf("Error opening file\n");
// return -1;
// }
// if(ptr2 == NULL)
// {
// printf("Error creating file\n");
// return -1;
// }
while (fscanf(ptr1, "%d ", &num)==1)
{
fprintf(ptr2, " %d", num * 2);
}
fclose(ptr1);
fclose(ptr2);
printf("Doubled integers written to the [Link] file .\n");
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])
lOMoARcPSD|61952182
- //Write a program that takes Roll_No, Name, Address and Date of Birthand utput
the information on screen.
#include <stdio.h>
#include <string.h>
struct date
{
int year;
int month;
int day;
};
struct student
{
int Roll_No;
char Name[100];
char Address[100];
struct date d;
};
void main()
{
struct student a;
printf(" Enter The Roll No. : ");
scanf("%d", &a.Roll_No);
printf(" Enter the Name : ");
scanf("%s", &[Link]);
printf(" ENter the Address : ");
scanf("%s", &[Link]);
printf(" Enter the Date of Birth YYYY/MM/DD : ");
scanf("%d %d %d", &[Link], &[Link], &[Link]);
printf("\n*************************************************\n\n");
printf("\n\n***********************************************\n\n");
}
Nagendra Das
Downloaded by Aayush Mandal (mandalaayush069@[Link])