Introduction
C Language (Notes)
● Operators in C Language:
1. Arithmetic (+,-,/,*,%)
2. Relational (>,<,=,==,<=,>=,=!)
3. Logical (NOT !, AND &&, OR ||)
4. Unary (INCREMENT ++, DECREMENT - -)
5. Terminary (condition ? expression_if_true : expression_if_false;)
* Arithmetic, Relational, & Logical Operators are Binary Operators
● Single equals to is used to assign values to the variables. (Assignment Operator)
● Double equals to shows that the values of two variables are equal. For example: a==b. In
this case values of a and b are equal.
● <= is used to show less than equal to.
● >= is used to show greater than equal to.
● != is used to show not equal to.
● ! (NOT) converts true values into false values and vice versa.
● && (AND) prints the output as TRUE STATEMENT when all conditions are true. If one
condition is false it will print FALSE STATEMENT.
● || (OR) prints TRUE STATEMENT even if one condition is false. It also prints TRUE
STATEMENT when one condition is true.
● ++ (INCREMENT) increases the value of a variable by one.
● - - (DECREMENT) decreases the value of a variable by 1.
● Ternary Operator provides a concise way to express conditional logic, similar to a
shorthand if-else statement.
Example
Write a C program to print the bio data by taking input from the user. Use char[ ] in it.
Code: Output:
#include <stdio.h>
int main() {
char name[20];
char gender[15];
char father_name[150];
printf("Enter name: ");
scanf("%s",name);
printf("Enter gender: ");
scanf("%s",gender);
printf("Enter father name: ");
scanf("%s",father_name);
printf("Your name is %s.\n",name);
printf("Your gender is %s.\n",gender);
printf("Your father name is
%s.\n",father_name);
return 0;
}
If / Else If / Else
If / Else If /Else
If Condition
It checks if something is true or false and then performs a specific action based on the result.
● If the condition is true, the program runs a specific block of code.
● If the condition is false, it completely ignores that block of code and moves on.
If Else Condition
If Else Condition
● If the condition is true: The code inside the if block (the indented lines immediately
following if) is executed.
● Else: If the condition is false, the program skips the if block and instead executes the code
inside the else block.
Examples
1.
Code: Output:
#include <stdio.h>
int main()
{
int m1,m2;
printf("Enter 1st Subject's Marks: ");
scanf("%d",&m1);
printf("Enter 2nd Subject's Marks: ");
scanf("%d",&m2);
if(((m1>50)&&(m1<=90))||((m2>=50)&&(m2<=9
0)))
{
printf("Congratulations!! You are PASSED");
}
else
{
printf("Better luck next time!!");
}
return 0;
}
2.
Code: Output:
#include <stdio.h>
int main ()
{
int a;
printf("Enter any whole number: ");
scanf("%d",&a);
if(a%2==0) {
printf("It is an even number!");
}
Else {
printf("It is an odd number!");
}
return 0;
}
3. Write a C Program to find out whether the triangle is valid or not. Take 3 input values
representing angles of a Triangle.
Code: Output:
#include <stdio.h>
int main()
{
int a,b,c,sum;
printf("Enter 1st angle of a Triangle:");
scanf("%d",&a);
printf("Enter 2nd angle of a Triangle:");
scanf("%d",&b);
printf("Enter 3rd angle of a Triangle:");
scanf("%d",&c);
sum=a+b+c;
if (sum==180)
{
printf("The triangle is VALID.");
}
else
{
printf("The triangle is INVALID.");
}
return 0;
}
If Else If Condition
If Else If Condition
● If (condition1): If condition1 is true, the code inside this if block executes.
● Else if (condition2): If condition1 is false, then condition2 is checked. If condition2 is true,
the code inside this else if block executes.
● Else (optional): If all preceding if and else if conditions are false, the code inside this else
block executes.
Examples
1.
Code: Output:
#include <stdio.h>
int main()
{
int t;
printf("Enter the temperature today:");
scanf("%d",&t);
if(t>35)
{
printf("It is hot!");
}
else if(t>=20 && t<=35)
{
printf("Nice day!");
}
else
{
printf("It is cold!");
}
return 0;
}
2. Write a C Program to find out whether the triangle is Equilateral, Isosceles or Scalene.
Take 3 input values representing sides of a Triangle.
Code: Output:
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter 1st side of a Triangle:");
scanf("%d",&a);
printf("Enter 2nd side of a Triangle:");
scanf("%d",&b);
printf("Enter 3rd side of a Triangle:");
scanf("%d",&c);
if((a==b)&&(b==c))
{
printf("The triangle is EQUILATERAL.");
}
else if(a==b||b==c||a==c)
{
printf("The triangle is ISOSCELES.");
}
else
{
printf("The triangle is SCALENE.");
}
return 0;
}
3. Write a c program to find out whether the input entered by the user is capital
alphabet, small alphabet or special character.
Code: Output:
#include <stdio.h>
int main()
{
char c;
printf("Enter any CHARACTER: ");
scanf("%c",&c);
if(c>='a' && c<='z')
{
printf("The CHARACTER is small
alphabet.");
}
else if(c>='A' && c<='Z')
{
printf("The CHARACTER is capital
alphabet.");
}
else
{
printf("The CHARACTER is a special
character or number.");
}
return 0;
}
Switch Case
Switch Case
● It is a conditional statement that allows you to execute code blocks on the value of a variable
or an expression.
● Used in place of if-else statements.
● It is used when there are more than 3 or more conditions
● It helps to give users options.
Example
Code: Output:
#include <stdio.h>
int main()
{
int var = 1;
switch (var)
{
case 1:
printf("Case 1 is Matched.");
break;
case 2:
printf("Case 2 is Matched.");
break;
case 3:
printf("Case 3 is Matched.");
break;
default:
printf("Default case is Matched.");
break;
}
return 0;
}
In the similar code, when ‘break;’ is removed:
Code: Output:
#include <stdio.h>
int main()
{
int var = 1;
switch (var)
{
case 1:
printf("Case 1 is Matched.");
case 2:
printf("Case 2 is Matched.");
case 3:
printf("Case 3 is Matched.");
default:
printf("Default case is
Matched.");
}
return 0;
}
Example # 2
Write a C program to print a calculator with two numbers input.
Code: Output:
#include <stdio.h> Addition
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator); Subtraction
printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
switch (operator) { Multiplication
case '+':
result = num1 + num2;
printf("%.2lf + %.2lf = %.2lf\n", num1, num2,
result);
break;
case '-':
result = num1 - num2;
Division
printf("%.2lf - %.2lf = %.2lf\n", num1, num2,
result);
break;
case '*':
result = num1 * num2;
printf("%.2lf * %.2lf = %.2lf\n", num1, num2,
result); Division by zero
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("%.2lf / %.2lf = %.2lf\n", num1,
num2, result);
} else { Invalid operator
printf("Error: Division by zero is not
allowed.\n");
}
break;
default:
printf("Error: Invalid operator entered.\n");
}
return 0;
}
For Loop
Looping
In C programming, there is often a need for repeating the same part of the code multiple times.
1. It is used to execute statements.
2. Loops help run the same code again and again.
3. You don’t need to write the same code many times.
4. Makes the program shorter and cleaner.
5. Useful for repeating actions like printing numbers or taking inputs.
6. Helps perform repeated calculations easily.
For Loop
i is an iterator.
z is the limit.
a is the number by which i will be multiplied.
Example # 1
Code: Output
#include <stdio.h>
int main()
{
int i;
for(i=0;i<=5;i++)
{
printf("%d\n",i);
}
return 0;
}
Example # 2
Code: Output:
#include <stdio.h>
int main()
{
int i,z,a;
printf("Enter value of I:\n");
scanf("%d",&i);
printf("Enter value of Z:\n");
scanf("%d",&z);
printf("Enter value of A:\n");
scanf("%d",&a);
for (i;i<=z;i++)
{
printf("%d*%d=%d\n",i,a,i*a);
}
return 0;
}
Example # 3
a) Write the code to output the factorial of the number given by the user(each factorial should
be defined).
Code: Output:
#include <stdio.h>
int main()
{
int n,i,fact=1;
printf("Enter an INTEGER: ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
fact=fact*i;
printf("Factorial of %d=%d\n",n,fact);
}
return 0;
}
b) Write the code to output the factorial of the number given by the user.
Code: Output:
#include <stdio.h>
int main()
{
int n,i,fact=1;
printf("Enter an INTEGER: ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
fact=fact*i;
}
printf("Factorial of %d=%d\n",n,fact);
return 0;
}
Example # 4
A class of 15 students take an examination in which marks range from 1 to 100, write a c program to
find and print the average marks.
Code: Output:
#include <stdio.h>
int main() {
int i, marks;
float sum = 0, average;
printf("Enter the marks of 15 students:\n");
for(i = 1; i <= 15; i++) {
printf("Student %d: ", i);
scanf("%d", &marks);
sum += marks;
}
average = sum / 15;
printf("\nThe average marks of the class =
%.1f\n", average);
return 0;
}
While Loop
While Loop
Example # 1
Write a C program to print numbers from 1 till 50 using WHILE loop.
Code: Output:
#include <stdio.h>
int main()
{
int num;
printf("enter a number: ");
scanf("%d",&num);
while(num<=50)
{
printf("%d\n", num);
num++;
}
return 0;
}
Example # 2
Write a C program to convert weight in kgs into pounds. Also zero should signal exit from code.
Code: Output:
#include <stdio.h>
int main ()
{
float kg,pounds;
printf("Enter weight in KGS:\n");
scanf("%f",&kg);
while (kg!=0)
{
pounds=2.2*kg;
printf("Weight in POUNDS:%f\n\n",pounds);
printf("Enter KGS (zero to exit): ");
scanf("%f",&kg);
}
printf("Goodbye");
return 0;
}
Do While Loop
Do While Loop
It is used when the code block within the loop needs to be executed at least once, regardless of
whether the loop's condition is initially true or false.
Example # 1
Write a C program to print numbers from 1 till 50 using the DO WHILE loop.
Code: Output:
#include <stdio.h>
int main()
{
int num;
printf("enter a number: ");
scanf("%d",&num);
do
{
printf("%d\n",num);
num++;
}
while(num<=50);
return 0;
}
Example # 2
Write a C program to print even numbers between 1 till 50 using DO WHILE loop.
Code: Output:
#include <stdio.h>
int main()
{
int num;
printf("enter a number: ");
scanf("%d",&num);
if(num%2==0)
{
printf("The number is even.");
do
{
printf("%d\n",num);
num+=2;
}
while(num<=50);
}
else
{
printf("The number is odd. COULDN'T
PROCEED.");
}
return 0;
}
Example # 3
Write a C program to print odd numbers between 1 till 50 using DO WHILE loop.
Code: Output:
#include <stdio.h>
int main()
{
int num;
printf("enter a number: ");
scanf("%d",&num);
if(num%2!=0)
{
printf("The number is ODD.");
do
{
printf("%d\n",num);
num+=2;
}
while(num<=50);
}
else
{
printf("The number is even. COULDN'T
PROCEED!");
}
return 0;
}
Example # 4
Write a c program to print all the lower case letters of the alphabet using do-while loop. The
ASCII codes for the lowercase letters are in the range of 97 to 122.
Code: Output:
#include <stdio.h>
int main() {
int i=97;
do
{
printf("%c\n",i);
i++;
}
while (i<=122);
return 0;
}
C Type Conversion
C Type Conversion
1. Implicit
● converts the value of one data type (int, float, double, etc.) to another.
● the value of one data type is automatically converted into another data type.
2. Explicit
● manually converts values of one data type to another type.
● The value of one data type is manually converted into another data type.
Examples (IMPLICIT)
1.
Code: Output:
#include<stdio.h>
int main()
{
double value = 4150.12;
printf("Double Value: %.2lf\n", value);
int number = value;
printf("Integer Value: %d", number);
return 0;
}
2.
Code: Output:
#include<stdio.h>
int main()
{
char alphabet = 'a';
printf("Character Value: %c\n", alphabet);
int number = alphabet;
printf("Integer Value: %d", number);
return 0;
}
Examples (EXPLICIT)
1.
Code: Output:
#include<stdio.h>
int main()
{
int number = 35;
printf("Integer Value: %d\n", number);
double value = (double) number;
printf("Double Value: %.2lf", value);
return 0;
}
2.
Code: Output:
#include<stdio.h>
int main()
{
int number = 97;
printf("Integer Value: %d\n", number);
char alphabet = (char) number;
printf("Character Value: %c", alphabet);
return 0;
}
Pre-Post Increment & Decrement
Pre-Post Increment & Decrement
Post Increment:
Value is assigned first then increment takes place.
Code: Output:
#include <stdio.h>
int main()
{
int x=5;
int y=x++;
printf("%d\n",x);
printf("%d",y);
return 0;
}
Pre Increment:
First increment takes place then value is assigned.
Code: Output:
#include <stdio.h>
int main()
{
int x=5;
int y=++x;
printf("%d\n",x);
printf("%d",y);
return 0;
}
Post Decrement:
Value is assigned first then decrement takes place.
Code: Output:
#include <stdio.h>
int main()
{
int x=5;
int y=x--;
printf("%d\n",x);
printf("%d",y);
return 0;
}
Pre Decrement:
First decrement takes place then values are assigned.
Code: Output:
#include <stdio.h>
int main()
{
int x=5;
int y=--x;
printf("%d\n",x);
printf("%d",y);
return 0;
}
Nesting Loop
Nesting Loop
● A nested loop means a loop statement inside another loop statement.
● For nested loops, the inner loop performs all of its iterations for each iteration of outer
loops.
● Outer loop is used for Rows. The inner loop is used for Columns.
Example # 1
a) Write a C program to print half triangles using asterisks.
Code: Output:
#include <stdio.h>
int main() {
int i,j,rows;
printf("Enter number of ROWS:");
scanf("%d",&rows);
for (i=1;i<=rows;i++)
{
for (j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
b) Write a C program to print a half triangle using numbers. Enter values of I as output.
Code: Output:
#include <stdio.h>
int main() {
int i,j,rows;
printf("Enter number of ROWS:");
scanf("%d",&rows);
for (i=1;i<=rows;i++)
{
for (j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}
c) Write a C program to print a half triangle using numbers. Enter values of J as output.
Code: Output:
#include <stdio.h>
int main() {
int i,j,rows;
printf("Enter number of ROWS:");
scanf("%d",&rows);
for (i=1;i<=rows;i++)
{
for (j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Example 2:
a) Write a C program to print a square using an asterisk.
Code: Output:
#include<stdio.h>
int main()
{
int i,j,rows;
printf("Enter no. of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=rows;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
b) Write a C program to print a square using numbers.
Code: Output:
#include<stdio.h>
int main()
{
int i,j,rows;
printf("Enter no. of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=rows;j++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}