C Function for Unary Quadratic Equation
C Function for Unary Quadratic Equation
Features of C?
1) It is a procedural language
2) It is high level language
3) It is used for software development.
4) ‘C’ language is a first step for programmer to start
5) It is reliable, simple, & easy to use.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-1
C- Programming Fundamentals of C-programming
Structure of c program
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
#include <stdio.h>
void main()
{
/*my first program in C */
printf("Hello, World! \n");
return 0;
}
▪ /* Comments */
This is a comment block, which is ignored by the compiler. Comment can be used anywhere in
the program to add info about the program or code block, which will be helpful for developers to
understand the existing code in the future easily.
▪ #include<stdio.h>
This is a preprocessor command. That notifies the compiler to include the header file stdio.h
in the program before compiling the source-code.
▪ int/void main()
int/void is a return value, which will be explained in a while.
This is the main function, which is the default entry point for every C program and the void in
front of it indicates that it does not return a value.
▪ Braces
Curly braces which shows how much the main() function has its scope.
▪ printf()
This is another pre-defined function of C which is used to be displayed text string in the
screen.
▪ return 0
At the end of the main function returns value 0.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-2
C- Programming Fundamentals of C-programming
TOKENS OF C
The C programs are made up of different things termed as tokens.
• CHARACTER SET
Character Set Example
Digits (numbers) 0, 1, 2, 3. .. .. . .. 9
• KEYWORDS
✓ There are some special words that have a predefined meaning for the C compiler known as
keywords.
✓ Keywords are a set of words which are reserved for the certain operations and hence are also
sometimes referred as reserved words.
✓ These words cannot be used as identifiers.
✓ There are 32 Keywords in C.
✓ The keywords used in C are as given below:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-3
C- Programming Fundamentals of C-programming
• IDENTIFIERS
Identifiers are name given to different user defined things like variables, constants, functions,
array, structures, unions, etc.
some rules need to follow while making these identifiers.
These rules are stated below:
▪ A Variable name consists of any combination of alphabets, digits and underscores.
▪ It should not start with the digit; the first character of the variable name must either be alphabet or
underscore.
▪ Commas and blanks are not allowed in the variable name.
▪ Special symbols are not allowed except underscore.
▪ You cannot use Keywords (reserve words) as variable name.
• DATA TYPE
What type of data can a variable hold is called data type.
▪ The data type decides the type of data required for storing in the memory.
▪ The data types of C can be divided into two types: primary and secondary data types.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-4
C- Programming Fundamentals of C-programming
• CONSTANTS
▪ Constants are values given to the identifiers that do not change throughout the execution of the
program.
▪ Constants can be defined either by writing the keyword const before the data type by using
#define.
▪ Constants are used to declare the values that remain constant, for e.g. value of pi.
Declaration
– const float PI=3.14;
– #define PI 3.14
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-5
C- Programming Fundamentals of C-programming
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-6
C- Programming Fundamentals of C-programming
• ESCAPE SEQUENCES
▪ Escape sequence is a character followed by a backslash (\).
▪ They are used specially to perform some operations like going to new line, providing a horizontal
tab, vertical tab etc.
The following is a list of escape sequences.
\n Newline
\t Horizontal Tab
\v Vertical Tab
\b Backspace
\r Carriage Return
\f From feed
\a Audible Alert (bell)
\\ Backslash
\? Question mark
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-7
C- Programming Fundamentals of C-programming
OPERATORS
▪ Operators are different symbols used to perform operations on operands.
▪ The operators are one of the tokens of C.
▪ There are different types of operators classified based on their functions
Unary Operators
o These are operators that require only one operand
• Unary minus (-):
o It changes the algebraic sign of the variable to which it is preceded.
o For e.g., if x = 3 and y = 6 then
y = −x;
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-8
C- Programming Fundamentals of C-programming
Program: Write a program to accept a float number and display the integer part
using type casting operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
float b;
clrscr();
printf(“enter float number:”);
scanf(“%f”.&b);
a = (int)b;
printf(“the integer part of the number is :%d”,a);
getch();
}
output:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-9
C- Programming Fundamentals of C-programming
#include<stdio.h>
#include<conio.h>
int main()
{
int Marks1, Marks2, Marks3, Marks4, Marks5,Add;
float Avg;
printf("\nEnter marks for five subject::");
scanf("%d%d%d%d%d",&Marks1,&Marks2,&Marks3,&Marks
4, &Marks5);
Add = Marks1+Marks2+Marks3+Marks4+Marks5;
Avg = (float)(Add)/5;
printf("\nAvg is = %f", Avg);
getch();
return 0;
}
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-10
C- Programming Fundamentals of C-programming
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-11
C- Programming Fundamentals of C-programming
#include<stdio.h>
#include<conio.h>
void main()
{
int x=4, y=9;
int z;
z=(x++) +(--y) +y;
Printf(“Value=%d\n”,z);
z=(--x) +x+(y--);
printf(“Value=%d\n”,z);
getch();
}
output:
Value = 20
Value = 16
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-12
C- Programming Fundamentals of C-programming
#include<stdio.h>
#include<conio.h>
void main()
{
int x=10, y, z;
z=y=x;
y-=--x;
z-=x--;
x-=--x-x--;
printf(“x=%d y=%d z=%d”, x, y, z);
}
output:
x=6
y=1
z=1
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-13
C- Programming Fundamentals of C-programming
• Size of operator :
o This operator is used to know the size of variable required to store value in memory.
o Size of operator can also find size of a data type.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-14
C- Programming Fundamentals of C-programming
Binary Operators
o Operators that require two operands are called as binary operators.
• Arithmetic Operators:
o This are set of operators used to perform basic arithmetic operations like addition,
subtraction, multiplication and division.
o There are five operators in this set. They are:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-15
C- Programming Fundamentals of C-programming
5 & 3 =1
( 5 )10 = (0 1 0 1)2
( 3 )10 = (0 0 1 1)2
(0 0 0 1)2= (1)10
2. Bitwise OR operation (|)
12 | 9 =13
( 12 )10 = (1 1 0 0)2
( 9)10 = (1 0 0 1)2
(1 1 0 1)2 = (13)10
3. Bitwise EXOR operation (^)
8 ^ 10 =2
( 8 )10 = (1 0 0 0)2
(10)10 = (1 0 1 0)2
(0 0 1 0)2 = (2)10
4. Bitwise NOT operation (~)
~ 7 = -8
(7)10 = (0 1 1 1)2
(1 0 0 0)2 = (−8)10
5. Left shift operator: (<<)
This statement will shift binary digit to the left by 1 digit.
3<<2 = 12
( 3)10 = (0 0 0 0 0 0 1 1)2
13>>3 = 1
(13)10 = (0 0 0 0 1 0 1 1)2
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-16
C- Programming Fundamentals of C-programming
• Logical operators: -
o Logical operators are used to evaluate conditions or expressions.
o The logical operators are AND and OR.
1. AND Operator (&)
o The AND operators checks for all conditions to be true then only it returns true else false.
o For example, a statement
o y >5 && y <10;
o will result in “true” i.e., 1 if the value of y is greater than 5 AND less than 10, else it will result
in false i.e.,0
2. OR Operator: -
o The OR operator checks for any one or all conditions from the given conditions to be true then
it evaluates true.
o Otherwise, if all conditions are false then it returns false.
o For example, a statement
o y >5 || y <10;
o will result in “true” i.e., 1 if the value of y is greater than 5 OR less than 10, else it will result
in false i.e.,0
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-17
C- Programming Fundamentals of C-programming
• Relational Operator:
o The relational operators are used to test the relation between two variables or a variable
and constant.
o The operators like
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-18
C- Programming Fundamentals of C-programming
Conditional Operators
o Conditional operator is also known as ternary operator.
o The operator that requires three operands is called as ternary operator.
o Syntax:
(Condition)? Expr1: Expr2;
o It checks for the condition if condition is true then expr1 will be executed otherwise expr2 will be
executed.
E.g., max= (a > b)? a: b;
o This will put the value of a into max if condition is true else the value of b into max.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-19
C- Programming Fundamentals of C-programming
Program: Write a program to accept two numbers from user and display
greatest of two numbers using conditional operator.
#include<stdio.h>
#include<conio.h>
Void main()
{
int n1, n2, greater;
printf(“Enter a two number”);
scanf(“%d %d”,&n1,&n2);
greater=(n1>n2)? n1: n2;
printf(“The greater number is: %d”,greater);
getch();
}
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-20
C- Programming Fundamentals of C-programming
Program: Write a program to accept three numbers from user and display the
greatest of three using the conditional operator.
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,n3,greater;
clrscr();
printf(“enter three numbers\n”);
scanf(“%d %d %d”,&n1,&n2,&n3);
greater = (n1>n2)?((n1>n3)?n1:n3): ((n2>n3)?n2:n3));
printf(“the largest number is : %d ”,greater);
getch();
}
output:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-21
C- Programming Fundamentals of C-programming
1. Formatted IO Functions
• Two formatted IO functions in C
o printf() :- display a data on monitor.
o scanf() :- accept data from keyboard.
printf():
Example:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-22
C- Programming Fundamentals of C-programming
scanf():
Syntax:
ERROR
While writing c programs, errors also known as bugs in the world of programming may occur
unwillingly which may prevent the program to compile and run correctly as per the expectation of the
programmer.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-23
C- Programming Fundamentals of C-programming
1. Runtime Errors
2. Compile Errors
3. Logical Errors
C Runtime Errors
• C RUNTIME ERRORS are those errors that occur during the execution of a c program.
• Errors which occur during program execution(run-time) after successful compilation are called
run-time errors, One of the most common run-time errors are
• These types of error are hard to find as the compiler doesn’t point to the line at which the error
occurs.
Compile Errors
• Compile errors are those errors that occur at the time of compilation of the program. C compile
errors may be further classified as:
A. Syntax Error
B. Semantic Error
A. Syntax Error
When the rules of the c programming language are not followed, the compiler will show syntax errors.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-24
C- Programming Fundamentals of C-programming
Logical Errors
• Logical errors are the errors in the output of the program. The presence of logical errors leads to
undesired or incorrect output and are caused due to error in the logic applied in the program to
produce the desired output.
• Also, logical errors could not be detected by the compiler, and thus, programmers has to check
the entire coding of a c program line by line.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-25
C- Programming Fundamentals of C-programming
#include<stdio.h>
#include<conio.h>
void main()
{
float p,r,n,si;
clrscr();
printf("\nEnter the profit, rate & no of yr\n\n ");
scanf("%f%f%f",&p,&r,&n);
si=(p*r*n)/100;
printf("\nSimple Intrest=%f",si);
getch();
}
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-26
C- Programming Fundamentals of C-programming
#include<stdio.h>
int main()
{
int a,b,temp;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}
output:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-27
C- Programming Fundamentals of C-programming
#include <stdio.h>
#include <conio.h>
void main()
{
int x , y ;
printf("Enter two integers: ") ;
scanf("%d %d" , &x , &y) ;
printf("x=%d y=%d \n" , x , y) ;
x=x+y ;
y=x-y ;
x=x-y ;
printf("After swapping: \n") ;
printf("x=%d y=%d" , x , y) ;
getch() ;
}
output:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-28
C- Programming Fundamentals of C-programming
Program: WAP to accept the length and breadth of a rectangle from the user.
Calculate and display the area and perimeter.
#include<stdio.h>
#include<conio.h>
void main ()
{
float length, breadth, area, perimeter; clrscr():
printf (“Enter the length and breadth of the rectangle:”);
scanf(“%f%f”, &lenghth, & breadth);
area = length * breadth;
perimeter = 2* (length + breadth);
printf(“The area of rectangle is = % f and its perimeter is = % f”, area,
perimeter);
getch();
}
output:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-29
C- Programming Fundamentals of C-programming
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
float x1,x2;
clrscr(0;
printf (Enter the coefficients of the quadratic equation :”);
scanf (“%d”%d%”,&a,&b,&c);
x1=(-b+pow((b*b-4*a*c),0.5)))/2*a;
x2=(-b-pow((b*b-4*a*c),0.5)))/2*a;
printf (“the roots of quadratic equation are :% f and %f”, x1,x2);
getch();
}
Output:
3
The roots of quadratic equation are : -1.000000 and -1.000000
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-30
C- Programming Fundamentals of C-programming
#include<stdio.h>
#include<conio.h>
void main()
{
int x=1;
clrscr();
printf (“%d”%d%d\n”,x,(x=x+2),(x<<2));
x<<2;
printf (“%d”%d%d\n”,++x,++,++x);
getch();
}
Output:
334
644
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-31
C- Programming Fundamentals of C-programming
Questions
1. Write algorithm and draw flowchart to calculate roots of quadratic equations. (6 Marks.) DEC-
9. Define algorithm, write algorithm to check whether given number is Armstrong number or not.
13. Write a program to accept marks of 5 subjects marks, and display total and average.
ii) 5 + 5 = = 10 | | 1 + 3 = = 5 ans: 1
16. Explain logical operators. Write a c program to demonstrate the same. [4M]
18. Explain bitwise right shift operator, bitwise x-or operator [4 M].
[5 M] DEC-18
21. Explain gets() and puts() functions of C language, comment on their parameter and return values.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.2-32