ARVIND SIR’S NOTES IN C PROGRAMMING PROGRAMMING QUESTION BANK ANSWERS
Explain C tokens (2 marks)
Tokens are a re basic building blocks of a C program. A token is the smallest element ele ment of a C program that is meaningful to the compiler. A token is source-program text that the compiler does not break down The C compiler recognizes the following kinds of tokens:
keywords : these are reserved identifiers having predefined meanings identifiers : these tokens name functions, variables, constants, and types literals : these tokens that specify values operators : these tokens are used to combine values in expressions punctuation : these tokens separate or terminate c omplex constructions me aning to the preprocessor or compiler special : these tokens have special meaning
A token can be a reserved word (such as int or while), an identifier (such as b or sum), a constant (such as 3.14 or "Arvind Kumar"), a delimiter (such as { or ;) or an operator (such as + or =). Example: Consider the following program:
mai n( ) { i nt r =10, ar ea; ar ea = 3. 14 * r * r ; pr i nt f ( "are "area a of ci r cl e = %d\ n", ar ea) ; } The tokens in this program are: are: main ( ) { int r = 10 , area ; 3.14 *
-
identifier left bracket, delimiter right bracket, delimiter left brace, delimiter reserved word identifier equals sign, operator constant comma, delimiter identifier semicolon, delimiter constant asterisk, operator
and so on. Thus a C program is a ‘stream of tokens’
1
Explain C Keywords What is a keyword? State two keywords of C
Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are :
aut o br ea eak k case ca se char ch ar con co nst con co nt i nue def aul t do
doubl e el se enum ext er n f l oat f or got o if
i nt l ong r egi st er r et ur n sho sh or t si gned s i z eo eof stati c
st r uct swi t ch t yp ype edef uni on unsi gned voi d vol at i l e vo whi l e
All 32 keywords are in lower case letters. These keywords are reserved and cannot be redefined. Following rules must be kept in mind when using keywords.
Keywords are case sensitive. For F or example, return is a keyword and it must be used as is. So it cannot be used as Return, or RETURN. The keywords have special meaning in the language and cannot be used for any other purpose such as constant name or variable name.
State the use of %d and %f . Write a print statement in C using above mentioned symbols
%d and %f are conversion specifiers. They are used with the control string in output function printf() and input function scanf() Consider the following statement
pr i nt f ( “r adi us=%d , ar ea= %f ”, r , a) ; The c ont r ol describes how the value of n will o l - s t r i ng uses %d conversion specifier. It describes be printed. The following example shows use of %d. In this example %d is used in scanf() statement statement to accept an integer value. value . Again %d is used in the printf() statement to print the integer value.
#i ncl ude mai n( ) { i nt a; a; pr i nt f ( “P “Pll ease ent er an integer ” ) ; scanff ( “%d”, &a) scan pr i nt f ( “y “yo ou en ent er ed t he va val ue %d”, a) ; } The next example shows use of % f . In this example %f is used in scanf() statement to accept a float value. Again %f is used in the printf() statement to print the float value.
2
#i ncl ude mai n( ) { i nt b; pr i nt f ( “Pl ease ent er a f l oat number ”) ; scanf ( “%f ”, &b) pr i nt f ( “you ent er ed t he val ue %f ”, b) ; }
Define expressions
Expressions: Expression is a single value or a combination of values and variables which are connected by operators. Following are examples of expression:
a +b - c ; x =a ; x >6 ; The first expression uses arithmetic operators + and – , the second expression uses the assignment operator =, and the third expression uses relational operator >. Expression can also be a logical condition that is true or false. In C the true and false condition are represented by integer value 1 and 0 respectively.
What are operators?
Operators: Operators are tokens used to combine values in an expression. The following are examples of operators used in an expression:
a +b - c ; x =a ; x >6 ; The first expression uses arithmetic operators + and – , the second expression uses the assignment operator =, and the third expression uses relational operator >. Some operators perform operation on two operands, while others perform operation on only one operand.
Explain the various operators used in C Explain the bit wise operators in C Explain the logical operators used in C What is an operator? Explain unary and binary operators. Explain the increment and decrement operators. State four arithmetic operators and four logical operators of C
The operators used in C are divided into following types:
Arithmetic Operators Relation Operator Logical Operator
3
Arithmetic Operators: The operators used for basic arithmetic are =, +, - , * , and / . Operator – + * / %
Meaning subtraction addition multiplication division modulus
The operator % is used in integer arithmetic. It is called modulus operator. It gives the remainder when the integer to its left is divided by the integer to its right. Example: 13 % 5
i s r ead as "13 modul o 5" and i t has t he val ue 3
The modulus operation gives remainder of an integer division. Hence % cannot be used in type float or double.
Unary Operators: These operators perform an operation on a single variable and give a new value. The unary operators are:
Operat or – + ++ – – sizeof
Meaning negative value positive value increment decrement size of variable in memory
Unary Minus: This operator is used to negate a numerical constant, variable or a expression. Examples:
– 35
– 2. 5
– ( a * b)
– a + 35
Increment operator: The operator ++ will increase the value of its operand by 1.. Decrement operator: The operator – – will decrease the value of its operand by 1. Sizeof operator: This operator will return the size of the operand. The operand can be an expression.
Example:
#i ncl ude mai n( ) { i nt p=10; f l oat q = 2. 5 ; pr i nt f ( “ t he s i z e of p i n byt e i s %d” , s i z eof ( p) ) ; pr i nt f ( “ t he s i z e of q i n byt e i s %d” , s i z eof ( q) ) ; } 4
The output of this will be
t he si ze of p i n byt e i s 2 t he si ze of q i n byt e i s 4
Bitwise Boolean Operators: The six bit wise operators used in C are given below. The bit wise operators work bit by bit on operands. The operands must be of inte gral type. & | ^ ~ << >>
AND OR XOR (exclusive OR) NOT ( changes 1 to 0 and 0 to 1) Shift left Shift right
Logical Operators: The logical operators used in C are given below. && || !
logical AND logical OR NOT
if any one operand is false, the result is false if any one operand is true, the result is true if the operand is true, the result is false and vice versa
The operators && and || are binary operators i.e. the logical operators require two operands. The ! operator is a unary operator. Consider operands A and B. The truth table for && and || operators are g iven below
A false false true true
B false true false true
A && B false false false true
A false false true true
B false true false true
A ||B false true true true
These logical operators are used to combine or negate expression containing relational operators. Here's an example of a logical expression.
r = ( ( a&&b) | | ( c>d) ) ; In the example: r is set equal to 1 if a and b are nonzero, or if c is greater than d. In all other cases, r is set to 0.
Define/Explain Data Types
Every variables used in a C program is defined with a specific type. In Standard C there are four basic data types. They are int, char, float, and double.
char i nt f l oat doubl e
-
char act er s i nt eger s ( whol e numbers ) r eal number s hi gher pr eci si on r eal number s 5
In addition, C provides structured types:
ar r ay st r uct s uni ons
- gr oups of var i abl es of i dent i cal t ypes, accessed usi ng i nt eger i ndi ces - gr oups of var i abl es of mi xed t ypes, accessed by usi ng named f i el d sel ect or s - var i abl es t hat can cont ai n val ues of di f f er ent t ypes, dependi ng on a f i el d sel ect or
C also allows enumerated types - variables that can take on a small number of different named values
Explain the break statement
The break statement is used in loop statements (for, while, and do-while) . It is possible to force an immediate exit from a loop by using the break statement. A break statement is formed with the keyword break followed by a semicolon.
Example :
#i ncl ude mai n( ) { i nt x; f or ( x=1; x<=100; x++) { i f ( x==10) br eak; pr i nt f ( “ %d” , x) ; } pr i nt f ( “l oop t er mi nat ed”) ; } When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. Break is also used in switch statements to terminate execution of statement. When the break statement is executed, execution proceeds to the statement that follows switch statement.
Example :
#i ncl ude mai n( ) { i nt n; pr i nt f ( “\ nEnt er a number bet ween 1 t o 3”) ; scanf ( “%d”, %n) ; swi t ch ( n) case 1 : pr i nt f ( “you ent er ed 1”) ; br eak; case 2 : pr i nt f ( “you ent er ed 2”) ; 6
br eak; case 3 : pr i nt f ( “you ent er ed 3”) ; br eak; def aul t : pr i nt f ( “wr ong choi ce”) ; }
Explain the continue statement
It is possible to bypass the loops normal control structure and force an early iteration of a loop and. This is accomplished by using continue. The continue statement is used in loop statements (for, while, and do-while) to terminate iteration of the loop. The next iteration of loop to takes place, skipping code between itself and conditional expression that controls the loop. A continue statement is formed with the keyword continue followed by a se micolon.
Example: The following program prints even numbers between 0 and 100
#i ncl ude mai n( ) { i nt x; f or ( x=0; x<=100; x++) { i f ( x%2! =0) cont i nue; pr i nt f ( “ %d” , x) ; } After a continue statement is executed, execution proceeds to increment clause and continuation test of for loop. Hence only even numbers are printed because an odd one will cause the loop to iterate early. The continue statement can be used in while and do while loops. It will cause control to go directly to the conditional expression and then continue the looping process. The continue statement is useful when data in body of loop is bad, out of bounds or unexpected. Instead of acting on bad data, control can go back to top of loop and get another data value.
Give the selection process of the switch statement Explain switch case statement with syntax
The switch statement is used to select and execute a particular group of statement from several available groups. The selection is based on the value of an expression or variable The syntax of the switch statement is:
swi t ch ( expr essi on or var i abl e) { case const ant 1 : st at ement - bl ock1; br eak; 7
case const ant 2 : st at ement - bl ock2; br eak; . . . case const ant N : st at ement - bl ockN; br eak; case def aul t : st at ement - bl ock3; }
where expression is of simple type such as int, char, or enum . It cannot have float or double type. When switch statement is executed, the expression is evaluated. The resulting value is compared to values of constants 1 through N in order until a matching value is found. If a match is found in constant i then statements-block i through N and the default statements will be executed. Normally, last statement in each statement-block is a break statement so that only one statement-block is executed. The default clause is optional. If it is present then the default-statements are executed whenever value of expression does not match the constant values.
Example: The program defines variable v of char type. The program takes input and stores it in variable v. The program will check the value of v and match it with the several cases. If the case matches the corresponding statement is executed.
#i ncl ude mai n( ) { char v; pr i nt f ( “\ nEnt er a vowel : ”) ; scanf ( “%d”, %c) ; swi t ch ( v) case ‘ a’ : pr i nt f ( “you ent er ed a”) ; br eak; case ‘ e’ : pr i nt f ( “you ent er ed e”) ; br eak; cas e ‘ i ’ : pr i nt f ( “ you ent er ed i ” ) ; br eak; case ‘ o’ : pr i nt f ( “you ent er ed o”) ; br eak; case ‘ u’ : pr i nt f ( “you ent er ed u”) ; br eak; def aul t : pr i nt f ( “you di d not ent er a vowel ”) } Example : In this example, the switch statement will allows execution of different statements depending on the value of n entered by the user.
#i ncl ude mai n( ) { i nt n; 8
pr i nt f ( “\ nEnt er a number bet ween 1 t o 3”) ; scanf ( “%d”, %n) ; swi t ch ( n) case 1 : pr i nt f ( “you ent er ed 1”) ; br eak; case 2 : pr i nt f ( “you ent er ed 2”) ; br eak; case 3 : pr i nt f ( “you ent er ed 3”) ; br eak; def aul t : pr i nt f ( “wr ong choi ce”) }
Explain the for-statement. Give the syntax of for-statement
The for statement is a looping construction. It has the following form:
f or ( i ni t i al i z at i on; condi t i on; i ncr ement ) { st at ement s } where
initialization is a statement that is executed once at beginning of for loop. condition is an expression that can be true (nonzero) or false (zero). This expression is tested prior to each iteration of the loop. The loop terminates when it is false. increment is a statement that is executed after statements. statements is a sequence of statements. If there is only one statement then the braces may be omitted.
Example: The following program will print the numbers 1 to 10 in reverse order
#i ncl ude mai n( ) { i nt i ; f or ( i =10 ; i >=0 ; i - - ) pr i nt f ( “ %d\ n” , i ) ; } Example: The following program will print the sum of numbers from 1 to 10
#i ncl ude mai n( ) { i nt i , s um = 0 ; f or ( i =1 ; i <=10 ; i ++) sum=sum + i ; pr i nt f ( “sum = %d”, sum) ; } Example: The following program will print the sum of all even numbers from 1 to 100 9
#i ncl ude mai n( ) { i nt i , s um = 0 ; f or ( i =2 ; i <=100 ; i =i +2) sum=sum + i ; pr i nt f ( “s um of even number s = %d”, sum) ; } Example: The following program will print the first 10 odd numbers
#i ncl ude mai n( ) { i nt i ; f or ( i =1 ; i <=20 ; i =i +2) pr i nt f ( “ %d\ n” , i ) ; }
Explain the syntax of while statement
The while statement is a looping construction which has the following form:
whi l e ( l oop condi t i on) { st at ement bl ock; } where:
loop condition is an expression that can be true (nonzero) or false (zero). This expression is tested prior to each iteration of the loop. The loop terminates when it is false. When the condition is false, c ontrol will go to the first statement after the curly bracket ‘ }’ statements is a sequence of statements. If there is only one statement then the braces can be omitted.
Example: The following program will print the first 10 natural numbers. The loop is executed 10 times.
#i ncl ude mai n( ) { i nt i =1 ; whi l e ( i <=10) { pr i nt f ( “ %d\ n” , i ) ; i ++; } }
Explain : Do-While Loops
The do-while statement is a looping construction. It has following form: 10
do { st at ement bl ock ; } whi l e ( l oop condi t i on) ; where
statements is a sequence of statements. If there is only one statement then the braces may be omitted. condition is an expression that can be true (nonzero) or false (zero). This expression is tested after each iteration of the loop. The loop terminates w hen it is false.
Example: The following program will print the first 10 natural numbers. The loop is executed 10 times.
#i ncl ude mai n( ) { i nt i =1 ; do { pr i nt f ( “ %d\ n” , i ) ; i ++; } whi l e ( i <=10) }
Give the syntax of the simple if- statement
The simplest form of if statement is as follows:
i f ( c ondi t i on) st at ement ;
The if keyword must be followed by a set of parentheses containing the expression to be tested. The parentheses is followed by a single statement which is executed only if the test expression evaluates to true.
Example: The following program uses the simple if statement:
#i ncl ude mai n( ) { i nt n; pr i nt f ( “Ent er a posi t i ve number l ess t han 10”) ; scanf ( “%d”, &n) ; i f ( n<=10) pr i nt f ( "Thank you”) ; i f ( n>10) pr i nt f ( " Sor r y. The number i s gr eat er t han 10”) ; }
Describe the if-else statement 11
The if statement has one of two forms:
i f ( condi t i on) { t r ue- st at ement s } or
i f ( condi t i on) { t r ue- st at ement s } el se { f al se- st at ement s } where
condition is an expression that can be true (nonzero) or false (zero). true-statements and false-statements are sequences of statements. If there is only one statement in a sequence then the surrounding braces may be omitted. The second form includes the else clause For both forms, true-statements are executed only if condition is true. For second form the false-statements are executed if condition is false.
Example : The following statement is used to test whether a number entered by the user is positive or negative
#i ncl ude voi d mai n ( ) { i nt n ; pr i nt f ( " Ent er a non- zer o number ") ; scanf ( " %d", &n) ; i f ( n < 0) pr i nt f ( "number i s negat i ve") ; el se pr i nt f ( "The number i s posi t i ve") ; } Example: Finding the greater of two numbers
#i ncl ude voi d mai n ( ) { i nt a, b ; pr i nt f ( "Ent er a number ") ; scanf ( " %d" , &a) ; pr i nt f ( "Ent er anot her number ") ; scanf ( " %d" , &b) ; i f ( a>b) 12
pr i nt f ( "%d i s gr eat er t han %d", a, b) ; el se pr i nt f ( "%d i s gr eat er t han %d", a, b) ; }
State four forms of the if statement and explain any two
The simplest form of if statement is as follows:
i f ( c ondi t i on) st at ement ; The gener al f or m of t he i f st at ement i ncl udes t he el se cl ause. Thi s has t he f ol l owi ng f or m: i f ( c ondi t i on) st at ement ; el se st at ement ; If the programmer wants to execute more than one statement at this point, they may be grouped using curly braces. Such statement group is called compound statement.
i f ( c ondi t i on) { st at ement ; st at ement ; } In most general form of the if statement is :
i f ( c ondi t i on) { st at ement ; st at ement ; } el se { st at ement ; st at ement ; } Example: The f ol l owi ng exam pl e uses i f
st at ement t o f i nd t he gr eat est of
t hr ee number s ent er ed by t he user . #i ncl ude mai n ( ) { i nt a, b, c, bi g ; pr i nt f ( "Ent er t hr ee number s" ) ; scanf ( " %d %d %d", &a, &b, &c) ; i f ( a > b) && ( a > c) bi g = a ; 13
el se { i f ( b > c) bi g = b ; el se bi g = c ; } pr i nt f ( "l ar gest number i s %d", bi g)
}
Explain the nesting of if-statement
The if statement can itself contain another if statement. This is known as nesting of if statement. The nested if may appear in a program as :
i f ( condi t i on 1) { i f ( condi t i on 2) st at ement 1; el se st at ement 2; } el se { i f ( condi t i on 3) st at ement 3; el se st at ement 4; } The main thing to remember about nested if is that the else statement always refers to the nearest if statement within the same block. Example : This program uses the nested if to find greatest of three numbers
#i ncl ude mai n ( ) { i nt a, b, c, bi g ; pr i nt f ( "Ent er t hr ee number s" ) ; scanf ( " %d %d %d", &a, &b, &c) ; i f ( a > b) { i f ( a > c) bi g = a ; el se bi g = c; } el se { i f ( b > c) bi g = b ; el se bi g = c ; } pr i nt f ( " l ar gest of %d, %d & %d = %d" , a, b, c, bi g) ; 14
}
Explain the else-if ladder with example
The construct of else-if ladder is shown below :
i f ( condi t i on1) st at ement 1 ; el s e i f ( c ondi t i on1) st at ement 1 ; el s e i f ( c ondi t i on2) st at ement 2 ; . . . el s e i f ( c ondi t i onN) st at ement N ; el se st at ement ; The conditional expressions are evaluated from top downward. When a true condition is found, the associated statement is executed, and rest of ladder is bypassed. If none of conditions is true, then final else statement is executed. The final else acts as a default condition i.e. if all other conditions tests fail, then the last else statement is performed. If there is no final else and all other conditions are false then no action will take place.
Example : Following example illustrates the of if-else-if ladder
#i ncl ude mai n ( ) { i nt n; pr i nt f ( " Ent er an i nt eger bet ween 1 and 5" ) ; scanf ( “%d”, n) ; i f ( n==1) pr i nt f ( "number i s one\ n" ) ; el se i f ( n==2) pr i nt f ( "number i s t wo\ n") ; el se i f ( n==3) pr i nt f ( "number i s t hr ee\ n") ; el se i f ( n==4) pr i nt f ( "number i s f our \ n") ; el se i f ( n==5) pr i nt f ( "number i s f i ve\ n") ; el s e pr i nt f ( " You di dn' t f ol l ow t he r ul es " ) ; }
Explain the ? : operator (Conditional Operator)
C language uses a combination of ? and : for making two way decisions. This operator is called the conditional operator and it takes three operands. The general form of the conditional operator is : conditional expression ? expression1 : e xpression2
15
The conditional expression is first evaluated. If result is non-zero, then expression1 is evaluated and its value is returned. If result is zero, then expression2 is evaluated and its value is returned.
Example : Consider the following code :
i f ( x>3) a = 5; el se a = 1; The same can be written as :
a = ( x>3) ? 5 : 1 ; The conditional operator can be nested for more complex assignments. For example consider the following Salary = 4R+20 for R < 20 = 150 for R = 20 = 5R+90 for R > 20 This can be written as:
sal ar y = ( R=20) ? 150: ( ( R<20) ? ( 4* R+20) : ( 5*R+90) ) The code is concise, but more difficult to read. Hence it is better to use if … else statements when nesting of conditional operator is required.
i f ( R=20) sal ar y el se if R < sal ar y el se sal ar y
= 150 ; 20 = 4*R+20 ; = 5*R+90 ;
16
C PROGRAMS
Write a C program to accept marks of three subjects, and then print the total and average marks
/ * Pr ogr am: Add t hr ee mar ks and pr i nt t ot al and aver age */ #i ncl ude mai n( ) { i nt m1, m2, m3, t ot al ; f l oat aver age; pr i nt f ( “Ent er mar ks of t hr ee subj ect s”) ; scanf ( “%d %d %d”, &m1, &m2, &m3) ; t ot al = m1 + m2 + m3; pr i nt f ( " t ot al mar ks i s %d" , t ot al ) ; average = t ot al / 3; pr i nt f ( "aver age mar ks i s %d" , t ot al ) ; }
Write a C program to accept a number and display it in octal and hexadecimal form
/ * Pr ogr am t o pr i nt oct al and hexadeci mal number s */ #i ncl ude mai n( ) { i nt n; pr i nt f ( “\ nEnt er a deci mal number : ”) ; scanf ( “%d”, &n) ; pr i nt f ( “\ nNumber i s %d i n deci mal , %o i n oct al , %x i n hexadeci mal ”) ; }
Write C program using conditional operator to determine whether year entered through at the keyboard is a leap year or not
/ * Pr ogr am : To t est f or a l eap year */ #i ncl ude mai n( ) { i nt y ear ; pr i nt f ( “ Ent er an year : ” ) ; scanf ( “%d”, &year ) ; i f ( year %4 == 0 && year %100 ! = 0 | | year %400 == 0) pr i nt f ( “ year i s a l eap year ” ) ; el se pr i nt f ( “year i s not a l eap year ”) ; }
Write a C program to find whether a number is odd or even
/ * Progr am f or odd- even t est */ #i ncl ude mai n( ) 17
{ i nt n; pr i nt f ( “ Ent er an i nt eger : ” ) ; scanf ( “%d”, &n) ; i f ( n%2 == 0) pr i nt f ( “number i s even”) ; el se pr i nt f ( “number i s odd”) ; }
Write a C program to find whether number is divisible by 7 or not
/ * Pr ogr am : To t es t #i ncl ude mai n( ) { i nt n; pr i nt f ( “ Ent er an i scanf ( “%d”, &n) ; i f ( n%7 == 0) pr i nt f ( “number i s el se pr i nt f ( “number i s }
di vi s i bi l i t y by 7 * /
nt eger : ” ) ; di vi si bl e by 7”) ; not di vi si bl e by 7”) ;
Write a C program to find the factorial of a number
/ * Pr ogr am : To f i nd t he f act or i al of a number */ #i ncl ude mai n( ) { i nt n, f act =1; pr i nt f ( “ Ent er a pos i t i ve i nt eger : ” ) ; scanf ( “%d”, &n) ; f or ( i =1; i <=n; i ++) f act = f ac t * i ; pr i nt f ( “ \ nf ac t or i al of %d i s %d” , n, f act ) ; }
Write a C program to find whether a number is prime of not
/ * Pr ogr am : Pr i me number t est */ #i ncl ude mai n( ) { i nt i , n, i s pr i me=0; pr i nt f ( " Ent er t he number t o be checked" ) ; scanf ( " %d", &n) ; f or ( i =2; i <=n; i ++) { i f ( n%i ==0) i spr i me=- 1; } I f ( i spr i me == 0 ) pr i nt f ( "number i s pr i me") ; 18
el se pr i nt f ( " number i s not pr i me" ) ;
} / * Pr ogr am : Pr i me number t est */ #i ncl ude mai n( ) { i nt n, count ; pr i nt f ( “Ent er a number ”) ; scanf ( " %d" , &n) ; f or ( i =2; i 1) pr i nt f ( " not pr i me no. " ) ; el se pr i nt f ( " pr i me" ) ; }
Write a C program to print sum of digits in 3 digit numbers
/ * Pr ogr am : Add di gi t s of number */ #i ncl ude mai n( ) { i nt n, u, t , h; pr i nt f ( “ Ent er a 3 di gi t i nt eger : ” ) ; scanf ( “%d”, &n) ; u = n%10; n = n/ 10; t = n%10; n = n/ 10; h = n%10; pr i nt f ( “\ n Sum of di gi t s : %d“, u+t +h) ; }
Write a C program to reverse the three digit number entered as input through keyboard
/ * Pr ogr am : Rever se 3 di gi t number */ #i ncl ude mai n( ) { i nt n, u, t , h; pr i nt f ( “ Ent er a 3 di gi t i nt eger : ” ) ; scanf ( “%d”, &n) ; u = n%10; n = n/ 10; t = n%10; n = n/ 10; 19
h = n%10; pr i nt f ( “\ nRever sed number : %d%d%d“, u, t , h) ; }
Write a C program to reverse digits of a number entered as input through keyboard
/ * Pr ogr am : Rever se di gi t s of any number */ #i ncl ude mai n( ) { i nt n, di gi t ; pr i nt f ( “ Ent er a 3 di gi t i nt eger : ” ) ; scanf ( “%d”, &n) ; pr i nt f ( “\ nThe r ever sed di gi t s ar e: “) ; do { di gi t =n%10; pr i nt f ( “ %d” , di gi t ) ; n = n/ 10; } whi l e ( n>0) ; pr i nt f ( “ \ n”) ; }
Write a C program using if else ladder to grade student according to following rules. Marks 70 to 100 60 to 69 50 to 59 40 to 49 0 to 39
Grade Distinction I class II class pass class fail
/ * Progr am : Gr adi ng of st udent s */ #i ncl ude mai n ( ) { i nt mar ks ; pr i nt f ( "Ent er mar ks\ n") ; scanf ( " %d" , &mar ks) ; i f ( mar ks<=100 && mar ks>=70) pr i nt f ( " \ n Di st i nct i on" ) ; el se i f ( mar ks>=60) pr i nt f ( "\ n Fi r st cl ass") ; el se i f ( mar ks>=50) pr i nt f ( "\ n second cl ass" ) ; el se i f ( mar ks>=35) pr i nt f ( "\ n pass cl ass") ; el s e pr i nt f ( " Fai l " ) ; }
Write a C program to find sum of series : 1+3+5+7+…+N
/ * Pr ogr am : Sum of s er i es * / 20
#i ncl ude mai n ( ) { i nt i , n, s um = 0 ; pr i nt f ( " Ent er val ue of n: " ) ; scanf ( " %d", &n) ; f or ( i =0; i <=n; i =i +2) sum = sum + i ; pr i nt f ( "sum = %d” , sum") ; }
Write a C program to find sum of series : 0+1+3+6+10+15+21+…upto N numbers
/ * Pr ogr am : Sum of s er i es * / #i ncl ude mai n ( ) { i nt i , n, sum = 0, t er m=0 ; pr i nt f ( " Ent er val ue of n: " ) ; scanf ( " %d", &n) ; f or ( i =0; i <=n- 1; i ++) { t er m = t er m + i ; sum = sum + t er m ; } pr i nt f ( "sum = %d” , sum") ; }
21