0% found this document useful (0 votes)
22 views

C Language

C notes

Uploaded by

jonny2019420
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

C Language

C notes

Uploaded by

jonny2019420
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

 C PROGRAMING

LANGUAGE
C language:-
C is a procedural programming language
initially developed by Dennis Ritchie in
the year 1972 at Bell Laboratories of
AT&T Labs. It was mainly developed as a
system programming language to write
the UNIX operating system
Stdio.h:- stdio.h header file allows us to
perform input and output operations in C.
The functions like printf() and scanf() are
used to display output and take input
from a user.
Printf():-in C language, print f function is
used to print output on the screen. This
function is a part of the C standard library
“stdio.h” and it can allow formatting the
output in numerous ways.
Scanf():-In C programming language,
scanf is a function that stands for Scan
Formatted String. It is used to read data
from stdin (standard input stream i.e.
usually keyboard) and then writes the
result int o the given arguments.
 It accepts character, string, and
numeric data from the user using
standard input.
 scanf also uses format specifiers like
printf.

:- also reads a single character from the


keyboard. But it does not use any buffer,
so the entered character is immediately
returned without waiting for the enter
key.
Int main():-int main() is the entry point
for execution in C program. The void is a
keyword that represents function will not
return anything but a void value. Main is
the name of the function and ()
represents parameter list that can be
passed to function in this case nothing is
passed
Format Specifier in C
In C, a value can be a character type,
integer type, float type, and so on. To
display these values we have format
specifiers used in print f function. These
format specifiers start with the
percentage symbol ‘%’. Some of the
commonly used format specifiers are
given below.

%d - for printing integers


%f - for printing floating-point numbers
%c - for printing characters
%s - for printing strings
%p - for printing memory addresses
%x - for printing hexadecimal values
 BASIC C PROGRAMING
CODE :

 ADDITION OF 2 NUMBERS:
#include <stdio.h>
#include<conio.h>
Int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("%d + %d = %d", number1,
number2, sum);
}
 SUBTRACTION OF 2 NUMBER’S:
include <stdio.h>
Int main()
{
int number1, number2, subtract;
printf("Enter two int egers: ");
scanf("%d %d", &number1, &number2);
subtract = number1 - number2;
printf("%d - %d = %d", number1,
number2, subtract);
}
 Program to Multiplication of two
numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2,product;
printf("Enter two numbers:");
scanf("%d %d",&num1,&num2);
product=num1*num2;
printf("Product of two numbers:
%d",product);
}

 Program to division of two


numbers

#include<stdio.h>
#include<conio.h>
Int main()
{
int num1,num2,quotient;
printf("Enter two numbers:");
scanf("%d %d",&num1,&num2);
quotient=num1/num2;
printf("Quotient: %d",quotient);
}

 AREA OF SQUARE:

#include<stdio.h>
#include<conio.h>
Int main()
{
int side,area;
printf("Enter a side of square:");
scanf("%d",&side);
area=side * side;
printf("\nArea of Square: %d",area);
}
 C Program to find area of
Rectangle
#include<stdio.h>
#include<conio.h>
Int main()
{
int length,breadth, area;
printf("Enter length and breadth of
rectangle:");
scanf("%d %d",&length,&breadth);
area=length * breadth;
print f("\nArea of Rectangle: %d",area);
}

 Simple int erest:

#include<stdio.h>
#include<conio.h>
Int main()
{
float p,r,t,si;
printf(“enter the p and t and r=”);
scanf("%f %f %f",&p,&t,&r);
si=(p*t*r)/100;
print f("%f",si);
}

 C Program to convert
FAHRENHIET TO CELSIUS
#include<stdio.h>
#include<conio.h>
Int main()
{
float f,c;
print f("Enter celsius:");
scanf("%f",&c);
f=c*1.8+32;
print f("Fahrenhiet:%f",f);
}
 AREA OF TRIANGLE:
#include <stdio.h>
#include<conio.h>
int main()
{
float base, height, area;

print f("Enter base of the triangle: ");


scanf("%f", &base);
print f("Enter height of the triangle: ");
scanf("%f", &height);
area = (base * height) / 2;
print f("Area of the triangle = %.2f sq.
units", area);
}
 AREA OF CIRCLE:
#include<stdio.h>
#include<conio.h>
Int main()
{
const float pi=3.14;
int radius;
float area;
printf("Enter Radius:");
scanf("%d",&radius);
area=pi * (radius * radius);
print f("Area of Circle: %f",area);
}

 IF CONDITION

 Even or odd:
#include <stdio.h>
#include<conio.h>
Int main()
{
int num;
print f("Enter an int eger: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 = = 0)
print f("%d is even.", num);
else
print f("%d is odd.", num);
}

 LEAP YEAR OR NOT:


#include <stdio.h>
#include<conio.h>
Int main()
{
int year;
print f("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
print f("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
print f("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
print f("%d is a leap year.", year);
}
// all other years are not leap years
else {
print f("%d is not a leap year.", year);
}
}

 C program to find maximum


between three numbers
#include <stdio.h>
#include<conio.h>
int main()
{
float n1, n2, n3;
print f("Enter three different numbers: ");
scanf("%f %f %f", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1
is the largest
if (n1 >= n2 && n1 >= n3)
print f("%f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2
is the largest
if (n2 >= n1 && n2 >= n3)
print f("%f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3
is the largest
if (n3 >= n1 && n3 >= n2)
print f("%f is the largest number.", n3);
}

 Student grade calculation:

#include<stdio.h>

#include<conio.h>

Int main()
{

int num;

print f("Enter your mark ");

scanf("%d",&num);

print f(" You entered %d", num); // print


ing outputs

if(num >= 80){

print f(" You got A grade"); // print ing


outputs

else if ( num >=60){ // Note the space


between else & if

print f(" You got B grade");


}

else if ( num >=40)

print f(" You got C grade");

else if ( num < 40){

print f(" You Failed in this exam");}

 Metre to kilometre convert:

#include <stdio.h>
#include<conio.h>
Void main
{
int m;
float km;
print f("Enter Meters:");
scanf("%d",&m);
km=m/1000.00;
print f("Kilometers: %f",km);
}

 C program to calculate gross salary of


an employee

#include <stdio.h>
#include<conio.h>
Int main()
{
float basic, gross, da, hra;
print f(“Enter basic salary of an
employee: “);
scanf(“%f”, &basic);
if(basic <= 10000)
{
da = basic * 0.8;
hra = basic * 0.2;
}
else if(basic <= 20000)
{
da = basic * 0.9;
hra = basic * 0.25;
}
else
{
da = basic * 0.95;
hra = basic * 0.3;
}
/* Calculate gross salary */
gross = basic + hra + da;
print f(“GROSS SALARY OF EMPLOYEE =
%f”, gross);
}

 AMSTRONG NUMBER:
#include <math.h>
#include <stdio.h>
#include<conio.h>
int main()
{
int num, originalNum, remainder, n = 0;
float result = 0.0;
print f(“Enter an int eger: “);
scanf(“%d”, &num);
originalNum = num;
// store the number of digits of num in n
for (originalNum = num; originalNum !=
0; ++n) {
originalNum /= 10;
}
for (originalNum = num; originalNum !=
0; originalNum /= 10) {
remainder = originalNum % 10;
// store the sum of the power of individual
digits in result
result += pow(remainder, n);
}
// if num is equal to result, the number is
an Armstrong number
if ((int )result == num)
print f(“%d is an Armstrong number.”,
num);
else
print f(“%d is not an Armstrong
number.”, num);
}

 Palindrome number:

#include<stdio.h>
#include<conio.h>
Int main()
{
int n,r,sum=0,temp;
print f(“enter the number=”);
scanf(“%d”,&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
print f(“palindrome number “);
else
print f(“not palindrome”);
}
 Reverse number:

#include<stdio.h>
#include<conio.h>
int main()
{
int num, rev = 0, rem;
print f(“Enter the number to reverse: “);
scanf(“%d”, &num);
while (num != 0){
rem = num % 10;
rev = rev * 10 + rem;
num = num/10;
}
print f(“The reversed number is: %d”,
rev);
}

Sum of 1 to 10 even number:


#include <stdio.h>
include <conio.h>
Int main()
{
Int s=0;
for(int i=2; i<=10; i+=2)
{
s+=i;
}
print f("Sum of 1st 10 even numbers =
%d",s);
}
 SUM OF 1 TO N 0DD NUMBER:

#include <stdio.h>
#include<conio.h>
Int main()
{
int i, n, sum=0;
print f("Enter upper limit: ");
scanf("%d", &n);
/* Find the sum of all odd number */
for(i=1; i<=n; i+=2)
{
sum += i;
}
print f("Sum of odd numbers = %d",
sum);
}

 LOOP IN C
PROGRAMME
Loops in programming are used to
repeat a block of code until the
specified condition is met. A loop
statement allows programmers to
execute a statement or group of
statements multiple times without
repetition of code.

 here are mainly two types of loops in C


Programming:
1. Entry Controlled loops: In Entry controlled loops
the test condition is checked before entering the
main body of the loop. For Loop and While
Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops
the test condition is evaluated at the end of the
loop body. The loop body will execute at least
once, irrespective of whether the condition is true
or false. do-while Loop is Exit Controlled loop.

Loop Type Description

first Initializes, then condition check, then executes the body


for loop
and at last, the update is done.

while first Initializes, then condition checks, and then executes the
Loop Type Description

loop body, and updating can be inside the body.

do-while do-while first executes the body and then the condition check is
loop done.

 for Loop

for loop in C programming is a repetition control


structure that allows programmers to write a loop that
will be executed a specific number of times. for loop
enables programmers to perform n number of steps
together in a single line.
In for loop, a loop variable is used to control the loop.
Firstly we initialize the loop variable with some value,
then check its test condition. If the statement is true
then control will move to the body and the body of for
loop will be executed. Steps will be repeated till the exit
condition becomes true. If the test condition will be false
then it will stop.
 Initialization Expression: In this expression, we
assign a loop variable or loop counter to some
value. for example: int i=1;
 Test Expression: In this expression, test
conditions are performed. If the condition
evaluates to true then the loop body will be
executed and then an update of the loop variable
is done. If the test expression becomes false then
the control will exit from the loop. for example,
i<=9;
 Update Expression: After execution of the loop
body loop variable is updated by some value it
could be incremented, decremented, multiplied, or
divided by any value.

 While Loop
While loop does not depend upon the number of
iterations. In for loop the number of iterations was
previously known to us but in the While loop, the
execution is terminated on the basis of the test
condition. If the test condition will become false then it
will break from the while loop else body will be executed.

 do-while Loop

The do-while loop is similar to a while loop but the only


difference lies in the do-while loop test condition which is
tested at the end of the body. In the do-while loop, the
loop body will execute at least once irrespective of the
test condition.

You might also like