C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop

Lets write a C program to find sum of squares of all the numbers between 1 and user entered number, using while loop.

Related Read:
while loop in C programming

Logic To Find Sum of Squares of Numbers from 1 to N, using While Loop

We ask the user to enter the limit. We initialize variable count to 1. Now inside while loop we square the value of variable count and add it to the previous value of variable sum – we repeat this for each iteration of while loop until value of count is less than or equal to value entered by the user(which is stored in variable N). For each iteration of the while loop value of variable count is incremented by 1.

Video Tutorial: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop


[youtube https://www.youtube.com/watch?v=YZWcf2lW1sA]

YouTube Link: https://www.youtube.com/watch?v=YZWcf2lW1sA [Watch the Video In Full Screen.]

Source Code: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop

#include < stdio.h >

int main()
{
    int N, count = 1, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &N);

    while(count <= N)
    {
        sum = sum + (count * count);
        count++;
    }
    printf("Sum of squares of numbers from 1 to %d is %d.\n", N, sum);

    return 0;
}

Output:
Enter the limit
5
Sum of squares of numbers from 1 to 5 is 55.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Sizeof Operator in C Programming Language

Lets write a C program to see how to use sizeof() method or function in C programming language.

sizeof() is a builtin method/function present in C programming. It is used to calculate the size(in bytes) that a datatype occupies in the computers memory.

sizeof() method takes a single argument and it is not a function whose value is determined at run time, but rather an operator whose value is determined by compiler – so it’s called as compile time unary operator.

sizeof() method can be used with primitive data type like int, float, char or user defined data types like structures, unions etc.

Video Tutorial: Sizeof Operator in C Programming Language


[youtube https://www.youtube.com/watch?v=Mlw4jJ4WX8A]

YouTube Link: https://www.youtube.com/watch?v=Mlw4jJ4WX8A [Watch the Video In Full Screen.]

Source Code: Sizeof Operator in C Programming Language

#include < stdio.h >

int main()
{
    int a, a1[10];
    char s;
    char s1[10];
    int b = 10, c = 5;

    printf("Size of a single character is %d byes\n", sizeof(s));
    printf("Size of a character array s1[10] is %d byes\n", sizeof(s1));
    printf("Size of a integer is %d byes\n", sizeof(a));
    printf("Size of a long integer is %d byes\n", sizeof(long int));
    printf("Size of a long long integer is %d byes\n", sizeof(long long int));
    printf("Size of a integer array a1[10] is %d byes\n", sizeof(a1));
    printf("Size of a float is %d byes\n", sizeof(float));
    printf("Size of a double is %d byes\n", sizeof(double));
    printf("Size of a long double is %d byes\n", sizeof(long double));
    printf("Size of a b+c is %d byes\n", sizeof(b+c));

    return 0;
}

Output:
Size of a single character is 1 byes
Size of a character array s1[10] is 10 byes
Size of a integer is 4 byes
Size of a long integer is 4 byes
Size of a long long integer is 8 byes
Size of a integer array a1[10] is 40 byes
Size of a float is 4 byes
Size of a double is 8 byes
Size of a long double is 12 byes
Size of a b+c is 4 byes

short, int, long int, long long int

#include < stdio.h >

int main()
{
    printf("Size of short data type is %d\n", sizeof(short));
    printf("Size of integer data type is %d\n", sizeof(int));
    printf("Size of long integer data type is %d\n", sizeof(long int));
    printf("Size of long long integer data type is %d\n", sizeof(long long int));

    return 0;
}

Output:
Size of short data type is 2
Size of integer data type is 4
Size of long integer data type is 4
Size of long long integer data type is 8

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Biggest of 3 Numbers using Binary Minus Operator

Lets write a simple C program to find biggest of three numbers using binary minus operator.

Related Read:
if else statement in C
Nested if else Statement In C

Logic To Find Biggest of 3 Numbers using Binary Minus Operator

If user enters a = 1, b = 2, c = 3;

We check if a – b > 0 and a – c > 0
i.e., 1 – 2 > 0 and 1 – 3 > 0
=> -1 > 0 and -2 > 0 (Both these conditions are false).
So a is not the biggest.

In else block we check if b – c > 0
=> 2 – 3 > 0
=> -1 > 0 (which is false)
So b is not the biggest.

In that case whatever number present in variable c is biggest.

Video Tutorial: C Program To Find Biggest of 3 Numbers using Binary Minus Operator


[youtube https://www.youtube.com/watch?v=2kpHLoBdBjc]

YouTube Link: https://www.youtube.com/watch?v=2kpHLoBdBjc [Watch the Video In Full Screen.]

Source Code: C Program To Find Biggest of 3 Numbers using Binary Minus Operator

#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter 3 numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    if(a-b > 0 && a-c > 0)
    {
        printf("%d is the biggest\n", a);
    }
    else
    {
        if(b-c > 0)
        {
            printf("%d is the biggest\n", b);
        }
        else
        {
            printf("%d is the biggest\n", c);
        }
    }

    return 0;
}

Output 1:
Enter 3 numbers
1
2
3
3 is the biggest

Output 2:
Enter 3 numbers
10
30
20
30 is the biggest

Output 3:
Enter 3 numbers
100
50
75
100 is the biggest

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Octal Number To Binary Number, using While Loop

Lets write a C program to convert a number from Octal number system(base 8) to Binary number system(base 2), using while loop.

In this C program we first convert the user entered number from Octal number system to Decimal number system. Then we take the result(which is in Decimal number system) and convert to Binary number system.

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

Related Read:
while loop in C programming
C Program To Convert Octal Number To Decimal Number, using While Loop
C Program To Convert Decimal Number To Binary Number, using While Loop

Note:
In this C program we are dealing with Octal Number System, Decimal Number System and Binary Number System.

Octal Number System
Octal number system has base 8 and digits 0, 1, 2, 3, 4, 5, 6, 7.
octal number system
Example:
etc .. 84, 83, 82, 81, 80

etc .. 84 = ‭4,096‬, 83 = 512, 82 = 64, 81 = 8, 80 = 1.

Binary Number System
Binary number system has base 2 and digits 0 and 1.
binary decimal number system
Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

Expected Output for the Input

User Input:
Enter an Octal Number
14

Output:
Binary Equivalent of Octal no 14 is 1100.

Video Tutorial: C Program To Convert Octal Number To Binary Number, using While Loop


[youtube https://www.youtube.com/watch?v=_juS8cQRMYs]

YouTube Link: https://www.youtube.com/watch?v=_juS8cQRMYs [Watch the Video In Full Screen.]

Logic To Convert Octal Number To Binary Number, using While Loop

To get complete logic to this program kindly watch these two video tutorials without fail.

1. C Program To Convert Octal Number To Decimal Number, using While Loop
2. C Program To Convert Decimal Number To Binary Number, using While Loop

Source Code: C Program To Convert Octal Number To Binary Number, using While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    int  num, dec = 0, rem = 0, place = 0;
    long bin = 0;

    printf("Enter an Octal Number\n");
    scanf("%d", &num);

    printf("Binary Equivalent of Octal no %d is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * pow(8, place);
        num = num / 10;
        place++;
    }

    place = 1;
    rem   = 0;
    while(dec)
    {
        rem   = dec % 2;
        bin   = bin + (rem * place);
        dec   = dec / 2;
        place = place * 10;
    }
    printf("%ld.\n", bin);

    return 0;
}

Output 1:
Enter an Octal Number
71
Binary Equivalent of Octal no 71 is 111001.

Output 2:
Enter an Octal Number
11
Binary Equivalent of Octal no 11 is 1001.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Binary Number To Octal Number, using While Loop

Lets write a C program to convert a number from Binary number system(base 2) to Octal number system(base 8), using while loop.

In this C program we first convert the user entered number from Binary number system to Decimal number system. Then we take the result(which is in Decimal number system) and convert to Octal number system.

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

Related Read:
while loop in C programming
C Program To Convert Binary Number To Decimal Number, using While Loop
C Program To Convert Decimal Number To Octal Number, using While Loop

Note:
In this C program we are dealing with Binary Number System, Decimal Number System and Octal Number System.

Binary Number System
Binary number system has base 2 and digits 0 and 1.
binary decimal number system
Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

Octal Number System
Octal number system has base 8 and digits 0, 1, 2, 3, 4, 5, 6, 7.
octal number system
Example:
etc .. 84, 83, 82, 81, 80

etc .. 84 = ‭4,096‬, 83 = 512, 82 = 64, 81 = 8, 80 = 1.

Expected Output for the Input

User Input:
Enter a Binary Number
111001

Output:
Octal Equivalent of Binary no 111001 is 71.

Video Tutorial: C Program To Convert Binary Number To Octal Number, using While Loop


[youtube https://www.youtube.com/watch?v=W1T6Jm41fok]

YouTube Link: https://www.youtube.com/watch?v=W1T6Jm41fok [Watch the Video In Full Screen.]

Logic To Convert Binary Number To Octal Number, using While Loop

To get complete logic to this program kindly watch these two video tutorials without fail.

1. C Program To Convert Binary Number To Decimal Number, using While Loop
2. C Program To Convert Decimal Number To Octal Number, using While Loop

Source Code: C Program To Convert Binary Number To Octal Number, using While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    long num;
    int  dec = 0, oct = 0, rem = 0, place = 0;

    printf("Enter a Binary Number\n");
    scanf("%ld", &num);

    printf("\nOctal Equivalent of Binary no %ld is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * pow(2, place);
        num = num / 10;
        place++;
    }

    place = 1;
    rem   = 0;

    while(dec)
    {
        rem   = dec % 8;
        oct   = oct + rem * place;
        dec   = dec / 8;
        place = place * 10;
    }
    printf("%d.\n", oct);

    return 0;
}

Output 1:
Enter a Binary Number
1111

Octal Equivalent of Binary no 1111 is 17.

Output 2:
Enter a Binary Number
111001

Octal Equivalent of Binary no 111001 is 71.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert