0% found this document useful (0 votes)
67 views4 pages

Program To Count The Number of Digits

The document contains C program code snippets for the following tasks: 1) A program to count the number of digits in a given integer by repeatedly dividing the number by 10 and incrementing a counter. 2) A program to find the sum of the first and last digits of a given number by extracting the last digit, copying the number, and repeatedly dividing to extract the first digit. 3) A program to calculate an employee's gross salary based on their basic salary and different HRA and DA rates depending on salary range. 4) A program to print an inverted pyramid pattern of stars of a given number of rows. 5) A program to calculate the sum of a harmonic series from 1 to a

Uploaded by

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

Program To Count The Number of Digits

The document contains C program code snippets for the following tasks: 1) A program to count the number of digits in a given integer by repeatedly dividing the number by 10 and incrementing a counter. 2) A program to find the sum of the first and last digits of a given number by extracting the last digit, copying the number, and repeatedly dividing to extract the first digit. 3) A program to calculate an employee's gross salary based on their basic salary and different HRA and DA rates depending on salary range. 4) A program to print an inverted pyramid pattern of stars of a given number of rows. 5) A program to calculate the sum of a harmonic series from 1 to a

Uploaded by

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

Q.

Program to Count the Number of Digits


#include <stdio.h>
#include<conio.h>
void main()
{
int n;
int count = 0;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);

// iterate at least once, then until n becomes 0


// remove last digit from n in each iteration
// increase count by 1 in each iteration
while(n!=0)
{
n /= 10;
++count;
}

printf("Number of digits: %d", count);


getch();
}

Q. C program to find the sum of first and last digit of a given num

#include <stdio.h>

int main()
{
int num, sum=0, firstDigit, lastDigit;

/* Input a number from user */


printf("Enter any number to find sum of first and last digit: ");
scanf("%d", &num);

/* Find last digit to sum */


lastDigit = num % 10;

/* Copy num to first digit */


firstDigit = num;

/* Find the first digit by dividing num by 10 until first digit is left */
while(num >= 10)
{
num = num / 10;
}
firstDigit = num;
/* Find sum of first and last digit*/
sum = firstDigit + lastDigit;

printf("Sum of first and last digit = %d", sum);

return 0;
}

Q.C program to enter basic salary and calculate gross salary of an


employee.
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
#include <stdio.h>
#include<conio.h>
void main()
{
float basic, gross, da, hra;
clrscr();
/* Input basic salary of employee */
printf("Enter basic salary of an employee: ");
scanf("%f", &basic);

/* Calculate D.A and H.R.A according to specified conditions */


if(basic <= 10000)
{
da = basic * 0.8;
hra = basic * 0.2;
}
else if(basic <= 20000)
{
da = basic * 0.9;
hra = basic * 0.25;
}
else
{
da = basic * 0.95;
hra = basic * 0.3;
}

/* Calculate gross salary */


gross = basic + hra + da;

printf("GROSS SALARY OF EMPLOYEE = %.2f", gross);

getch();
}
Q.. Inverted full pyramid of *

* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

#include <stdio.h>
#include<conio.h>
void main() {
int rows, i, j, space;
clrscr();
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (space = 0; space < rows - i; ++space)
printf(" ");
for (j = i; j <= 2 * i - 1; ++j)
printf("* ");
for (j = 0; j < i - 1; ++j)
printf("* ");
printf("\n");
}
getch();
}

Q. Sum of series 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/n

#include<stdio.h>
int main()
{
int n,i;
float sum=0.0;

printf("Enter the limit (n Value): ");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
sum+=(1.0/i); //sum = sum + 1.0/i
}

printf("Sum = %f\n", sum);

return 0;
}

You might also like