C Assingment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

Assingment

Que-1 Implement a program in GNU C to display the message “Hello World!” on screen
Code:

#include<stdio.h>
int main(){
    printf("Hello World!");
    return 0;
}
Output:
Hello World!

Que-2 Implement a program in GNU C where the user enters an integer from keyboard at run-time. The
entered integer should be displayed on screen.

Code:

#include<stdio.h>
int main(){
    int num;
    printf("Enter the number :");
    scanf("%d",&num);
    printf("the number is %d.",num);
    return 0;
}
Output:
Enter the number :08
the number is 8.

Que-3 Implement a program in GNU C where the user enters two integers from keyboard at run-time. The sum
of both the integers should be displayed on screen.

Code :
#include<stdio.h>
int main(){
    int num1,num2;
    printf("\nEnter number-1 here :");
    scanf("%d",&num1);
    printf("\nEnter number-2 here :");
    scanf("%d",&num2);
    int num=num1+num2;
    printf("\nthe sum of %d and %d is %d.",num1,num2,num);
    return 0;
}

Output:
Enter number-1 here :2
Enter number-2 here :3
the sum of 2 and 3 is 5.

Que-4 Implement a program in GNU C where the user enters two integers from keyboard at run-time. The
sum, difference and product of both the integers should be displayed on screen.

Code :
#include<stdio.h>
int main(){
    int num1,num2;
    printf("\nEnter number-1 here :");
    scanf("%d",&num1);
    printf("\nEnter number-2 here :");
    scanf("%d",&num2);
    int sum=num1+num2;
    int sub = num1-num2;
    int multi = num1*num2;
    printf("\nthe sum of %d and %d is %d.",num1,num2,sum);
    printf("\nthe sum of %d and %d is %d.",num1,num2,sub);
    printf("\nthe sum of %d and %d is %d.",num1,num2,multi);
    return 0;
}

Output:
Enter number-1 here :4
Enter number-2 here :3
the sum of 4 and 3 is 7.
the sum of 4 and 3 is 1.
the sum of 4 and 3 is 12.

Que-3 Implement a program in GNU C where the user enters a floating point number (decimal number) from
keyboard at run-time. The entered number should be displayed on screen.

Code:

#include<stdio.h>
int main(){
    float num;
    printf("Enter floating number here :");
    scanf("%f",&num);
    printf("the floating number is %f.",num);
    return 0;
}

Output:
Enter floating number here :4
the floating number is 4.000000.

Que-6 Implement a program in GNU C where the user enters two floating point numbers from keyboard at
run-time. The sum, difference and product of both the numbers should be displayed on screen.

Code:

#include<stdio.h>
int main(){
    float num1,num2;
    printf("\nEnter number-1 here :");
    scanf("%f",&num1);
    printf("\nEnter number-2 here :");
    scanf("%f",&num2);
    float sum=num1+num2;
    float sub = num1-num2;
    float multi = num1*num2;
    printf("\nthe sum of %f and %f is %f.",num1,num2,sum);
    printf("\nthe sum of %f and %f is %f.",num1,num2,sub);
    printf("\nthe sum of %f and %f is %f.",num1,num2,multi);
    return 0;
}

Output:
Enter number-1 here :4
Enter number-2 here :2
the sum of 4.000000 and 2.000000 is 6.000000.
the sum of 4.000000 and 2.000000 is 2.000000.
the sum of 4.000000 and 2.000000 is 8.000000.

Que-7 Implement a program in GNU C where the user enters a character from keyboard at runtime. The
entered character and its corresponding ASCII value (in decimal) should be displayed on screen.

Code:

#include<stdio.h>
int main(){
    char str;
    printf("Enter character here :");
    scanf("%c",&str);
    printf("the floating number is %d.",str);
    return 0;
}

Output:
Enter character here :A
the floating number is 65.
Que-8 Implement a program in GNU C where the user enters the radius of a circle from keyboard at run-time.
The circumference and area of the circle should be displayed on screen. DO NOT USE any library function of
math.h for this program.

Code:

#include<stdio.h>
int main(){
    int r,area,circumference;
    printf("Enter radius here :");
    scanf("%d",&r);
    area=3.14*(r*r);
    circumference=2*3.14*r;
    printf("the Area of circle is %d.\n",area);
    printf("the circumference of circle is %d.\n",circumference);
    return 0;
}

Output:
Enter radius here :4
the Area of circle is 50.
the circumference of circle is 25.

Que-9 Implement a program in GNU C where the user enters the length of a side of a square from keyboard at
run-time. The perimeter and area of the circle should be displayed on screen. Make use of pow() library
function of math.h for this program.

Code:

#include<stdio.h>
#include<math.h>
int main(){
    int s,area,circumference;
    printf("Enter side of a square here :");
    scanf("%d",&s);
    area=pow(s,2);
    circumference=4*s;
    printf("the Area of square is %d.\n",area);
    printf("the circumference of square is %d.\n",circumference);
    return 0;
}
Output:
Enter side of a square here :2
the Area of square is 4.
the circumference of square is 8.

Que-10 Implement a program in GNU C where the user enters the temperature in celcius unit from keyboard
at run-time. The corresponding temperature in Fahrenheit unit should be displayed on screen.

Code:

#include<stdio.h>
int main(){
    int c,f;
    printf("Enter the temperature in celcius :");
    scanf("%d",&c);
    f=(c*(9/5))+32;
    printf("Fahrenheit unit is %d.",f);
    return 0;
}
Output:
Enter the temperature in celcius :0
Fahrenheit unit is 32.

Que-11 Implement a program in GNU C where the user enters the temperature in Fahrenheit unit from
keyboard at run-time. The corresponding temperature in Celcius unit should be displayed on screen.

Code:

#include<stdio.h>
int main(){
    int c,f;
    printf("Enter the temperature in  Fahrenheit :");
    scanf("%d",&f);
    c=(f-32)*(9/5);
    printf("Celcius unit is %d.",c);
    return 0;
}
Output:
Enter the temperature in Fahrenheit :32
Celcius unit is 0.

Que-12 Implement a program in GNU C where the user enters the weight in kilograms unit from keyboard at
run-time. The corresponding weight in grams unit should be displayed on screen.

Code:

#include<stdio.h>

int main(){

    int kg;

    printf("Enter the kilogram value :");

    scanf("%d",&kg);

    printf("the gram value of %dkg is %d.\n",kg,kg*1000);

    return 0;

Output:

Enter the kilogram value :5

the gram value of 5kg is 5000.

Que-13 Implement a program in GNU C where the user enters the weight in grams unit from keyboard at run-
time. The corresponding weight in kilograms unit should be displayed on screen.

Code :

#include<stdio.h>

int main(){

    float gram;

    printf("Enter the gram value :");

    scanf("%f",&gram);

    printf("the gram value of %fgm is %f.\n",gram,gram/1000);

    return 0;

Output:

Enter the gram value :800


the gram value of 800.000000gm is 0.800000.

Que-14 Implement a program in GNU C to swap the values of two variables. The value of both the variables
before swapping and after swapping should be displayed on screen.

Code :

#include<stdio.h>

int main(){

    int num1,num2;

    printf("Enter number 1 here :");

    scanf("%d",&num1);

    printf("Enter  number 2 here :");

    scanf("%d",&num2);

    printf("after number 1 is %d.\n",num1+num2-num1);

    printf("after number 2 is %d.\n",num1+num2-num2);

    return 0;

Output:

Enter number 1 here :43

Enter number 2 here :32

after number 1 is 32.

after number 2 is 43.

Que-15 Implement a program in GNU C where the user enters two integers a and b from keyboard at run-time.
Accordingly, the message “a is greater than b”, “a is less than b” or “a is equal to b” should be displayed on
screen.

Code:

#include<stdio.h>

int main(){

    int a,b;

    printf("Enter number 1 here :");

    scanf("%d",&a);

    printf("Enter  number 2 here :");

    scanf("%d",&b);

    if(a==b){

        printf("a ia equal to b\n");


    }else if(a>b){

        printf("a greater than b\n");

    }else{

        printf("a is less than b\n");

  }

    return 0;

Output:

Enter number 1 here :45

Enter number 2 here :34

a greater than b

Que-16 Implement a program in GNU C where the user enters an integer from keyboard at run-time.
Accordingly, the message “ The entered integer is odd” or “The entered integer is even” should get displayed
on screen.

Code:

#include<stdio.h>

int main(){

    int a;

    printf("Enter number 1 here :");

    scanf("%d",&a);

    if(a%2==0){

        printf("a is even number !\n");

    }else{

        printf("a is odd number !\n");

  }

    return 0;

Output:

Enter number 1 here :5

a is odd number !

Que-17 Implement a program in GNU C where the user enters the percentage result obtained from keyboard
at run-time. The corresponding grade should be displayed on screen. Assume that the rules of grading are as
follows: for % =40 and <=49, grade is PASS, for % >=50 and <=59 grade is SECOND, for % >=60 and <=69 grade is
FIRST, for % >=70 grade is DISTINCTION. Make use of if /if-else/else constructs for above program.
Code:

#include<stdio.h>

int main(){

    int percentage;

    printf("Enter percentage here :");

    scanf("%d",&percentage);

    if(percentage>=40 && percentage<=49){

        printf("grade is PASS !\n");

    }else if(percentage>=50 && percentage<=59){

        printf("grade is SECOND !\n");

    }else if(percentage>=60 && percentage<=69){

        printf("grade is FIRST !\n");

    }else{

        printf("DISTNCTION !\n");

  }

    return 0;

Output:

Enter percentage here :56

grade is SECOND !

Que-18 Implement above program using SWITCH-CASE construct.

Code :
#include <stdio.h>

int main() {
    int percentage;
    printf("Enter percentage here: ");
    scanf("%d", &percentage);
  
    switch (percentage/10) {
        case 4:
            printf("Grade is PASS!\n");
            break;
        case 5:
            printf("Grade is SECOND!\n");
            break;
        case 6:
            printf("Grade is FIRST!\n");
            break;
        default:
            printf("DISTINCTION!\n");
            break;
  }
  
    return 0;
}

Output:
Enter percentage here: 45
Grade is PASS!

Que-19 Implement a program in GNU C where the user enters three integers from keyboard at runtime. The
greatest of all the three numbers should be displayed on screen. DO NOT USE ARRAYS.

Code:

#include <stdio.h>

int main() {

    int num1, num2, num3, greatest;

    printf("Enter number 1 here : ");

    scanf("%d", &num1);

    printf("Enter number 2 here :");

    scanf("%d",&num2);

    printf("Enter number 3 here :");

    scanf("%d",&num3);

    greatest = num1;

    if (num2 > greatest) {

        greatest = num2;
  }

    if (num3 > greatest) {

        greatest = num3;

  }

    printf("The greatest of the three numbers is: %d\n", greatest);

  

    return 0;

Output:

Enter number 1 here : 45

Enter number 2 here :56

Enter number 3 here :34

The greatest of the three numbers is: 56

Que-20 Implement a program in GNU C where the user enters an alphabetic character from keyboard at run-
time. The program should classify the entered alphabet as “VOWEL” or “CONSONANT”

Code:

#include<stdio.h>

int main(){

    char str;

    printf("Enter character here :");

    scanf("%c",&str);

    if(str=='a' || str=='e' ||str=='i' ||str=='o' ||str=='u'){

        printf("entered alphabet is VOWEL !\n");

    }else{

        printf("entered alphabet is CONSONANT !\n");

  }

    return 0;

Output:

Enter character here :a

entered alphabet is VOWEL !


Que-21 Implement a program in GNU C where the user enters a character from keyboard at runtime. The
program should classify whether the entered character is an UPPER CASE alphabet, LOWER CASE alphabet,
numeric or OTHER character. DO NOT USE ANY LIBRARY FUNCTIONS except printf and scanf.

Code:

#include<stdio.h>

int main(){

    char str;

    printf("Enter the character here :");

    scanf("%c",&str);

    if(str>='A' && str<='Z'){

        printf("Entered character is uppercase character 1\n");

    }else if(str>='a' && str<='z'){

        printf("Entered character is lowercase character 1\n");

    }else if(str>='0' && str<='9'){

        printf("Entered character is numeric character 1\n");

    }else{

        printf("Entered character is other character 1\n");

  }

    return 0;

Output:

Enter the character here :3

Entered character is numeric character 1

Que-22 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should extract the last digit of the number and display its value.

Code:

#include<stdio.h>

int main(){

    int num,result;

    printf("Enter number here :");

    scanf("%d",&num);

    result=num%10;

    printf("last digit of %d is %d.",num,result);

    return 0;

}
Output:

Enter number here :423

last digit of 423 is 3.

Que-23 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should display the total number of digits in that number. Eg. If 23 is entered, the output of the
program should be 2. Assume that no leading zeroes are entered while entering the integer.

Code:

#include<stdio.h>

int main(){

    int num,result,i=0;

    printf("Enter number here :");

    scanf("%d",&num);

    while(num>0){

        result=num%10;

        num=num/10;

        i++;

  }

    printf("Total number in number is %d.",i);

    return 0;

Output:

Enter number here :234

Total number in number is 3.

Que-24 Implement a program in GNU C to print the message “Hello World” 20 times, without replicating the
code.

Code:

#include<stdio.h>

int main(){

    for(int i=1;i<=15;i++){

        printf("%d-Hello World!\n",i);

  }

    return 0;

}
Output:

1-Hello World!

2-Hello World!

3-Hello World!

4-Hello World!

5-Hello World!

6-Hello World!

7-Hello World!

8-Hello World!

9-Hello World!

10-Hello World!

11-Hello World!

12-Hello World!

13-Hello World!

14-Hello World!

15-Hello World!

Que-25 Implement a program in GNU C where the user enters a natural number from keyboard at run-time.
The program should display the SIGMA or sum of integers starting from 1 up to the given number.

Code:

#include<stdio.h>

int main(){

    int num,sum=0;

    printf("Enter number here :");

    scanf("%d",&num);

    for(int i=1;i<=num;i++){

        sum=sum+i;

  }

    printf("sum of integers form 1 up to %d is %d.",num,sum);

    return 0;

Output:

Enter number here :10

sum of integers form 1 up to 10 is 55.


Que-26 Implement a program in GNU C where the user enters a natural number from keyboard at run-time.
The program should display the factorial of the given number.

Code:

#include<stdio.h>

int main(){

    int num,sum=1;

    printf("Enter number here :");

    scanf("%d",&num);

    for(int i=1;i<=num;i++){

        sum=sum*i;

  }

    printf("Factorial of  %d is %d.",num,sum);

    return 0;

Output:

Enter number here :3

Factorial of 3 is 6.

Que-27 Implement a program in GNU C where the user enters a natural number from keyboard at run-time.
The program should classify the number as a prime, composite or a special number.

Code:

#include<stdio.h>

int main(){

int num ;

printf("enter a number here :");

scanf("%d",&num);

if(num==1){

printf("special number !");

}else if(num==2){

printf("yes,it is a prime number !");

else{

for(int i=2;i<num;i++){
if(num%i==0){

printf("composite number !");

break;

}else{

printf("Yes,it is prime number !");

break;

return 0;

Output:

enter a number here :1

special number !

Que-28 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should display the sum of digits in that number. Eg. If 23 is entered, the output of the program
should be 5.

Code:

#include<stdio.h>

int main() {

    int n,sum=0,m;

    printf("Enter a number:");

    scanf("%d",&n);

    while(n>0){

        m=n%10;

        sum=sum+m;

        n=n/10;

  }

    printf("Sum is=%d",sum);

    return 0;

Output:

Enter a number:3456

Sum is=18
Que-29 Implement a program in GNU C where the user enters a multi-digit integer from keyboard at run-time.
The program should display the reversed number. Eg. If 23 is entered, the output of the program should be 32.
For single digit number, the output should remain same as the original number entered.

Code:

#include <stdio.h>

int main() {

  int n, reverse = 0, remainder;

  printf("Enter an integer: ");

  scanf("%d", &n);

  while (n != 0) {

    remainder = n % 10;

    reverse = reverse * 10 + remainder;

    n /= 10;

 }

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

  return 0;

Output:

Enter an integer: 54

Reversed number = 45

Que-30 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display the GCD of the two integers.

Code:

#include <stdio.h>

int main()

    int n1, n2, i, gcd;

    printf("Enter two integers: ");

    scanf("%d %d", &n1, &n2);


    for(i=1; i <= n1 && i <= n2; ++i){

        if(n1%i==0 && n2%i==0)

            gcd = i;

  }

    printf("G.C.D of %d and %d is %d", n1, n2, gcd);

    return 0;

Output:

Enter two integers: 4

G.C.D of 4 and 5 is 1

Que-31 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display the LCM of the two integers

Code:

#include<stdio.h>

int main(){

    int num1,num2;

    printf("enter num1 here :");

    scanf("%d",&num1);

    printf("enter num2 here :");

    scanf("%d",&num2);

    if(num1>num2){

        if(num1%num2==0){

            printf("%d",num1);

        }else{

        for (int i=1;i<num1;i++){

            int num=num1*i;

            if(num%num2==0){

                printf("The LCM of %d and %d is %d.\n",num1,num2,num);

                break;

      }

    }

    }

    }else{
        if(num2%num1==0){

            printf("%d",num2);

        }else{

        for (int i=1;i<num2;i++){

            int num=num2*i;

            if(num%num1==0){

                printf("The LCM of %d and %d is %d.\n",num1,num2,num);

                break;

      }

    }

  }

  }

    return 0;

output:

enter num1 here :4

enter num2 here :5

The LCM of 4 and 5 is 20.

Que-32 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display whether the first integer is exactly divisible by the second integer.

Code:

#include <stdio.h>

int main() {

    int num1, num2;

    printf("Enter number 1 here : ");

    scanf("%d", &num1);

    printf("Enter number 2 here : ");

    scanf("%d", &num2);

    if (num1 % num2 == 0) {

        printf("%d is exactly divisible by %d.\n", num1, num2);

  }

    else {

        printf("%d is not exactly divisible by %d.\n", num1, num2);

  }
    return 0;

Output:

Enter number 1 here : 4

Enter number 2 here : 2

4 is exactly divisible by 2.

Que-33 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display whether the first integer is a factor of the second integer

Code:

#include <stdio.h>

int main() {

    int num1, num2;

    printf("Enter number 1 here : ");

    scanf("%d", &num1);

    printf("Enter number 2 here : ");

    scanf("%d", &num2);

    if (num1 % num2 == 0) {

        printf("%d is Factor of  %d.\n", num2, num1);

  }

    else {

        printf("%d is not Factor of %d.\n", num2, num1);

  }

    return 0;

Output:

Enter number 1 here : 4

Enter number 2 here : 2

2 is Factor of 4.

Que-34 Implement a program in GNU C where the user enters two integers from keyboard at runtime. The
program should display whether the first integer is a multiple of the second integer.

Code:

#include <stdio.h>
int main() {

    int num1, num2;

    printf("Enter number 1 here : ");

    scanf("%d", &num1);

    printf("Enter number 2 here : ");

    scanf("%d", &num2);

    if (num2 % num1 == 0) {

        printf("%d is multiple of  %d.\n", num1, num2);

  }

    else {

        printf("%d is not multiple  of %d.\n", num1, num2);

  }

    return 0;

Output:

Enter number 1 here : 2

Enter number 2 here : 4

2 is multiple of 4.

Que-35 Implement a program in GNU C where the user enters a decimal integer from keyboard at run-time.
The program should display the equivalent binary, octal and hexadecimal value of the entered number.

Code:

#include<stdio.h>

int Binary(int num){

int remainder,b[15],i,j,rev=0,a=1;

while(num){

remainder=num%2;

rev=rev+remainder*a;

a=a*10;

num=num/2;

b[i]=remainder;

i++;

}
printf("Binary value =%d.\n",rev);

int Octal(int num){

int remainder,O[15],i,rev=0,a=1;

while(num){

remainder=num%8;

rev=rev+remainder*a;

a=a*10;

num=num/8;

O[i]=remainder;

i++;

printf("Octal value =%d.\n",rev);

int Hexa_decimal(int num){

int remainder,hex_num,i=0,rev=0,a=1;

char h[50];

while(num){

remainder=num%16;

rev=rev+remainder*a;

a=a*10;

if(remainder<10){

remainder=remainder+48;

i++;

}else{

remainder=remainder+55;

i++;

h[i++]=remainder;

num=num/16;

printf("Hexadecimal value is: ");

for (int j = i - 1; j > 0; j--)


printf("%c", h[j]);

int main(){

int num;

printf("Enter the number here :");

scanf("%d",&num);

Binary(num);

Octal(num);

Hexa_decimal(num);

return 0;

Output:

Enter the number here :56

Binary value =111000.

Octal value =70.

Hexdecimal value =38.

Que-36 Implement a program in GNU C where the user enters a binary integer from keyboard at run-time.
The program should display the equivalent decimal, octal and hexadecimal value of the entered number.

Code:

#include <stdio.h>

#include <stdlib.h>

int Hexa_decimal(int num){

int remainder,hex_num,i=0,rev=0,a=1;

char h[50];

while(num){

remainder=num%16;

rev=rev+remainder*a;

a=a*10;

if(remainder<10){

remainder=remainder+48;

i++;

}else{

remainder=remainder+55;
i++;

h[i++]=remainder;

num=num/16;

printf("Hexadecimal value is: ");

for (int j = i - 1; j > 0; j--)

printf("%c", h[j]);

int Octal(int num){

int remainder,O[15],i,rev=0,a=1;

while(num){

remainder=num%8;

rev=rev+remainder*a;

a=a*10;

num=num/8;

O[i]=remainder;

i++;

printf("Octal value =%d.\n",rev);

int main() {

long long binaryNumber;

int decimalNumber = 0, i = 0, remainder;

printf("Enter a binary number: ");

scanf("%lld", &binaryNumber);

while (binaryNumber != 0) {

remainder = binaryNumber % 10;

binaryNumber /= 10;

decimalNumber += remainder * pow(2, i);

++i;

printf("Decimal equivalent: %d\n", decimalNumber);


Octal(decimalNumber);

Hexa_decimal(decimalNumber);

return 0;

Output:

Enter a binary number: 11011

Decimal equivalent: 27

Octal value =33.

Hexadecimal value is: 1

Que-37 Implement a program in GNU C where the user enters a hexadecimal integer from keyboard at run-
time. The program should display the equivalent binary, octal and decimal value of the entered number.

Code:

#include <stdio.h>

#include <stdlib.h>

int Binary(int num){

int remainder,b[15],i,j,rev=0,a=1;

while(num){

remainder=num%2;

rev=rev+remainder*a;

a=a*10;

num=num/2;

b[i]=remainder;

i++;

printf("Binary value =%d.\n",rev);

int Octal(int num){

int remainder,O[15],i,rev=0,a=1;

while(num){

remainder=num%8;
rev=rev+remainder*a;

a=a*10;

num=num/8;

O[i]=remainder;

i++;

printf("Octal value =%d.\n",rev);

int main() {

char hex[20];

long int decimal = 0;

int i = 0, j = 0;

printf("Enter a hexadecimal number: ");

scanf("%s", hex);

// convert hexadecimal to decimal

while (hex[i] != '\0') {

if (hex[i] >= '0' && hex[i] <= '9') {

decimal = decimal * 16 + (hex[i] - '0');

else if (hex[i] >= 'a' && hex[i] <= 'f') {

decimal = decimal * 16 + (hex[i] - 'a' + 10);

else if (hex[i] >= 'A' && hex[i] <= 'F') {

decimal = decimal * 16 + (hex[i] - 'A' + 10);

i++;

printf("Decimal equivalent: %ld\n", decimal);

Binary(decimal);
Octal(decimal);

return 0;

Output:

Enter a hexadecimal number: 1B

Decimal equivalent: 27

Binary value =11011.

Octal value =33.

Que-38 Implement a program where the user is supposed to enter an even integer. Till the user doesn’t enter
an even integer, the program continuously prompts the user that the entered number is incorrect and to kindly
enter an even integer.

Code:

#include<stdio.h>

int main(){

    int num;

    while(1){

        printf("Enter the number here :");

        scanf("%d",&num);

        if(num%2==0){

            printf("Even integer !\n");

            break;

        }else{

            printf("entered number is incorrect and to kindly enter an even integer!\n");

    }

  }  

    return 0;

Output:

Enter the number here :3

entered number is incorrect and to kindly enter an even integer!

Enter the number here :4

Even integer !
Que-39 Implement a program where the user is supposed to enter an even integer. Till the user doesn’t enter
an even integer, the program continuously prompts the user that the entered number is incorrect and to kindly
enter an even integer upto 4 times. After 4th consecutive failure to enter the correct integer, the program
terminates.

Code:

#include<stdio.h>

int main(){

    int num,i=0;

    while(1){

        printf("Enter the number here :");

        scanf("%d",&num);

        if(num%2==0){

            printf("Even integer !\n");

            break;

        }else{

            printf("entered number is incorrect and to kindly enter an even integer!\n");

            i++;

    }

        if(i>4){

            printf("consecutive failure to enter the correct integer!\n");

            break;

    }

  }  

    return 0;

Output:

Enter the number here :3

entered number is incorrect and to kindly enter an even integer!

Enter the number here :3

entered number is incorrect and to kindly enter an even integer!

Enter the number here :3

entered number is incorrect and to kindly enter an even integer!

Enter the number here :3

entered number is incorrect and to kindly enter an even integer!

Enter the number here :3

entered number is incorrect and to kindly enter an even integer!


consecutive failure to enter the correct integer!

Que-40 Implement a program where the user is supposed to enter 5 odd integers. The program should display
the sum of all the entered odd integers. In case, the user enters an even integer, then the entered even integer
SHOULD NOT BE COUNTED while calculating the sum.

Code:

#include<stdio.h>

int main(){

    int num,sum=0;

    for(int i=0;i<5;i++){

        printf("Enter odd number here :");

        scanf("%d",&num);

        if(num%2!=0){

            sum=sum+num;

    }

  }

    printf("the sum of od  integers is %d.\n",sum);

    return 0;

Output:

Enter odd number here :1

Enter odd number here :2

Enter odd number here :3

Enter odd number here :4

Enter odd number here :5

the sum of od integers is 9.

Que-41 Implement a program where the user is supposed to enter 5 odd integers. The program should display
the sum of all the entered odd integers. In case, the user enters an even integer, then the program displays the
sum of the entered odd integers up to the last odd integer entered and terminates.

Code:

#include<stdio.h>

int main(){

    int num,sum=0;

    for(int i=0;i<5;i++){

        printf("Enter odd number here :");


        scanf("%d",&num);

        if(num%2!=0){

            sum=sum+num;

        }else{

            break;

    }

  }

    printf("the sum of od  integers is %d.\n",sum);

    return 0;

Output:

Enter odd number here :1

Enter odd number here :3

Enter odd number here :5

Enter odd number here :2

the sum of od integers is 9.

Que-42 Implement a program which displays the following menu when the program is run. 1-ACCOUNT
OPENING 2-DEPOSIT 3-WITHDRAW 4-BALANCE ENQUIRY 5-ACCOUNT CLOSING 6-ACCOUNT DEACTIVATE
Depending upon the number entered by the user, the corresponding message will be displayed. Eg. If the user
enters 4, the message “BALANCE ENQUIRY” will be displayed.

Code:

#include <stdio.h>

int main() {

    int choice;

    printf("MENU\n");

    printf("1-ACCOUNT OPENING\n");

    printf("2-DEPOSIT\n");

    printf("3-WITHDRAW\n");

    printf("4-BALANCE ENQUIRY\n");

    printf("5-ACCOUNT CLOSING\n");

    printf("6-ACCOUNT DEACTIVATE\n");

    printf("Enter your choice: ");

    scanf("%d", &choice);
    switch (choice) {

        case 1:

            printf("ACCOUNT OPENING\n");

            break;

        case 2:

            printf("DEPOSIT\n");

            break;

        case 3:

            printf("WITHDRAW\n");

            break;

        case 4:

            printf("BALANCE ENQUIRY\n");

            break;

        case 5:

            printf("ACCOUNT CLOSING\n");

            break;

        case 6:

            printf("ACCOUNT DEACTIVATE\n");

            break;

        default:

            printf("Invalid choice.\n");

  }

    return 0;

Output:

MENU

1-ACCOUNT OPENING

2-DEPOSIT

3-WITHDRAW

4-BALANCE ENQUIRY

5-ACCOUNT CLOSING

6-ACCOUNT DEACTIVATE
Enter your choice: 4

BALANCE ENQUIRY

You might also like