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.
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.
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:
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;
}
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.
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.
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.
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 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
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.