Important Questions of C-language

To find average of two numbers.

 1#include <stdio.h>
 2int main()
 3{
 4    float a;
 5    float b;
 6    printf("give the values for a and b:\n");
 7    scanf("%f%f", &a,&b);
 8    float c=(a+b)/2;
 9    printf("the average of two numbers is %f\n", c);
10    return 0;
11}

To find the binary equivalent of a number.

 1#include <stdio.h>//LEARN!!!
 2int main()
 3{
 4    int n,r;
 5    printf("enter the values for n:");
 6    scanf("%d", &n);
 7    int binary=0,rem,count=1;
 8    while(n>0&&n<255){
 9        rem=n%2;
10        binary=binary+(count*rem);
11        n=n/2;
12        count=count*10;
13    }
14    printf("the binary equivalent is %d", binary);
15    return 0;
16}

Using Reccursion

 1#include <stdio.h>
 2int main()
 3{
 4    unsigned i, r;
 5    printf("ENTER THE NUMBER:");
 6    scanf("%d", &i);
 7    //using for loop:
 8    // if (i < 255 && i > 0) 
 9    // {
10    //     for (; i > 0; i = i / 2)
11    //     {
12    //         printf("%d", i % 2);
13    //     }
14    // }
15    // else
16    // {
17    //     printf("given number is not in range");
18    // }
19
20    if (i < 255 && i > 0)
21    {
22
23        while (1)
24        {
25            r = i % 2;
26            printf("%d", r);
27
28            if (i / 2 == 0)
29            {
30                break;
31            }
32            else
33            {
34                i = i / 2;
35            }
36        }
37    }
38    else
39    {
40        printf("given number is not in range");
41    }
42}

Checking whether the number is prime or not.

 1#include <stdio.h>
 2int main()
 3{   int a,i,flag;
 4float b;
 5    printf("enter the number:\n");
 6    scanf("%d", &a);
 7    for(i=2;i<a;i++){
 8
 9        if(a%i==0){
10            flag=1;
11            break;
12            
13        }}
14        if(flag==0){
15            printf("prime number");
16        }
17        else{
18            printf("not prime");
19        }
20
21    
22    
23    return 0;
24}

Using Conditional Statements

 1#include <stdio.h>
 2#include <string.h>
 3
 4void main()
 5{
 6    int i = 0;
 7
 8    if (i > 10){
 9        printf("value is greater then 10");
10    }
11    else if( i > 5 ){
12        printf("value is greater then 5");
13    }else if(i > 1){
14        printf("value is less then 10 and 5");
15    }else{
16        printf("jdfjsldjflsd");
17    }
18
19    i = 1;
20    switch (i)
21    {
22    case 1:
23        printf("hi this is 1");
24        break;
25    case 2:
26        printf("hi this is 2");
27    default:
28        printf("mujhe nahi pata");
29    }
30
31
32    // checking the condition with logical operators
33    // [and &&] - [OR ||] [not !]
34    i = 6;
35
36    if (i > 4 && i < 8){
37        printf(" i in the range of 4 to 6 \n ");
38    }
39
40    if (i > 4 || i < 10 ){
41        printf(" i in the range of 4 and 6 \n");
42    }
43
44    if (!0){
45        printf(" this is true conditions");
46    }
47
48
49    // printf("hi");
50}

To find the greatest of all of the given values.

 1#include <stdio.h>
 2int main()
 3{
 4    float a, b, c;
 5    printf("enter the values:\n");
 6    scanf("%f%f%f", &a, &b, &c);
 7    if (a > b && a > c)
 8    {
 9        printf("the greatest of all is %f", a);
10    }
11    else if (b > a && b > c)
12    {
13        printf("the greatest of all is %f", b);
14    }
15    else
16    {
17        printf("greatest of all is %f", c);
18    }
19    return 0;
20}

To print the min and max from the list.

 1#include <stdio.h>
 2int main()
 3{
 4    int a[10], n, i, max = 0,min=a[0];
 5    printf("\nenter the size of the list");
 6    scanf("%d", &n);
 7    printf("enter the elements of the list");
 8    for (i = 0; i < n; i++)
 9    {
10        scanf("%d", &a[i]);
11    }
12    printf("\nthe given list is");
13    for (i = 0; i < n; i++)
14    {
15        printf("%d", a[i]);
16        printf("\n");
17    }
18    for (i = 0; i < n; i++)
19    {
20        if (a[i] > max)
21        {
22            max = a[i];
23        }
24        if (a[i] < min)
25        {   
26            min = a[i];
27        }
28        
29    }
30    printf("\nmax is %d",max);
31    printf("\nmin is %d",min);
32
33    return 0;
34}

To print the multiplication table of a given number.

 1#include <stdio.h>
 2int main()
 3{
 4    int i,j,count;
 5    printf("enter the number and the number of rows:");
 6    scanf("%d%d", &i, &j);
 7    for(count=1;count<=j;count++){
 8        printf(" \n%d * %d = %d", i, count, i * count);
 9    }
10    return 0;
11}

To find the palindrome of a given value.

 1#include <stdio.h>
 2int main()
 3{   unsigned n;
 4    printf("enter the value");
 5    scanf("%d", &n);
 6    int rem,num;
 7    int rev=0;
 8    num=n;
 9    while(num!=0){
10        rem=num%10;
11        rev=rev * 10+rem;
12        num=num/10;
13    }
14    printf("reverse is %d", rev);
15    if(rev==n){
16        printf("\npalindrome");
17
18    }
19    else{
20        printf("not palindrome\n");
21    }
22    return 0;
23}

To check whether the given number is prime or not.

 1#include <stdio.h>
 2int main()
 3{
 4    int i, count;
 5    printf("enter the number:");
 6    scanf("%d", &i);
 7    int flag = 0;
 8    for (count = 2; count < i; count++)
 9    {
10        if (i % count == 0){
11            flag = 1;
12            break;
13        }
14    }
15
16    if(flag == 0){
17        printf("Provided number is a prime number");
18    }else{
19        printf("Provided number is not a prime number");
20    }
21
22    return 0;
23}

To find the roots of a quadratic equation.

 1#include <stdio.h>
 2#include <math.h>
 3int main()
 4{
 5    double a, b, c, r1, r2;
 6    printf("enter the values for a,b,c:");
 7    scanf("%lf%lf%lf", &a, &b, &c);
 8    double d = b * b - 4 * a * c;
 9    if (d > 0)
10    {
11        r1 = (-b + sqrt(d)) / (2 * a);
12        r2 = (-b - sqrt(d)) / (2 * a);
13        printf("r1=%lf r2=%lf", r1, r2);
14    }
15    else if (d == 0)
16    {
17
18        printf("roots are %lf", b / (2 * a));
19    }
20    else
21    {
22        printf("imaginary roots");
23    }
24}

Range of a value.

1#include<stdio.h>
2int main()
3{
4    int x = 21500000000; //it is out of the range that's why it will print any garbage value
5    printf("the value of x is %d", x);
6    return 0;
7}

To find the reverse of a given number.

 1#include <stdio.h>
 2int main()
 3{
 4    int i, j, k;
 5    printf("ENTER THE NUMBER:");
 6    scanf("%d", &i);
 7    int n = 10;
 8    printf("%d", i % 10);
 9    j = i / 10;
10    printf("%d", j % 10);
11    k = i / 100;
12    printf("%d", k);
13
14    return 0;
15}

To calculate simple of compound interest.

 1#include <stdio.h>
 2int main()
 3{
 4    float p, r, t, f;
 5    printf("ENTER THE VALUES FOR P,R,T:");
 6    scanf("%f%f%f", &p, &r, &t);
 7    float s = (p * r * t) / 100;
 8    float d = 1 + r / 100;
 9   
10   
11    int count;
12    float rem;
13    for (count = 1; count < t; count++)
14    {
15        d *= d;
16    }
17    printf("\n SIMPLE INTEREST:%f", s);
18    printf("\n COMPOUND INTEREST:%f", p * d);
19    return 0;
20}

Using sizeof function.

 1#include <stdio.h>
 2int main(){
 3    int a;
 4    float b;
 5    double c;
 6    char d;
 7    printf("\n the size of integer is %ld", sizeof(a));
 8    printf("\n the size of float is %ld", sizeof(b));
 9    printf("\n the size of double is %ld", sizeof(c));
10    printf("\n the size of character is %ld", sizeof(d));
11    return 0;
12
13}

To find the sum of the digits of a number.

 1#include <stdio.h>
 2int main()
 3{   unsigned n;
 4int rem,sum=0;
 5    printf("enter the number:");
 6    scanf("%u", &n);
 7    while(n!=0){
 8        rem=n%10;
 9        sum=sum+rem;
10        n=n/10;
11    }
12    printf("the sum is:%d\n",sum);
13    return 0;
14}