C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array

Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, using Ternary Operator or Conditional Operator and without using arrays.

Related Read:
while loop in C programming
Positive or Negative or Zero Using Ternary Operator: C Program

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

scale

Expected Output for the Input

User Input:
Enter the limit
6
Enter 6 numbers
-1
0
2
-3
5
6

Output:
Positive Numbers: 3
Negative Numbers: 2
Number of zero: 1

Logic To Count Positive, Negative and Zero using Ternary Operator and without using Array

Complete logic to this program is present at C Program To Count Positive, Negative and Zero without using Array

Video Tutorial: C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array


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

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

Source Code: C Program to Count Positive, Negative and Zero using Ternary Operator and without using Array

#include < stdio.h >

int main()
{
    int limit, num, positive = 0, negative = 0, zero = 0;

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

    printf("Enter %d numbers\n", limit);

    while(limit)
    {
        scanf("%d", &num);
        (num > 0) ? positive++ : ((num < 0) ? negative++ : zero ++);
        limit--;
    }

    printf("\nPositive Numbers: %d\n", positive);
    printf("Negative Numbers: %d\n", negative);
    printf("Number of zero: %d\n", zero);

    return 0;
}

Output:
Enter the limit
5
Enter 5 numbers
2
-1
5
6
0

Positive Numbers: 3
Negative Numbers: 1
Number of zero: 1

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 The Day on 01 January using Gregorian Calendar

According to the Gregorian Calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

Note: From 01/01 we understand that it’s 1st of January. We shall take the year as 1900. We also know that 01 January 1900 was Monday.

Related Read
while loop in C programming
C Program To Check Leap Year
Simple Calculator Program using Switch Case: C

Gregorian Calendar: the calendar introduced in 1582 by Pope Gregory XIII, as a modification of the Julian Calendar.

Expected Output for the Input

User Input:
Enter a year between 1900 and 2099
2020

Output:
The day on 01 January 2020 was Wednesday.

Video Tutorial: C Program To Find The Day on 01 January in Gregorian Calendar


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

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

Logic To Find The Day on 01 January using Gregorian Calendar

We take reference year as 1900. We ask the user to enter a year between 1900 and 2099 and store it inside the address of variable year. Now we find the difference (1900 – year) and store it inside variable diff.

Using while loop, we loop through until reference year(1900) is less than the user entered year. Inside the while loop we check for all the leap years present from 1900 to user entered year.

Next we calculate exact number of days between 1900 and user entered year using formula:

diff = ref_year – year;
Total no of leap years is calculated and stored in variable leap.

Total_days = (diff – leap) x 365 + leap x 366;

Now every year has 12 months, but each month has different number of days. But every week has exactly 7 days. So we divide Total_days by 7 and store the reminder inside variable day.

Note: Since our reference year 1900 has Monday on 1st of January. So we take Monday as first day:

0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday

We make use of Switch case to match the number in variable day to the day of the week.

Source Code: C Program To Find The Day on 01 January using Gregorian Calendar

#include < stdio.h >

int main()
{
    int ref_year = 1900, year, leap = 0, diff, total_days = 0, day = 0;

    printf("Enter a year between 1900 and 2099\n");
    scanf("%d", &year);

    diff = year - ref_year;

    while(ref_year < year)
    {
        if(ref_year % 100 == 0)
        {
            if(ref_year % 400 == 0)
            {
                leap++;
            }
        }
        else
        {
            if(ref_year % 4 == 0)
            {
                leap++;
            }
        }
        ref_year++;
    }

    total_days = (diff - leap) * 365 + leap * 366;
    day        = total_days % 7;

    printf("\nThe day on 01 January %d was ", year);

    switch(day)
    {
        case 0: printf("Monday.\n");
                break;
        case 1: printf("Tuesday.\n");
                break;
        case 2: printf("Wednesday.\n");
                break;
        case 3: printf("Thursday.\n");
                break;
        case 4: printf("Friday.\n");
                break;
        case 5: printf("Saturday.\n");
                break;
        case 6: printf("Sunday.\n");
                break;
    }

    return 0;
}

Output 1:
Enter a year between 1900 and 2099
1900

The day on 01 January 1900 was Monday.

Output 2:
Enter a year between 1900 and 2099
2015

The day on 01 January 2015 was Thursday.

Output 3:
Enter a year between 1900 and 2099
2016

The day on 01 January 2016 was Friday.

Output 4:
Enter a year between 1900 and 2099
2017

The day on 01 January 2017 was Sunday.

Output 5:
Enter a year between 1900 and 2099
2018

The day on 01 January 2018 was Monday.

Lets Use Ternary / Conditional Operator To Find Leap Year

We modify above program and use Ternary Operator or Conditional Operator to find leap year. You can find detailed explanation of finding leap year using Conditional Operator here: C Program To Check Leap Year Using Ternary Operator

Source Code: C Program To Find The Day on 01 January using Gregorian Calendar Using Ternary or Conditional Operator

#include < stdio.h >

int main()
{
    int ref_year = 1900, year, leap = 0, nonleap = 0, total_days = 0, day = 0;

    printf("Enter a year between 1900 and 2099\n");
    scanf("%d", &year);

    while(ref_year < year)
    {
        (ref_year % 100 == 0) ?
        ( (ref_year % 400 == 0)?
          (leap++):
          (nonleap++)
        ) :
        ( (ref_year % 4 == 0)?
          (leap++):
          (nonleap++)
        );

        ref_year++;
    }

    total_days = nonleap * 365 + leap * 366;
    day        = total_days % 7;

    printf("\nThe day on 01 January %d was ", year);

    switch(day)
    {
        case 0: printf("Monday.\n");
                break;
        case 1: printf("Tuesday.\n");
                break;
        case 2: printf("Wednesday.\n");
                break;
        case 3: printf("Thursday.\n");
                break;
        case 4: printf("Friday.\n");
                break;
        case 5: printf("Saturday.\n");
                break;
        case 6: printf("Sunday.\n");
                break;
    }

    return 0;
}

Output 1:
Enter a year between 1900 and 2099
2019

The day on 01 January 2019 was Tuesday.

Output 2:
Enter a year between 1900 and 2099
2020

The day on 01 January 2020 was Wednesday.

Output 3:
Enter a year between 1900 and 2099
2021

The day on 01 January 2021 was Friday.

Output 4:
Enter a year between 1900 and 2099
2022

The day on 01 January 2022 was Saturday.

Output 5:
Enter a year between 1900 and 2099
2023

The day on 01 January 2023 was Sunday.

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 GCD and LCM of Two Numbers

Lets write a C program to find GCD(Greatest Common Divisor) or HCF(Highest common Factor) and LCM(Least Common Multiple) of 2 user entered integer numbers.

Related Read:
while loop in C programming
Relational Operators In C
Logical Operators In C

GCD or HCF: Largest Integer that can divide both the numbers without any remainder or with 0 as remainder.
C Program to Find GCD or HCF of Two Numbers

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.
C Program to Find LCM of Two Numbers
Biggest of Two Numbers Using Ternary Operator: C

Logic To Find GCD and LCM of Two Integer Numbers.

We ask the user to enter 2 integer numbers. Next we find the smallest number among the two. Example, if num1 = 2 and num2 = 3. We can’t have any number bigger than 2, which will divide num1 and have reminder as 0.

Further explanation to find GCD or HCF is present in detail in this article: C Program to Find GCD or HCF of Two Numbers

Formula To Calculate LCM

Once we get the GCD, we use the below formula to calculate LCM.

LCM = ( num1 * num2 ) / GCD;

Source Code: C Program To Find GCD and LCM of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, lcm, count = 1, small;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &num1, &num2);

    small = (num1 < num2) ? num1 : num2;

    while(count <= small)
    {
        if(num1 % count == 0 && num2 % count == 0)
        {
            gcd = count;
        }
        count++;
    }

    lcm = ( num1 * num2 ) / gcd;

    printf("GCD = %d\nLCM = %d\n", gcd, lcm);

    return 0;
}

Output 1:
Enter 2 integer numbers
30
40
GCD = 10
LCM = 120

Output 2:
Enter 2 integer numbers
12
24
GCD = 12
LCM = 24

C Program To Find GCD and LCM of Two Numbers


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

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


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 LCM of Two Numbers

Lets write a C program to find Least Common Multiple (LCM) of 2 integer numbers.

Related Read:
while loop in C programming
Biggest of Two Numbers Using Ternary Operator: C

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.

Logic to Find LCM of Two Numbers

We assign variable fact with a initial value of 1. While loop keeps executing until fact value is 0. We ask the user to enter 2 integer numbers. Now we find the biggest of these 2 numbers and store it inside variable lcm. We can even skip this step – in which case while loop has to execute more number of times. To optimize the code we find biggest of the 2 user input numbers and store it inside variable lcm – we do this because, we need to find a number which is perfectly divisible by both the user entered number, so obviously the number should be greater than or equal to the biggest number entered by the user.

Next, inside while loop we check if the value present in variable lcm is perfectly divisible by both num1 and num2. Once the condition is true, we display the result on to the console window and assign value 0 to variable fact. Once fact is 0, control exits while loop.

If the value of lcm is not perfectly divisible by both num1 and num2, then we increment the value of variable lcm by 1 and check for divisibility once again. We keep executing the while loop and keep incrementing the value of lcm by 1 until the value present in lcm is perfectly divisibile by value of num1 and num2.

Source Code: C Program to Find LCM of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, lcm, fact =1;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &num1, &num2);

    lcm = (num1 > num2) ? num1 : num2;

    while(fact)
    {
        if(lcm % num1 == 0 && lcm % num2 == 0)
        {
            printf("LCM of %d and %d is %d\n", num1, num2, lcm);
            fact = 0;
        }
        lcm++;
    }

    return 0;
}

Output 1:
Enter 2 numbers
24
60
LCM of 24 and 60 is 120

Output 2:
Enter 2 numbers
2
3
LCM of 2 and 3 is 6

C Program to Find LCM of Two Numbers


[youtube https://www.youtube.com/watch?v=XwSf-rvdkzo]

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


Explanation With Example

If 2 numbers are num1 = 2 and num2 = 3.
We find biggest of these two numbers and store it inside variable lcm.
So 3 is greater than 2, so lcm = 3.

Iteration 1
fact = 1;
lcm = 3;

lcm % num1 = 3 % 2 = 1 // not equal to 0
lcm % num2 = 3 % 3 = 0 // equal to 0

Increment value of lcm by 1. So lcm = 4.

Iteration 2
fact = 1;
lcm = 4;

lcm % num1 = 4 % 2 = 0 // equal to 0
lcm % num2 = 4 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 5.

Iteration 3
fact = 1;
lcm = 5;

lcm % num1 = 5 % 2 = 2 // not equal to 0
lcm % num2 = 5 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 6.

Iteration 3
fact = 1;
lcm = 6;

lcm % num1 = 6 % 2 = 0 // equal to 0
lcm % num2 = 6 % 3 = 0 // equal to 0

Since both the condition are true
i.e., lcm%num1 == 0 && lcm%num2 == 0

We printout the value of variable lcm i.e., 6 as the Least Common Multiple of numbers 2 and 3. Next we assign 0 to variable fact. Since fact is 0, control exits while loop.

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 Expert0

C Program to Calculate Generic Root of a Number using Ternary Operator

Lets write a C program to calculate Generic Root of a Number using Ternary Operator or Conditional Operator.

Related Read:
C Program to Find Generic Root of a Number
Ternary Operator / Conditional Operator In C

For Example: If user input number is 65987, then we add all the individual digits of the number i.e., 6 + 5 + 9 + 8 + 7 = 35. We got 35. Now we add individual digits of number 35 i.e., 3 + 5 = 8. So Generic Root of number 65987 is 8.

Source Code: C Program to Calculate Generic Root of a Number using Ternary Operator

 
#include < stdio.h >

int main()
{
    int num, res;

    printf("Enter a number above 10\n");
    scanf("%d", &num);

    printf("Generic Root of %d is %d\n", num, (res = num % 9) ? res : 9 );

    return 0;
}

Output 1:
Enter a number above 10
65987
Generic Root of 65987 is 8

Output 2:
Enter a number above 10
8
Generic Root of 8 is 8

Output 3:
Enter a number above 10
456
Generic Root of 456 is 6

Output 4:
Enter a number above 10
78910
Generic Root of 78910 is 7

Output 5:
Enter a number above 10
5555
Generic Root of 5555 is 2

C Program to Calculate Generic Root of a Number using Ternary Operator


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

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


Logic To Find Generic Root of a Number using Ternary Operator

(result = num % 9) ? result : 9;

OR

(num % 9) ? (num % 9) : 9;

We divide the user input number by 9 and store the remainder inside variable result. If the number is perfectly divisible by 9 or if the result is zero then we output 9 orelse we output the value present in variable result. Variable result will have the Generic Root of the user entered number. We are applying modular division on the user entered number. And we are dividing it by 9 because 9 is the biggest single digit number and by modulo division of number by 9 we get the Generic Root of the number.

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