0% found this document useful (0 votes)
39 views45 pages

Top 100 Programs Part 2

Uploaded by

M. Jeevamukesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views45 pages

Top 100 Programs Part 2

Uploaded by

M. Jeevamukesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Question 1:

Write a C program to find HCF of Two Numbers

The HCF or the Highest Common Factor of two numbers is the largest common
factor of two or more values. The HCF can be calculated using some simple
mathematical tricks. The following algorithm will determine how a c program
can calculate the HCF of two numbers.

#include <stdio.h>
int main()
{
//for initialize variables
int a, b, i, hcf;
a = 12;
b = 16;
//find hcf of number
for(i = 1; i <= a || i <= b; i++)
{
if( a%i == 0 && b%i == 0 )
hcf = i;
}
//display hcf
printf("HCF = %d", hcf);
return 0;
}
Question 2:
Write a C program to find the LCM of two number.

The Least Common Multiple (LCM) is also referred to as the Lowest Common
Multiple (LCM) and Least Common Denominator (LCD). The least common
multiple, or LCM, is another number that’s useful in solving many math
problems. Let’s find the LCM of 12 and 44. One way to find the least common
multiple of two numbers is to first list the prime factors of each number.

12 = 2 × 2 × 3

44 = 2 × 2 × 11

A C program can calculate the Lowest Common Multiple (LCM) of two


numbers. The method includes finding out the maximum values among two
numbers, which are common in both the numbers. The algorithm below will
help to calculate the LCM of two numbers.

/** C program to calculate the LCM of two numbers */

#include<stdio.h>

void lcm_two_no(int,int);
int main()
{
int n1,n2;

//to take user input n1,n2


printf("Enter two numbers: ");
scanf("%d %d",&n1,&n2);

//call of user define function


lcm_two_no(n1,n2);
return 0;
}

//function to calculate l.c.m


void lcm_two_no(int n1,int n2)
{
int check1,check2;
//to use of duplicity value
check1=n1;
check2=n2;

//to find lcm of number


while(check1!=check2)
{
//for condition true
if(check1< check2
check1=check1+n1;

//for condition false


else
check2=check2+n2;
}
printf("\nL.C.M of %d and %d is: %d",n1,n2,check1);
}

Question 3:
Write a C program to find GCD or HCF of Two Numbers

The Highest Common Multiple or the Greatest Common Divisor is the greatest
number that exactly divides both numbers. It is possible to calculate this
number through simple mathematical calculations. The following algorithm
shows how the GCD of two numbers is calculated.

Ex:-

the H.C.F or G.C.D of 12 and 14 is 2.

The H.C.F or G.C.D of 16 and 12 is 4

// C program to calculate GCD of two numbers


#include<stdio.h>
// The code used a recursive function to return gcd of p and q
int gcd(int p, int q)
{

// checking divisibility by 0
if (p == 0)
return q;

if (q == 0)
return p;
// base case
if (p == q)
return p;

// p is greater
if (p > q)
return gcd(p-q, q);

else
return gcd(p, q-p);
}

// Driver program to test above function


int main()
{
int p = 98, q = 56;
printf("GCD of %d and %d is %d ", p, q, gcd(p, q));
return 0;
}

Question 4:
Binary to decimal conversion:-

The C program converts binary number to decimal number that is equivalent.


A decimal number can be attained by multiplying every digit of binary digit
with power of 2 and totaling each multiplication outcome. The power of the
integer starts from 0 and counts to n-1 where n is assumed as the over-all
number of integers in binary number.

Ex:- (101100001) 2 =(353)10

To show on fig(1)

/** C program to convert the given binary number into decimal**/

#include<stdio.h>

int main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;

printf("Insert a binary num (1s and 0s) \n");


scanf("%d", &num); /* maximum five digits */

binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
//num/=10;
num = num / 10 ;
//base*=2;
base = base * 2;
}
//display binary number
printf("The Binary num is = %d \n", binary_val);
//display decimal number
printf("Its decimal equivalent is = %d \n", decimal_val);
return 0;
}

Question 5:
Binary to Octal Conversion:-

Binary to octal conversion can be easily done with the help of simple
calculations. The following section includes a stepwise procedure for such a
conversion. In this process, a binary number is inputted by a user and is later
converted to an octal number.

/** C Program to Convert Binary to Octal*/

#include<stdio.h>

int main()

{
//For initialize variables
long int binary_num, octal_num = 0, j = 1, rem;
//Inserting the binary number from the user

printf("Enter a binary number: ");


scanf("%ld", &binary_num);

// while loop for number conversion

while(binary_num != 0)
{

rem = binary_num % 10;


octal_num = octal_num + rem * j;
//j*=2
j = j * 2;
//binary_num/10;
binary_num = binary_num / 10;

printf("Equivalent octal value: %ld", octal_num);

return 0;
}
Question 6:
Decimal to binary conversion:-

The C program to convert decimal number into binary number is done by


counting the number of 1s. The program practices module process and
multiplication with base 2 operation for converting decimal into binary
number. It further uses modulo operation to check for 1’s and hence increases
the amount of 1s. The program reads an integer number in decimal in order to
change or convert into binary number.

Ex:- (180)10=(11101000)2

/*
* C program to accept a decimal number and convert it to binary
* and count the number of 1's in the binary number
*/

#include<stdio.h>
int main()
{
//for initialize a variables
long number, dec_num, rem, base = 1, bin = 0, count = 0;
//To insert a number
printf("Insert a decimal num \n");
scanf("%ld", &number);
dec_num = number;
while(number > 0)
{
rem = number % 2;
/* To count no.of 1s */
if (rem == 1)
{
count++;
}

bin = bin + rem * base;


//number/=2;
number = number / 2;
base = base * 10;
}
//display
printf("Input num is = %d\n", dec_num);
printf("Its binary equivalent is = %ld\n", bin);
printf("Num of 1's in the binary num is = %d\n", count);
return 0;
}

Question 7:
Decimal to Octal Conversion:-

The C program to convert decimal to octal number accepts a decimal number


from the user. This number is further converted to its equivalent octal number
after following a series of steps. The following section gives an algorithm for
this conversion. It is then followed by a C program.
Ex:- If a Decimal number is convert of octal number we use this method that is
show in fig(1).

(143)10=(217)8

//Program to convert Decimal number into octal number


#include<stdio.h>

int main()
{
//Variable initialization
long dec_num, rem, quotient;
int i, j, octalno[100];
//Taking input from user
printf("Enter a number for conversion: ");
//Storing the value in dec_num variable
scanf("%ld",&dec_num);
quotient = dec_num;
i=1;
//Storing the octal value in octalno[] array
while (quotient!=0)
{
octalno[i++]=quotient%8;
quotient=quotient/8;
}
//Printing the octalno [] in reverse order
printf("\nThe Octal of %ld is:\n\n",dec_num);
for (j=i-1;j>0;j--)
//display it
printf ("%d", octalno[j]);
return 0;
}

Question 8:
Octal to binary conversion:-

The C program helps to convert octal number to binary number. In this


program, the user is asked to insert an octal number and by using a loop or if-
else statement, the user can convert an octal number to binary number. An
integer variable is required to be used in the program.

/*
* C Program to Convert Octal to Binary number
*/
#include<stdio.h>
#include<conio.h>

#define MAX 1000

int main()
{
char octalnum[MAX];
//For initialize
long i = 0;
//taking user input of octalnum
printf("Insert an octal number: ");
scanf("%s", octalnum);
printf("Equivalent binary number: ");
while (octalnum[i])
{
//use switch case for multiple condition
switch (octalnum[i])
{
case '0':
printf("000"); break;
case '1':
printf("001"); break;
case '2':
printf("010"); break;
case '3':
printf("011"); break;
case '4':
printf("100"); break;
case '5':
printf("101"); break;
case '6':
printf("110"); break;
case '7':
printf("111"); break;
//for invalid case
default:
printf("\n Invalid octal digit %c ", octalnum[i]);
return 0;
}
i++;
}
return 0;
}

Question 9:
Octal to Decimal Conversion:-

It is easy to convert an octal number to decimal number. For this, the user is
asked to enter an octal number which is converted to a decimal number
following a series of steps. The algorithm below illustrates this process in a
step wise process. It is then followed by a C program that converts an Octal
number to a decimal number.

/** C Program to Convert Octal to Decimal */

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

int main()
{
//Variable Initialization
long int oct, dec = 0;
int i = 0;
//Taking input from user
printf("Enter an octal number: ");
scanf("%ld", &oct);
//Conversion of octal to decimal
while(oct != 0)
{
dec = dec +(oct % 10)* pow(8, i++);
//oct/=10;
oct = oct / 10;
}
//display
printf("Equivalent decimal number: %ld",dec);
return 0;
}

Question 10:
Write a C program to find Quadrants in which coordinates lie:-

The C program reads the coordinate point in a xy coordinate system and


identify the quadrant. The program takes X and Y. On the basis of x and y
value, the program will identify on which quadrant the point lies. The program
will read the value of x and y variable. If-else condition is used to identify the
quadrant of the given value.
Ex:- X and Y coordinates are 20, 30 these lie in 4th quadrant because in
mathematics quadrants rules are following

 x is positive integer and y is also positive integer so-that quadrant is a


first quadrant.
 x is negative integer and y is positive integer so-that Quadrant is a
second quadrant.
 x is negative integer and y is also negative integer so -that Quadrant is a
third quadrant.
 x is positive integer and y is negative integer so-that is a first quadrant.

#include<stdio.h>
int main()
{
//for initialization of coordinates
int x, y; //user input
printf("Insert the value for variable X and Y\n");
scanf("%d %d", &x, &y);
//find true condition of first quadrant
if (x > 0 && y > 0)
printf("point (%d, %d) lies in the First quadrant\n",x,y);
//find second quadrant
else if (x < 0 && y > 0)
printf("point (%d, %d) lies in the Second quadrant\n",x,y);
//To find third quadrant
else if (x < 0 && y < 0)
printf("point (%d, %d) lies in the Third quadrant\n",x,y);
//To find Fourth quadrant
else if (x > 0 && y < 0)
printf("point (%d, %d) lies in the Fourth quandrant\n",x,y);
//To find dose not lie on origin
else if (x == 0 && y == 0)
printf("point (%d, %d) lies at the origin\n",x,y);
return 0;
}

Question 11:

Permutations in which n people can occupy r seats in a classroom:-

C programming helps in identifying the r number of seats that can be occupied


by n number of people. Such a program is known as the possible permutations.
Here, We need to write a code to find all the possible permutations in which n
people can occupy r number of seats in a classroom/theater.

N students are looking to find r seats in a classroom. Some of the seats are
already occupied and only a few can accommodate in the classroom. The
available seats are assumed as r and n number of people are looking to
accommodate within the room.

#include<stdio.h>

// Program to find the factorial of the number


int factorial (long int x)
{
long int fact=1,i;
for(i=1;i<=x;i++)
{
fact=fact*i;
}
return fact;
}
int main()
{
long int n,r,permutation,temp;
long int numerator, denominator;

// Insert the num of people

printf("\nEnter the number of persons : ");

scanf("%ld",&n);

// Insert the num of seats

printf("\nEnter the number of seats available : ");

scanf("%ld",&r);
// Base condition
// Swap n and r when n is less than r

if(n < r)
{
temp=n;
n=r;
r=temp;
}
numerator=fact(n);
denominator=fact(n-r);
permutation=numerator/denominator;
printf("\nNum of ways people can be seated : ");
printf("%ld\n",permutation);
}
Question 12:
Write a C program to find Maximum number of handshakes:-

In C programming, there’s a program to calculate the number of handshakes.


The user is asked to take a number as integer n and find out the possible
number of handshakes. For example, if there are n number of people in a
meeting and find the possible number of handshakes made by the person who
entered the room after all were settled.

Working:-
Step 1: Start

Step 2: The user is asked to insert an integer value n, representing the number
of people

Step 3: Find nC2, and calculate as n * (n – 1) / 2.


Step 4: Print the outcome derived from the above program

Step 5: Stop

// C program to find the maximum number of handshakesM


#include<stdio.h>
int main()
{
//fill the code
int num;
scanf("%d",&num);
int total = num * (num-1) / 2; // Combination nC2
printf("%d",total);
return 0;
}

Question 13:

Write a C program to Add two fractions.

In this C program we will find sum of two fraction using C

To find the sum of two fractions we will be using the concept of LCM and GCD.

For example: we have to find the sum of 6/2 and 16/3


Firstly the LCM of 2 and 3 will be found. Using the LCM we will convert the
numerators i.e. 6 and 16 into digits that can be added and sum of those digits
is found, lastly normalization is done using the GCD of sum and LCM.

Working:-
Step 1. Start.

Step 2.Initialize variables of numerator and denominator

Step 3. Take user input of two fraction

Step 4. Find numerator using this condition (n1*d2) +(d1*n2 ) where n1,n2 are
numerator and d1 andd d2 are denominator .

Step 5. Find denominator using this condition (d1*d2) for lcm.

Step 6. Calculate GCD of a this new numerator and denominator .

Step 7. Display a two value of this condition x/gcd,y/gcd);

Step 8. Stop.

#include <stdio.h>
int main()
{
//for initialize variables
int numerator1, denominator1,numerator2,denominator2,x,y,c,gcd_no;
//To take user input of numerators and denominators
printf("\nEnter the numerator for 1st number : ");
scanf("%d",&numerator1);
printf("\nEnter the denominator for 1st number : ");
scanf("%d",&denominator1);
printf("\nEnter the numerator for 2nd number : ");
scanf("%d",&numerator2);
printf("\nEnter the denominator for 2nd number : ");
scanf("%d",&denominator2);
//numerator
x=(numerator1*denominator2)+(denominator1*numerator2);
//denominator
y=denominator1*denominator2;
// Trick part. Reduce it to the simplest form by using gcd.
for(c=1; c <= x && c <= y; ++c)
{
if(x%c==0 && y%c==0)
gcd_no = c;
}
//To display fraction of givien numerators and denominators
printf("\nThe added fraction is %d/%d ",x/gcd_no,y/gcd_no);
printf("\n");
return 0;
}

Question 14:
Replace all 0’s with 1 in a given integer:-
The replace all program in C programming works to replace the numbers with
zero, where the number must be an integer. All the zeros (if encountered) in
the given program will be replaced by 1.

Ex- number is 12004509 all 0’s are replays of 1’s so number is 12114519.

// C program to replace all 0’s with 1 in a given integer


#include
int replace (long int num)
{
// Base case for recursion termination
if (num == 0)
return 0;
// Extract the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 1;
// Convert remaining digits and append the last digit

return replace(num/10) * 10 + digit;


}
int Convert(long int num)
{
if (num == 0)
return 1;
else
return replace(num);
}
int main()
{
long int num;
//To take user input
printf("\nInsert the num : ");
scanf("%d", &number);
//display final result
printf("\n Num after replacement : %d\n", Convert (num));
return 0;
}

Question 15:

Write a Program to express a given Integer as a sum of two Prime Numbers?

The program in C takes a positive integer or number which is required to be


inserted by the user. The program further identifies whether that digit
can be expressed as the sum of two prime numbers. If the inserted number
can be expressed as sum of two prime numbers then, print the integer can
be expressed as sum of two prime numbers as a result.

// C program to check whether a number can be expressed as a sum of two


prime numbers

#include<stdio.h>
int sum_of_two_primes(int n);
int main()
{
int n, i;

printf(“Insert the num: “);


scanf(“%d”, &n);
int flag = 0;
for(i = 2; i <= n/2; ++i)
{
// Condition for i to be prime
if(sum_of_two_primes(i) == 1)
{
if(sum_of_two_primes(n-i) == 1)
{
printf(“\n%d can be expressed as the sum of %d and %d\n\n”, n, i, n
– i);
flag = 1;
}

}
}

if(flag == 0)
printf(“%d cannot be expressed as the sum of two primes\n”, n)

return 0;
}
int sum_of_two_primes(int n)
{
int i, isPrime = 1;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
return isPrime;
}
Question 16:
Count possible decodings of a given digit sequence:-
The decoding programs are the most possible questions asked and are largely
practiced in C programming. The program counts the number of possible
decodings of the entered digit by the user of the given sequence.
For example, if the digit sequence is “12” then there are two possible
decodings for this – One of them is ‘AB’ when we decode 1 as ‘A’ and 2 as ‘B’.
Now we can also decode this digit sequence “12” as ‘L’ when we decode digits
1 and 2 taken together as an integer 12.

//C Program to Count possible decodings of a given digit sequence


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

int cnt_decoding_digits(char *dig, int a)


{
// Initializing an array to store results
int cnt[a+1];
cnt[0] = 1;
cnt[1] = 1;

for (int k = 2; k <= a; k++) { cnt[k] = 0;


// If the last digit not equal to 0, then last digit must added to the number of
words if (dig[k-1] > '0')
cnt[k] = cnt[k-1];

// In case second last digit is smaller than 2 and last digit is smaller than 7,
then last two digits form a valid character
if (dig[k-2] == '1' || (dig[k-2] == '2' && dig[k-1] < '7') )
cnt[k] += cnt[k-2];
}
return cnt[a];
}

int main()
{
char dig[15];
printf("\n Enter the sequence : ");
gets(dig);
int a = strlen(dig);
printf("\n Possible count of decoding of the sequence : %d\n",
cnt_decoding_digits(dig, a));
return 0;
}

Question 17:
A character is a vowel or consonant using C
Given a character, check if it is vowel or consonant. Vowels are in Uppercase
‘A’, ‘E’, ‘I’, ‘O’, ‘U’ and Lowercase ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. and All other characters
both uppercase and lowercase (‘B’, ‘C’, ‘D’, ‘F’,’ b’, ‘c’,’ d’, ‘f’,…..) are
consonants. In this article, we will show you, How to write a C program to
check Vowel or Consonant with an example.

#include <stdio.h>
int main()
{
char c;
int isLowerVowel, isUpperVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);

//To find the corrector is lowercase vowel


isLowerVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
//To find the character is Upper case vowel
isUpperVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// compare to charector is Lowercase Vowel or Upper case Vowel

if (isLowerVowel || isUpperVowel)
printf("%c is a vowel", c);
//to check character is alphabet or not

elseif((c >= 'a' && c= 'A' && c <= 'Z'))


prinf("\n not a alphabet\n");

else
printf("%c is a consonant", c);

return 0;
}

Question 18:
Character is alphabet or not:
The C program checks whether the character entered is an alphabet or not.
The program makes use of character value inserted by the user. This value can
range between lowercase or uppercase alphabets, such as ‘a’ and <= ‘z’ and ‘A’
and <= ’Z’. If the character inserted by the user lies between the above
category or ranges then the character is an alphabet and if it does not lie
within the given range then it is not.

//C Program to find character is alphabet or not


#include<stdio.h>
#include<conio.h>
int main()
{
char a;

//Requesting user to insert the character


printf("Insert any character: ");

//keeping the inserted character into the variable a


scanf("%c",&a);

if( (ch>='a' && ch<='z') || (ch>='A' && ch<=Z'))


printf("The inserted character %c is an Alphabet”, a);

else
printf("The entered character %c is not an Alphabet”, a);
return 0;
}

Question 19:
Finding Area of a circle
I am going to tell you about finding an area of a circle in C programming.The
area of a circle is number of square units inside the circle. Standard formula to
calculate the area of a circle is: A=πr² .where π =22/7( value is 3.141 )

You can compute the area of a Circle if you know its radius, by simple formula
A = 3.14 * r * r in C programming.We allow user to enter radius, and then find
the area of the cirle.

#include<stdio.h>
int main()
{

//for initialization of radius and area in a float datatype


float radius,area,pi=3.14;

// for use user input

printf("Enter the Radius of a Circle : ");


scanf("%f",&radius);

//formula of area of circle

area = pi*radius*radius;

printf("Area of Circle is: %f",area);


return 0;
}
Question 20:
ASCII values of a character
ASCII value can be any integer number between 0 and 127 and consists of
character variable instead of the character itself in C programming. The value
derived after the programming is termed as ASCII value. With the help of
casting the character to an integer, the ASCII value can be derived. Every
character has an individual ASCII value that can only be an integer. Every time
the character is stored into a variable, as a substitute of keeping the character
itself, the ASCII value of the specific character will get stored.

/* C Program to identify ASCII Value of a Character */


#include<stdio.h>
#include<conio.h>
int main()
{
char a;

printf("\n Kindly insert any character \n");


scanf("%c",&a);

printf("\n The ASCII value of inserted character = %d",a);


return 0;
}

Question 21:
Prime Number
we know that,the whole number are the basic counting number 0,1,2,3….and
so on.So A whole number greater than 1 that can not be made by multiplying
other whole numbers.

Example-7 is a prime number because 7 is only divisible by one or itself.It can


not be divisible by 2,3,4,5,6.

So prime number has two factor is 1 and number itself is called prime
numberThe number 1 is neither prime nor composite.

//C Code to print prime number 1 to 100


#include <stdio.h>

//Main function
int main()
{
int i,j,count=0;

//print values between 1 to 100


printf("The value between 1 to 100\n");

for(i=2;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
count=0;
}
}

Question 21:
Finding the number of digits in an integer
Today, we will be learning how to code a C program for finding the digits in an
integer entered by a user. An integer is made up of a group of digits, i.e; it is a
combination of digits from 0-9
Here we will use loops along with an arithmetic operator.This program takes
an integer from the user and calculates the number of digits. For example: If
the user enters 6589, the output of the program will be 4.

#include <stdio.h>

int main()
{
//to initialize of variable
int count=0;
int n,c;

//to take user input


printf("enter the number: ");
scanf("%d",&n);
c=n;

//when number is zero


if(n==0)
printf("digit is 1");
//false condition

else
{
while(n!=0)
{
//find last digit of number
n=n/10;
//count of a number
++count;
}
printf("the total digit in giving number %d is : %d",c,count);
}
return 0;
}

Question 22:

Converting digit/number to words


The conversion of numbers in words is just a conversion of numeric values to
the English format of reading numbers. This code supports the conversion of
numbers from 0 – 9999 in English format. Digits have different places when
read from its ones place to above. Different places for numbers are:-

Single digits:- Ones


Two Digits:-Tens
Three Digits:- Hundreds
Four Digits:- Thousands

Algorithm

1. Taking input as a string from the user.


2. Check the length of the input.
3. if the length is zero print ’empty’ and if the length is greater than 4 print
‘give a string of specific length’
4. if length id between 1 – 4, Create arrays for different values.
5. Checking the length of the string.
6. According to the place of the digit, we will show the output.

//C++ program
//convert number to text
#include<iostream>
#include<string.h>
using namespace std;
//main Program
void numToWords(string num)
{
int length_of_string = strlen(num);
if (length_of_string == 0){
cout<<“String is Empty”;
return;
}

if (length_of_string > 4){


cout<<“Please enter the string with supported length”;
return;
}
string ones_digits = {“zero”, “one”, “two”, “three”, “four”, “five”, “six”,
“seven”, “eight”, “nine”};
string tens_digits = {“”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”,
“fifteen”, “sixteen”, “seventeen”, “eighteen”,“nineteen”};
string multiple_of_ten = {“”, “”, “twenty”, “thirty”, “forty”, “fifty”, “sixty”,
“seventy”, “eighty”, “ninety”};
string power_of_ten = {“hundred”, “thousand”};
cout<<num<<“:\n“;
if (length_of_string == 1){
cout<<ones_digits[num[0] – ‘0’];
return;
}
int x=0;
while (x < strlen(num)){
if(length_of_string >= 3){
if (num[x] – 48 != 0){
cout<<ones_digits[num[x] – 48]<<“\n“;
cout<<power_of_ten[length_of_string – 3]<<“\n“;
length_of_string–;
}
}
else{
if (num[x] – 48 == 1){
sum = (num[x] – 48 + num[x] – 48);
cout<<tens_digits[sum]);
return;
}
else if(num[x] – 48 == 2 and num[x + 1] – 48 == 0){
cout<<“twenty”;
return;
}
else{
int i = num[x] – 48;
if(i > 0){
print(multiple_of_ten[i], end = ” “);
}
else{
print(“”, end = “”);
}
x += 1;
if(num[x] – 48 != 0){
cout<<ones_digits[num[x] – 48];
}
}
}
x++;
}
}
int main()
{
numToWords(“1121”);
return 0;
}

Question 22:
Number of days in a given month of a given year
Number of days in any month of a year can vary specifically in February as the
cycle of leap year repeats in every 4 years when the year is leap February gives
the count to 29 days but the when the year is not leap it gives count to 28 days
and so no of days in a year varies from 365 to 366.
Rather than February every month gives the count of 30 or 31 days in any case
whether the year is a leap or not.

#include <stdio.h>
int main()
{
int month, year;
printf("enter the month : ");
scanf("%d",&month);
printf("enter the year : ");
scanf("%d",&year);
if(((month==2) && (year%4==0)) || ((year%100==0)&&(year%400==0)))
{
printf("Number of days is 29");
}
else if(month==2)
{
printf("Number of days is 28");
}
else if(month==1 || month==3 || month==5 || month==7 || month==8 ||
month==10 || month==12)
{
printf("Number of days is 31");
}
else
{
printf("Number of days is 30");
}
return 0;
}

Question 23:
Occurrence of a Digit in a given Number
we will count the number of occurrences of a given digit in the given input
number.
The input may lie within the range of integer.

If the digit does not occur in the input it should print 0 else the count of digits.

Sample Input :
Enter a number : 897982
Enter the digit : 9
Output : 2

Explanation : The digit 9 occurs twice

#include <stdio.h>

int main() {
int n;
int d;
int count=0;
scanf(“%d%d“,&n,&d);
while(n)
{
int k = n%10;
n=n/10;
if(k==d)
{
count++;
}
}
printf(“%d“,count);
return 0;
}

Question 24:
Number of integers which has exactly X divisors
In this page we will learn how to find number of integers which has exactly x
divisors. Divisors are numbers which perfectly divides a number. For example
take 6 as a number then divisors of 6 are 1,2,3,6. ‘1’ and number itself are
always divisors of the same number. If a number has exactly 2 divisors then
that number is a prime number.

Here our task is to write a C program to count out how many number of
integers which has exactly x divisors. This can be done by finding number of
divisors that each number has and then checking with the given ‘X‘. If X and
number of divisors match then we will increment the counter value.
Example:

Input

Number=30
X=3
Output

numbers with exactly 3 divisors are 4 9 25


count= 3

#include <stdio.h>

int main()
{
int Number,X,count1;
printf(“\nEnter range of number :”);
scanf(“%d“,&Number);
printf(“\nEnter exact number of divisors :”);
scanf(“%d“,&X);
count1 = 0;
for(int i=1;i<=Number;i++)
{
int count2 = 0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count2++;
}
}
if(count2==X)
{
count1++;
printf(“%d “,i);
}

}
printf(“\n%d“,count1);

return 0;
}
Question 25:
Finding Roots of a Quadratic Equation
In this C program, we will find the roots of a quadratic equation [ax2 + bx + c].
We can solve a Quadratic Equation by finding its roots. Mainly roots of the
quadratic equation are represented by a parabola in 3 different patterns like :

No Real Roots
One Real Root
Two Real Roots
We get the roots of the equation which satisfies any one of the above
conditions :
X = [-b (+or-)[Squareroot(pow(b,2)-4ac)]]/2a

Sample Test Case

Enter value of a :1
Enter value of b :-7
Enter value of c :12
Output
Two Real Roots
4.0
3.0

#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, d, root1, root2, r, i;
printf(“Enter value of a, b and c: “);
scanf(“%lf %lf %lf”, &a, &b, &c);

d = b * b – 4 * a * c;

// condition for real and different roots


if (d > 0) {
printf(“Two Real Roots\n“);
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b – sqrt(d)) / (2 * a);
printf(“root1 = %.2lf \nroot2 = %.2lf”, root1, root2);
}

// condition for real and equal roots


else if (d == 0) {
printf(“Equal Roots\n“);
root1 = root2 = –b / (2 * a);
printf(“root1 = root2 = %.2lf;”, root1);
}

// if roots are not real


else {
r = –b / (2 * a);
i = sqrt(-d) / (2 * a);
printf(“No Real Roots\n“);
printf(“root1 = %.2lf+%.2lfi \nroot2 = %.2f-%.2fi”, r, i, r, i);
}

return 0;
}

You might also like