RYTWAYS Fresher Interview Questions with Answers – Part 4

RYTWAYS Fresher Interview Questions with Answers – Part 4 

This blog explains about RYTWAYS Fresher Interview Questions with Answers – Part 4 and is below :

1. Write a C program to check whether a number if Armstrong or not 

 

Check Armstrong Number of n digits

#include <stdio.h>
#include <math.h>int main()
{
int number, originalNumber, remainder, result = 0, n
= 0 ;

printf(“Enter an integer: “);
scanf(“%d”, &number);

originalNumber = number;

while (originalNumber != 0)
{
originalNumber /= 10;
++n;
}

originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, n);
originalNumber /= 10;
}

if(result == number)
printf(“%d is an Armstrong number.”, number);
else
printf(“%d is not an Armstrong number.”,
number);

return 0;
}

Output

Enter an integer: 1634
1634 is an Armstrong number.

2 . Write a C program to check whether  a number is odd or even 

An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24

An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15

Example #1: Program to Check Even or Odd
#include <stdio.h>
int main()
{
int number;

printf(“Enter an integer: “);
scanf(“%d”, &number);

// True if the number is perfectly divisible by 2
if(number % 2 == 0)
printf(“%d is even.”, number);
else
printf(“%d is odd.”, number);

return 0;
}
Output

Enter an integer: -7
-7 is odd.
In the program, integer entered by the user is stored in variable number.
Then, whether the number is perfectly divisible by 2 or not is checked using modulus operator.

If the number is perfectly divisible by 2, test expression number%2 == 0 evaluates to 1 (true) and the number is even.

However, if the test expression evaluates to 0 (false), the number is odd.

 3. Write a C program to print leap years from 1 to N 

This program will read value of N and print all Leap Years from 1 to N years. There are two conditions for leap year: 1- If year is divisible by 400 ( for Century years), 2- If year is divisible by 4 and must not be divisible by 100 (for Non Century years).

 Write a C program to print leap years from 1 to N 

Print all Leap Years from 1 to N using C program
/*C program to print all leap years from 1 to N.*/

#include <stdio.h>

//function to check leap year
int checkLeapYear(int year)
{
if( (year % 400==0)||(year%4==0 && year%100!=0) )
return 1;
else
return 0;
}

int main()
{
int i,n;

printf(“Enter the value of N: “);
scanf(“%d”,&n);

printf(“Leap years from 1 to %d:\n”,n);
for(i=1;i<=n;i++)
{
if(checkLeapYear(i))
printf(“%d\t”,i);
}

return 0;
}
Output

Enter the value of N: 2000
Leap years from 1 to 2000:
4 8 12 16 20 24 28 32 36 40
44 48 52 56 60 64 68 72 76 80
84 88 92 96 104 108 112 116 120 124
128 132 136 140 144 148 152 156 160 164
168 172 176 180 184 188 192 196 204 208

1532 1536 1540 1544 1548 1552 1556 1560 1564 1568
1572 1576 1580 1584 1588 1592 1596 1600 1604 1608
1612 1616 1620 1624 1628 1632 1636 1640 1644 1648
1652 1656 1660 1664 1668 1672 1676 1680 1684 1688
1692 1696 1704 1708 1712 1716 1720 1724 1728 1732
1736 1740 1744 1748 1752 1756 1760 1764 1768 1772
1776 1780 1784 1788 1792 1796 1804 1808 1812 1816
1820 1824 1828 1832 1836 1840 1844 1848 1852 1856
1860 1864 1868 1872 1876 1880 1884 1888 1892 1896
1904 1908 1912 1916 1920 1924 1928 1932 1936 1940
1944 1948 1952 1956 1960 1964 1968 1972 1976 1980
1984 1988 1992 1996 2000

5.  Write a C program to convert Celsius to Fahrenheit 

Program to convert temperature from Celsius to Fahrenheit
/**
* C program to convert temperature from degree celsius to fahrenheit
*/

#include <stdio.h>

int main()
{
float celsius, fahrenheit;

/* Input temperature in celsius */
printf(“Enter temperature in Celsius: “);
scanf(“%f”, &celsius);

/* celsius to fahrenheit conversion formula */
fahrenheit = (celsius * 9 / 5) + 32;

printf(“%.2f Celsius = %.2f Fahrenheit”, celsius, fahrenheit);

return 0;
}
%.2f is used to print fractional numbers up to two decimal places. You can also use %f to print fractional normally up to six decimal places.

Output
Enter temperature in Celsius: 100
100 Celsius = 212.00 Fahrenheit

 6. Write a C program to convert a number from Decimal to Binary .

Decimal to Binary Conversion Algorithm
Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array
Step 2: Divide the number by 2 through / (division operator)
Step 3: Repeat the step 2 until number is greater than 0
Let’s see the c example to convert decimal to binary.

#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system (“cls”);
printf(“Enter the number to convert: “);
scanf(“%d”,&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf(“\nBinary of Given Number is=”);
for(i=i-1;i>=0;i–)
{
printf(“%d”,a[i]);
}
return 0;
}
Output:

Enter the number to convert: 5
Binary of Given Number is=101
 7 . Write a C program to convert a number from Binary to Decimal .
// C++ program to convert binary to decimal
#include<iostream>
using namespace std;

// Function to convert binary to decimal
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;

// Initializing base value to 1, i.e 2^0
int base = 1;

int temp = num;
while (temp)
{
int last_digit = temp % 10;
temp = temp/10;

dec_value += last_digit*base;

base = base*2;
}

return dec_value;
}

// Driver program to test above function
int main()
{
int num = 10101001;

cout <<binaryToDecimal(num)<<endl;
}

Output:
169
REFERENCES : 

https://www.programiz.com/c-programming/examples/check-armstrong-number
https://www.programiz.com/c-programming/examples/even-odd
https://www.includehelp.com/c-programs/c-program-to-print-all-leap-years-from-1-to-n.aspx
https://codeforwin.org/2015/05/c-program-to-convert-celsius-into-fahrenheit.html
https://www.javatpoint.com/c-program-to-convert-decimal-to-binary
https://www.geeksforgeeks.org/program-binary-decimal-conversion/