Tuesday 23 August 2022

Very important question of BICTE | Programming Concept C | Study Material/

 Previous year question solution

Relational and logical operators:

All the relational, equality and logical operators are binary except !(not) and they take two operands and yield either int value 1 or int value 0.

a.     Relational operators:

All the relational operators are binary operators and they yield either int value 1 or int value 0 to represent true or false respectively. In the following expression

a<b  

if a is less than b then expression returns int value 1 (true) but if a is not less than b then the expression returns int value 0 (false). Mathematically the expression a<b is equivalent to a-b<0.

The equivalent expressions of the other relational expression are:

a<b       is equivalent to        a-b<0

a>b       is equivalent to        a-b>0

a>=b     is equivalent to        a-b>=0

a<=b     is equivalent to        a-b<=0

 

example:

if int p=1, q=5, r=-7, and char c=’a’ then the result of the following expression becomes

expression                              equivalent expression                   value

p-3 <= p+q                            (p-3) <= (p+q)                                          1

‘b’ + 1 < c                               (‘b’ =1) < c                                               0

p>r+5                                       p> (r+5)                                                  1

p+r*2>=q-6                            (p+(r*3))>=(q-6)                                    0

 

 

b.     Logical operators:

The logical operator && (and) and || (or) are binary and the operator ! (not) is unary.

The logical operator && (and) and || (or) are binary operator and they yield either int value 1 or int value 0 to represent true or false respect.

ively.The logical not (!) operator is unary operator and can be applied for any expression. The not (!) operator return int value 0 if the operand is non zero and return int value 1 if the operand has zero value.

 Example of logical not (!):

if int p=5, q=5, float a=1.2, b=0.0  then the result of the following expression becomes

expression                              equivalent expression                   value

! (p-q)                                      ! (p-q)                                                      0

! p-q                                         (!p) -q                                                     -5

! ! q                                           ! (! q)                                                      1

! ! (a+b)                                    ! ( ! (a+b) )                                             2

 

Example of logical operator(&&) and (||):

 

if int p=3, q=7, r=2 and char c=’D’  then the result of the following expression becomes

expression                              equivalent expression                   value

p && q && r                              (p && q) && r                                      1

p<q && c>’B’                             ( p<q) &&( c>’B’)                                 1

p<q || r>5                                   (p<q )|| (r>5 )                                     1

1.      C program to find factorial of number.VVI

/* C Program to Find Factorial of a Number */

 

#include <stdio.h>

 

int main()

{

  int i, n, fact=1;

  printf("\nEnter any number to Find Factorial\n");

  scanf("%d", &n);

 

  for (i = 1; i <= n; i++)

   {

     fact = fact * i;

   }

 

  printf("\nFactorial of %d = %d\n", n, fact);

 

  return 0;

}

Rough:

N =5

5*4*3*2*1=120

I=1, 1<=5,true

Fact=1*1=1

I=2, 2<=5,tue

Fact=1*2=2

I=3, 3<=5,true

Fact=2*3=6

I=4, 4<=5,true

Fact=6*4=24

I=5, 5<=5

Fact=24*5=120

I=6, 6<=5

False

 

 

 

 

 

2.      C program to reverse a number.VVV

/* C Program to reverse a number */

#include <stdio.h>

int main() {

    int n, rev = 0, r;

    printf("Enter a number: ");

    scanf("%d", &n);

    while (n != 0) {

        r = n % 10;

        rev = rev * 10 + r;

        n = n/10;

    }

    printf("Reversed number = %d", rev);

    return 0;

}

Rough:

N =123

Step1:

        r = 123 % 10;

        r=3

        rev = 0 * 10 + 3;

        rev=3

        n = 12/10;

        n =12

        r = 12 % 10;

        r=2

        rev = 3* 10 + 2;

        rev=32

        n = 1/10;

        r =1%10;

        r=1

        rev=32*10+1

        rev=321

        num=0/10

 

 

 

 

 

 

3.      C program to palindrome a number.VVVI

/* C Program to check whether a number is palindrome or not */

#include <stdio.h>

int main() {

    int n, rev = 0, r, temp;

    printf("Enter an integer number: ");

    scanf("%d", &n);

    temp = n;

 

    // reversed integer is stored in rev

    while (n != 0) {

        r = n % 10;

        r = r * 10 + r;

        n =n/ 10;

    }

 

    // palindrome if temp and rev are equal

    if (temp == rev)

        printf("%d is a palindrome.", temp);

    else

        printf("%d is not a palindrome.", temp);

 

    return 0;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.      C program to display Fibonacci series.VVI

/* C Program to display Fibonacci series */

#include <stdio.h>

int main() {

    int i, n, t1 = 0, t2 = 1;

    int nextTerm = t1 + t2;

    printf("Enter the number of terms: ");

    scanf("%d", &n);

    printf("Fibonacci Series: %d, %d, ", t1, t2);

 

    for (i = 1; i <= n; ++i) {

        printf("%d, ", nextTerm);

        t1 = t2;

        t2 = nextTerm;

        nextTerm = t1 + t2;

    }

 

    return 0;

}

 

No comments:

Post a Comment