1 Learning in C Program (1)
1 Learning in C Program (1)
Program
Data Types in C Programming
Syntax
float variable_name;
Example of float Data Type in C
#include <stdio.h>
int main() {
float myNum =
6.85;
printf("%f",
myNum);
4. double return 0;
}
The double data type is used to store double-precision floating-point numbers
i.e. 64 bits of decimal numbers or floating points, which offer higher precision
than float for decimal values. This data type size is 8 bytes, i.e., it occupies 8
bytes of memory. It ranges between 1.7E-308 to 1.7E+308.
Syntax #include <stdio.h>
double variable_name; int main()
{
double num = 20.63;
printf("%lf", num);
return 0;
}
Tricks To Remember Operators
Precedence Rule In C Programming
Language.
Examples showing use of Arithmetic
Operators
Examples showing use of Relational
Operators
Examples showing use of Logical
Operators
Examples showing use of Bitwise
Operators
Examples showing use of Increment and
Decrement Operators
Examples showing Examples showing
use of Increment use of Decrement
Operators Operators
Examples showing use of Assignment
Operators
#include <stdio.h>
int main() {
int x = 5;
x += 3;
printf("The value of x is %d\n",
x);
return 0;
}
#include <stdio.h>
#include <stdio.h>
int main() {
int main() { int x = 5;
int x = 10; x *= 4;
x -= 3; printf("The value of x is %d\n",
printf("The value of x is %d\n", x);
x);
return 0; return 0;
} }
Examples showing use of Conditional
Operators
Practice Problems on Operator Precedence and
Associativity in C
#include <stdio.h> #include <stdio.h> int main()
int main() int main() {
{ { int x = 50, y = 5;
int a = 11, b = 5, c = 2, int x = 5, y = 10; int z;
d=4, e=8; int z; z = x / 2 + y;
// int result = a / b * c; z = x * 2 + y; printf(“\n output
int result = a / (b * (c+ printf(“\n output = = %d”, z);
(d-e))); %d”, z); return 0;
printf("%d\n", result); return 0; } Output:30
return 0; } Output:20
int int main()
} main()
{ {
int x = 30, y = 12; int x = 80, y = 3;
int z; int z;
z = (x * 2 )/ y; z = x / 2 * y;
printf(“\n output = printf(“\n output =
%d”, z); %d”, z);
return 0; return 0;
} Output:5 } Output:120
Flowchart of if statement in C #include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers:");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
#include<stdio.h> {
int main() printf("%d is largest",b);
{ }
int number=0; if(c>a && c>b)
printf("Enter a number:"); {
scanf("%d",&number); printf("%d is largest",c);
if(number%2==0) }
{ if(a == b && a == c)
printf("%d is even number",num {
ber); printf("All are equal");
} }
return 0; return 0;
} }
Flowchart of the if-else statement in C
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to
#include<stdio.h>
int main()
vote...");
{ }
int number=0; else
printf("enter a number:"); {
scanf("%d",&number); printf("Sorry ... you can't
if(number%2==0) vote");
{ }
printf("%d is even
}
number",number);
}
Else
{
printf("%d is odd
number",number);
}
Nested if else statement in C
Syntax:
A nested if-else statement is an if statement inside another if
statement. The general syntax of nested if-else statement in C is
as follows:
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10)
{
if (i < 15) // First if statement.
printf("i is smaller than 15\n"); // Nested -
if statement.
// Will only be executed if
statement above is true.
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
Nested if else statement in C Examples:
#include <stdio.h> Example 1: Grading System
Problem Statement: Determine the grade of a student based on their score. If
int main() the score is above 90, it's an 'A'. If the score is between 80 and 90, check if it's
{ above 85 for an 'A-' or else 'B+'. Below 80, if it's above 70, it's a 'C', otherwise,
it's a 'D'.
int score = 88;
// Check if score is greater than 90
if (score > 90)
{
printf("Grade: A");
} else
{
// Score is 90 or below, check if score is greater than 80
if (score > 80) {
// Score is between 81 and 90, check if score is
above 85
if (score > 85) {
printf("Grade: A-");
} else {
printf("Grade: B+");
}
} else {
// Score is 80 or below, check if score is above 70
if (score > 70) {
printf("Grade: C");
} else {
printf("Grade: D");
}
}
}
return 0;
}
If else-if ladder Statement
The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where
there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a
condition is true then the statements defined in the if block will be executed, otherwise if some other
condition is true then the statements defined in the else-if block will be executed, at the last if none of
the condition is true then the statements defined in the else block will be executed. There are multiple
else-if blocks possible. It is similar to the switch case statement where the default is executed instead
of else block if none of the cases is matched.
if(condition1){
//code to be executed if
condition1 is true
}else if(condition2){
//code to be executed if
condition2 is true
}
else if(condition3){
//code to be executed if
condition3 is true
}
...
else{
//code to be executed if all the
conditions are false
}
The example of an if-else-if ladder statement in C
language is given below.
#include <stdio.h>
#include<stdio.h> int main()
int main(){ {
int number=0; int marks;
printf("enter a number:"); printf("Enter your marks?");
scanf("%d",&number); scanf("%d",&marks);
if(number==10){ if(marks > 85 && marks <= 100)
printf("number is equals to 10"); {
} printf("Congrats ! you scored
else if(number==50){ grade A ...");
printf("number is equal to 50"); }
} else if (marks > 60 && marks <=
else if(number==100){ 85)
printf("number is equal to 100"); {
} printf("You scored grade B
else{ + ...");
printf("number is not equal to 10, 50 }
or 100"); else if (marks > 40 && marks <=
} 60)
return 0; {
} printf("You scored grade B ...");
}
else if (marks > 30 && marks <=
40)
{
printf("You scored grade C ...");
}
else
{
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute
multiple operations for the different possible values of a single variable called switch variable. Here, We
can define various statements in the multiple cases for the different values of a single variable.
Rules for switch statement in C language
1.The switch expression must be of an integer or character type.
2.The case value must be an integer or character constant.
3.The case value can be used only inside the switch statement.
4.The break statement in switch case is not must. It is optional. If there is no break statement
found in the case, all the cases will be executed present after the matched case. It is known
as fall through the state of C switch statement.
}
Thanking you