0% found this document useful (0 votes)
25 views81 pages

C Chapter 1

C programming Unit -I

Uploaded by

pravin chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views81 pages

C Chapter 1

C programming Unit -I

Uploaded by

pravin chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

.

Introduction to C language
1.1 History
1.2 Basic structure of C Programming
1.3 Language fundamentals
1.3.1 Character set, tokens
1.3.2 Keywords and identifiers
1.3.3 Variables and data types
1.4 Operators
1.4.1 Types of operators
1.4.2 Precedence and associativity
1.4.3 Expression

.
➢ C is General Purpose programming language .

➢ C developed by Dennis Ritchie in 1972 at AT and T’s Bell


Laboratories of USA

➢ C is seems popular is because it is reliable, simple and easy to use.

.
Year Language Developed By Remark

1960 ALGOL International Too General


Committee
1963 CPL Cambridge University Hard to learn, difficult to
implement.
1967 BCPL Martin Richards Could deals with only
specific problem
1970 B Ken Thompson Could deals with only
specific problem

1972 C Dennis Ritchie Lost generality of BCPL


and B

.
➢ General Purpose Programming Language:

➢ Middle Level language

➢ Structured Programming:

➢ Simplicity: its structured approach.(keyword…)

➢ Portability: This refers to the ability of a program to run in


different environments.,

.
➢ Wide Acceptability: C is widely popular in the software industry

➢ Modularity: C Language programs can be divided into small modules

➢ Clarity: The features like keywords.. improve the clarity.

➢ Easy Debugging: The syntax errors can be easily detected by the C


compiler.

.
➢ Memory Management

➢ Recursion technique.

➢ Portability: This refers to the ability of a program to run in


different environments.

➢ Rich set of Library Functions: C has a rich set of library


functions. provide readymade functionality to the users. It also
supports graphic programming.

.
➢ A character denotes any alphabet, digit or special symbol used to
represent information. Valid alphabets, numbers and special
symbols allowed in C. The alphabets, numbers and special
symbols when properly combined form constants, variables and
keywords.
➢ A–Z
➢ a -z
➢ 0-9
➢ (space) . , : ; ' $ "
➢ # % & ! _ {} [] () $ & |
➢ +-/*=

.
➢ Keywords are the reserved words used in programming.
➢ Each keywords has fixed meaning and that cannot be changed by
user.
➢ For example: int money;
➢ Here, int is a keyword that indicates, 'money' is of type
integer.
➢ As, C programming is case sensitive, all keywords must be written
in lowercase.

➢ There are 32 Keywords in C Language.

.
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do If static while
default goto sizeof volatile

const float short unsigned

.
➢ In C programming, identifiers are names given to C entities, such
as variables,functions, structures etc.
➢ Identifier are created to give unique name to C entities to identify
it during the execution of program.
➢ For example: int money; int mango_tree;
➢ Here, money is a identifier which denotes a variable of type
integer.
Similarly, mango_tree is another identifier, which denotes another
variable of type integer.
➢ They must begin with a alphabet or underscore(_).
➢ They must consist of only letters, digits, or underscore. No other
special character is allowed.
➢ It should not be a keyword.
➢ It must not contain white space or Blank Space.
➢ It should be up to 31 characters long as only first 31 characters are
significant.

.
Variables are memory location in computer's memory to store data.

To indicate the memory location, each variable should be given a unique name
called identifier.

Variable names are just the symbolic representation of a memory location.

Examples of variable name: sum, car_no, count etc.


int num;
Here, num is a variable of integer type.

.
➢ Variable name can be composed of letters (both uppercase and lowercase
letters), digits and underscore '_' only.

➢ The first letter of a variable should be either a letter or an underscore.

➢ There is no rule for the length of length of a variable. However, the first 31
characters of a variable are discriminated by the compiler. So, the first 31
letters of two variables in a program should be different.

.
➢ Constants are the terms that can't be changed during the
execution of a program.
➢ For example: 1, 2.5, "Programming is easy." etc.

➢ Constant Classified are-

1. Integer constant

2. Floating point Constant

3. Character Constant

.
1)Integer constants-

1. Integer constants are the numeric constants(constant associated with number)


without any fractional part or exponential part.
2. There are three types of integer constants in C language:
(decimal constant(base 10),
octal constant(base 8)
hexadecimal constant(base 16))
-> Decimal digits: 0 1 2 3 4 5 6 7 8 9
->Octal digits: 0 1 2 3 4 5 6 7
-> Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
.
2) Floating-point constants

1. Floating point constants are the numeric constants that has either
fractional form or exponent form.
For example:
-2.0
0.0000234
-0.22E-5
3)Character constants

Character constants are the constant which use single quotation around
characters. For example: 'a', 'l', 'm', 'F' etc.

.
➢ Data Type means which type of data used in program,.
➢ According to type of data the memory is allocated to variable.

Data type Byte Example

char 1
char a;

int 2 int Number1;


float 4 float salary;
double money;
double 8
void Null

.
Declaration of Variables:-

Synatx:-

Datatype variable1 ,variable2 ;

for e.g.

int a; it means a is a variable of type integer and it allocates


2 byte memory.
char c; it means c is a variable of type character and it
allocates 1 byte memory.
float f; it means f is a variable of type float and it allocates 4 byte
memory.
Variable initialization :-

When we assign any initial value to variable during the declaration, is


called initialization of variables.
When variable is declared but contain undefined value then it is called
garbage value.
The variable is initialized with the assignment operator such as

Syntax : Datatype variablename=value;

Example: int a=20; int b=50;


char ch=‘A’;
float f=10.5;
Operators are the symbol which operates on value or a variable.

Types of operators
1. Arithmetic Operators
2.Relational Operator
3.logical operators
4.increment /decrement operator
5.Assignment operator
6.Bitwise operator
7.Conditional operator
8.Compound Assignment Operators

Category of operators:- Unary operator


Binary Operator

.
Operator Meaning of Operator
+ addition or unary plus
subtraction or unary
-
minus
* multiplication
/ division
remainder after
% division( modulo
division)

int a=10,b=5;
f=a / b f=10/5 f= 2
g= a%b g= 0
.
Relational operators checks relationship between two operands. If the
relation is true, it returns value 1 and if the relation is false, it returns
value 0.
For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not
then, it returns 0.
Relational operators are used in decision making and loops in C
programming.

.
Operator Meaning of Operator Example

== Equal to 5==3 returns false (0)

> Greater than 5>3 returns true (1)

< Less than 5<3 returns false (0)

!= Not equal to 5!=3 returns true(1)

>= Greater than or equal to 5>=3 returns true (1)

<= Less than or equal to 5<=3 return false (0)


.
Logical operators are used to combine expressions containing relation

Operator Meaning of Operator

&& Logial AND

|| Logical OR

! Logical NOT

.
Condition1 Condition2 Output (Logical AND( && ))
F F F
F T F
T F F
T T T

Condition1 Condition2 Output (Logical OR( || ))


F F F
F T T
T F T
T T T

Condition1 Output Logical NOT( ! )

F T

T F
Example
1) Logical && (AND)
int a,b,c,d;
a=10;b=20;c=30;
d= a<b && b<c; 10<20 && 20<30 T(1) && T(1) T(1)
Value of d will be 1 (true)
d=a>b && b<c ; 10>20 && 20<30 F(0) && T(1) F(0)
value of d will be 0 (false)
2) Logical || (OR)
int a,b,c,d;
a=10;b=20;c=30;
d= a<b || b<c; 10<20 || 20<30 T(1) || T(1) T(1)
Value of d will be 1 (T)
d=a>b || b<c 10>20 || 20<30 F(0) || T(1) T(1)
value of d will be 1 (T)
3) Logical ! (NOT)
a=10; b=20;
c= ! a<b; 10<20 T(1) c= F(0)
Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement(NOT)

<< Shift left

>> Shift right

.
Bitwise AND Operator

▪ The & (bitwise AND) operator compares each bit of its first operand to
the corresponding bit of the second operand. If both bits are 1's, the
corresponding bit of the result is set to 1. Otherwise, it sets the
corresponding result bit to 0.

▪ The bitwise AND (&) should not be confused with the logical AND. (&&)
& operator.
▪ Logical table of Bitwise AND

Operand 1 Operand 2 Operand 1& operand 2

0 0 0
0 1 0
1 0 0
1 1 1

28/46
Suppose int n1=20;
int n2=15;

We have to perform n1 & n2


First convert this n1 and n2 into binary number.

n1=20 = 0000 0000 0001 0100 (int allocates 2 bytes means 16 bits)

n2=15 = 0000 0000 0000 1111

n1 0000 0000 0001 0100


&
n2 0000 0000 0000 1111
------------------------------------------------------
0000 0000 0000 0100 =4
n1&n2=15&20=4
29/46
Bitwise (Inclusive) OR Operator
▪ The | (bitwise inclusive OR) operator compares the values (in binary
format) of each operand and yields a value whose bit pattern shows
which bits in either of the operands has the value 1. If both of the bits are
| 0, the result of that bit is 0; otherwise, the result is 1.

▪ Logical Table of Bitwise OR ( | )

Operand 1 Operand 2 Operand 1 | operand 2


0 0 0
0 1 1
1 0 1
1 1 1

32/46
Suppose int n1=20;
int n2=15;

We have to perform n1 | n2
First convert this n1 and n2 into binary number.

n1=20 = 0000 0000 0001 0100 (int allocates 2 bytes means 16 bits)

n2=15 = 0000 0000 0000 1111

n1 0000 0000 0001 0100


|
n2 0000 0000 0000 1111
------------------------------------------------------
0000 0000 0001 1111 = 31
n1|n2=15|20=31
29/46
Bitwise XOR Operator
▪ The bitwise exclusive OR operator (in EBCDIC, the ^ symbol is
represented by the ¬ symbol) compares each bit of its first
operand to the corresponding bit of the second operand. If both
^ bits are 1's or both bits are 0's, the corresponding bit of the result
is set to 0. Otherwise, it sets the corresponding result bit to 1.

Operand 1 Operand 2 Operand 1 ^ operand 2

0 0 0
0 1 1

1 0 1
1 1 0
30/46
Suppose int n1=20;
int n2=15;

We have to perform n1 ^ n2
First convert this n1 and n2 into binary number.

n1=20 = 0000 0000 0001 0100 (int allocates 2 bytes means 16 bits)

n2=15 = 0000 0000 0000 1111

n1 0000 0000 0001 0100


^
n2 0000 0000 0000 1111
------------------------------------------------------
0000 0000 0001 1011 = 27
n1^n2=15^20=27
29/46
Bitwise (complement) NOT Operators
The ~ (bitwise negation) operator yields the bitwise (one)
complement of the operand. In the binary representation of the
result, every bit has the opposite value of the same bit in the binary
~ representation of the operand. The operand must have an integral
type. The result has the same type as the operand but is not an
lvalue (left value). The symbol used called tilde.

Operand 1 ~ Operand
0 1
1 0

25/46
Suppose int n1=15;

We have to perform ~n1


First convert this n1 and n2 into binary number.

n1=15 = 0000 0000 0000 1111

n1 0000 0000 0001 0100


~
------------------------------------------------------
1111 1111 1110 1011
~n1=

29/46
Bitwise Shift Operators
Left shift operator, shift their first operand left (<<) by the
<< nbits << nshiftSize
number of positions specified by the second operand.

Right shift operator, shift their first operand right (>>) by the
>> nbits >> nshiftSize
number of positions specified by the second operand.
▪ Both operands must be integral values. These operators perform the usual arithmetic conversions; the
type of the result is the type of the left operand after conversion.
▪ For leftward shifts, the vacated right bits are set to 0. For rightward shifts, the vacated left bits are filled
based on the type of the first operand after conversion. If the type is unsigned, they are set to 0.
Otherwise, they are filled with copies of the sign bit. For left-shift operators without overflow, the
statement:

expression1 << expression2

▪ is equivalent to multiplication by 2expression2. For right-shift operators:

expression1 >> expression2

▪ is equivalent to division by 2expression2 if expression1 is unsigned or has a nonnegative value.


▪ The result of a shift operation is undefined if the second operand is negative, or if the right operand is
greater than or equal to the width in bits of the promoted left operand.
▪ Since the conversions performed by the shift operators do not provide for overflow or underflow
conditions, information may be lost if the result of a shift operation cannot be represented in the type of
the first operand after conversion. 26/46
Suppose int n=15;

We have to perform n<<3


First convert this n
n=15 = 0000 0000 0000 1111

n=15 = 0000 0000 0000 1111


0000 0000 0011 1100
n<<3 = 0000 0000 0111 1000

------------------------------------------------------
n= 0000 0000 0111 1000 = 120

29/46
Suppose int n=15;

We have to perform n>>3


First convert this n
n=15 = 0000 0000 0000 1111

n=15 = 0000 0000 0000 1111


0000 0000 0011 1100
n>>3 = 0000 0000 0111 1000

------------------------------------------------------
n= 0000 0000 0111 1000 = 120

29/46
➢ ++ is a increment operators.
➢ -- is a decrement operators.
➢ Both of these operators are unary operators, i.e, used on single
operand.
➢ ++ adds 1 to operand and -- subtracts 1 to operand respectively.
➢ For example:
Let a=5 ;
b=10;
a++; //a becomes 6
a--; //a becomes 5
++a; //a becomes 6
--a; //a becomes 5

.
➢ When i++ is used as prefix(like: ++var),
➢ ++var will increment the value of var and then
return it
➢but, if ++ is used as postfix(like: var++),
➢operator will return the value of operand first and
then only increment it
➢Conditional operator takes three operands and consists of two
symbols ? and : . Conditional operators are used for decision
making in C.

➢Condition?true:flase;

➢ For example:
c=(c>0)?10:-10;

➢If c is greater than 0, value of c will be 10 but, if c is less than


0, value of c will be -10.
The most common assignment operator is =.
This operator assigns the value in right side to the left side.
For example: var=5 //5 is assigned to var
a=c; //value of c is assigned to a
5=c; // Error! 5 is a constant.

Operator Example Same as


= a=b a=b

+= a+=b a=a+b

-= a-=b a=a-b

*= a*=b a=a*b

/= a/=b a=a/b

%= a%=b a=a%b
➢Comma operators are used to link related expressions
together.

➢ For example:
int a,c=5,d;
Structure Of C program:-
Header File Declaration
void main( )
{
Statements;
}

.
Ex: 1.C Program to Print a Sentence

void main()
{
printf("C Programming");
}
C Program to Add Two Integers Entered by User
/*C programming source code to add and display the sum of two integers entered by user
*/
#include <stdio.h>
int main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2); /* Stores the two integer entered by
user in variable num1 and num2 */
sum=num1+num2; /* Performs addition and stores it in variable sum */
printf("Sum: %d",sum); /* Displays sum */
return 0;
}

Output
Enter two integers: 12
11
Sum: 23
C Program to Swap Two Numbers
#include <stdio.h> printf("\nAfter swapping,
int main(){ value of a = %.2f\n", a);
float a, b, temp; printf("After swapping,
printf("Enter value of a: "); value of b = %.2f", b);
scanf("%f",&a); return 0;
printf("Enter value of b: "); }
scanf("%f",&b);
temp = a;
/* Value of a is stored in variable temp */
a = b; Output
Enter value of a: 1.20
/* Value of b is stored in variable a */ Enter value of b: 2.45
b = temp; After swapping, value of a = 2.45
/* Value of temp(which contains initial After swapping, value of b = 1.2
value of a) is stored in variable b*/
Control Statements.

Control statements enable us to specify the flow of


program control; ie, the order in which the instructions in a
program must be executed. They make it possible to make
decisions, to perform tasks repeatedly or to jump from one
section of code to another.
Control
Statement

Conditional/Decision Looping Jumping


Making Statement Statement Statement

If If else switch for while do while break continue goto

.
Decision making with if statement

➢ Decision making are needed when, the program encounters


the situation to choose a particular statement among many
statements. In C, decision making can be performed with
following statements.

• if statement
• if else Statement
• switch statement
if statement
Syntax:
if (Condition)
{
statement/s to be executed if Condition is true;
}
➢ If the Condition is true then, statements for the body if, i.e, statements inside
parenthesis are executed.
➢ But, if the test expression is false, the execution of the statements for the body
of if statements are skipped.
Decision making with if statement
➢ Example:
void main()
{
int num;
clrscr();
printf(“Enter any number”);
scanf(“%d”,&num);
if(num>0)
{
printf("%d is Positive\n",num);
}
getch();
}
Decision making with if...else statement
➢ The if...else statement is used, if the programmer wants to execute some code, if
the test expression is true and execute some other code if the test expression is
false.
Syntax of if...else :
if (Condition)
statements to be executed if test expression is true;
else
statements to be executed if test expression is false;
Decision making with if-else statement
➢ Example:
void main()
{
int num;
clrscr();
printf(“Enter any number”);
scanf(“%d”,&num);
if(num>0)
{
printf("%d is Positive\n",num);
}
else
{
printf("%d is Negative\n",num);
}
getch();
}

.
Decision making with Nested if...else statement
➢ The if...else statement can be used in nested form when a serious decision are
involved.
➢ Syntax of nested if...else statement.:

if (test expression)
statements to be executed if test expression is true;
else if(test expression 1)
statements to be executed if test expressions 1 is true;
else if (test expression 2)
.
.
.
else
statements to be executed if all test expressions are false;

.
Decision making with Nested if...else statement
➢ Example:
void main()
{
int num;
clrscr();
printf(“Enter any number”);
scanf(“%d”,&num);
if(num>0)
{
printf("%d is Positive\n",num);
}
else if(num<0)
{
printf("%d is Negative\n",num);
}
else
{
printf("%d is Zero\n",num);
}
getch();
}
.
switch....case Statement
Decision making are needed when, the program encounters the situation to choose a particular
statement among many statements. If a programmer has to choose one among many alternatives
if...else can be used but, this makes programming logic complex.

• Syntax of switch...case:

switch (Option)
{
case Option1 :
Statements;
break;
case Option2 :
Statements;
break;
[ Default :]
codes to be executed if Option doesn't match to any
}
.
switch....case Statement
Example-
Option=4;
switch(option)
{
case 1:
printf(“ONE”);
break;
case 2:
printf(“TWO”);
break;

case 3:
printf(“THREE”);
break;
case 4:
printf(“FOUR”);
break;
Default : printf(“Enter Valid Option”);
}
.
switch....case Statement
Example-
switch(operator)
{
case'+':
printf("num1+num2=%.2f",num1+num2)
;break;
case'-':
printf("num1-num2=%.2f",num1-num2);
break;
case'*':
printf("num1*num2=%.2f",num1*num2)
;break;
case'/':
printf("num2/num1=%.2f",num1/num2)
;break;
default:/* if operator is other than +, -, * or /, error message is
shown */ printf(Error!operatorisnot correct");
break;
}
.
C Program to Check Vowel or Consonant
/* Source Code to Check Whether a Character is Vowel or consonant*/
#include <stdio.h>
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);

if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c==
'o'||c=='O'||c=='u'||c=='U')
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}
Output 1
Enter an alphabet: i
i is a vowel.
Output 2
Enter an alphabet: G
G is a consonant.
.
C Program to Check whether a year is leap year or not

/* C program to check whether a year is leap year or not using if else


statement.*/

#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else

.
C Program to Check whether a year is leap year or not
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}

Output 1
Enter year: 2000
2000 is a leap year.
Output 2
Enter year: 1900
1900 is not a leap year

.
Source Code to Make Simple Calculator in C programming
Example-
# include <stdio.h>
int main()
{
char operator;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&operator);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f",num1+num2);
break;
case '-':
printf("num1-num2=%.2f",num1-num2);
break;

.
case '*':
printf("num1*num2=%.2f",num1*num2);
break;
case '/':
printf("num2/num1 = %.2f",num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
Output
Enter operator either + or - or * or divide: /
Enter two operands: 13.456
4.56
num2/num1 = 2.95

.
4.Iterative/Looping Statements.

.
Looping Statements

➢ C programming loops:
➢ Loops causes program to execute the certain block of code repeatedly until some
conditions are satisfied, i.e., loops are used in performing repetitive work in programming.

➢ There are 3 types of loops in C programming:


1. for loop
2. while loop
3. do...while loop

.
Loops: for.

➢ for Loop Syntax


for(initialization ; condition ; updation)
{
code/s to be executed;
}
➢ How for loop works in C programming?
➢ The initial expression is initialized only once at the beginning of the
for loop.
➢ Then, the test expression is checked by the program.
➢ If the test expression is false, for loop is terminated.
➢ But, if test expression is true then, the codes are executed and
update expression is updated. Again, the test expression is checked.

.
For loop

Example of For loop

for(count=1;count<=5;++count) //for loop terminates if count>n


{
sum+=count; /* this statement is equivalent to sum=sum+count */
}
.
Loops: while
➢ Syntax of while loop
Intialization
while (Condition)
{
statements to be executed.
Updation;
}
➢ In the beginning of while loop, test expression is checked.
➢ If it is true, codes inside the body of while loop,i.e, code/s inside parentheses
are executed and again the test expression is checked and process continues
until the test expression becomes false.

.
Loops: while
➢ Example of while loop

i=1;
While(i<=n)
{
printf(“%d”,i);
i++;
}

1 2 3 4 5 6 7 8 9 10

.
do...while loop

➢ In C, do...while loop is very similar to while loop.


➢ Only difference between these two loops is that, in while loops, test
expression is checked at first but, in do...while loop code is executed
at first then the condition is checked.
➢ So, the code are executed at least once in do...while loops.
➢ Syntax of do...while loops
Initialization
do {
some code/statements;
}
while (Condition);

➢ At first codes inside body of do is executed. Then, the test


expression is checked. If it is true, code/s inside body of do are
executed again and the process continues until test expression
becomes false(zero).
➢ Notice, there is semicolon in the end of while (); in do...while loop.
.
do...while loop

Example of do- while loop

do
/* Codes inside the body of do...while loops are at least executed
once. */
{
printf("Enter a number\n");
scanf("%d",&num);
sum+=num;
}
while(num!=0);
.
break Statement:
break Statement:
In C programming, break is used in terminating the loop immediately after it is encountered. The
break statement is used with conditional if statement.

Syntax of break statement


break;
The break statement can be used in terminating all three loops for, while and do...while loops.
break Statement:

break Example:

for(i=1;i<=n;++i)
{
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}

.
continue Statement

continue Statement
➢ It is sometimes desirable to skip some statements inside the loop. In such
cases, continue statements are used.

➢ Syntax of continue Statement


continue;

.
continue Statement

Example:

if(num==0)
continue; / *In this program, when num equals to zero, it
skips the statement product*=num and continue the loop. */
product*=num;

.
Source Code to C Program to Reverse an Integer
/*C Program to Reverse an Integer*/
#include <stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number = %d",reverse);
return 0;
}
Output
Enter an integer: 2345
Reversed Number = 5432

.
Source Code to c Program Sum of Integers
/* This program is solve using for loop. */

#include <stdio.h>
int main()
{
int n, count, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
for(count=1;count<=n;++count) /* for loop terminates if count>n */
{
sum+=count; /* sum=sum+count */
}
printf("Sum = %d",sum);
return 0;
}
Output
Enter an integer: 100
Sum = 5050

.
Goto statement

goto label; statement;

label:
statement;

e.G
goto a;
printf(“hello”);

a: printf(“Welcome”);

.
Thank You

You might also like