C
Problem 6.1: Given a number, write a program using while loop to reverse the digits of the number.For example ,the number
12345
should be written as
54321.
Solution: #include <stdio.h>
int main()
{
int n;
printf(“enter your number to revice\n”);
scanf(“%d”,&n);
printf(“reverse of your number %d is : “,n);
while(n!=0)
{
printf(“%d”,n%10);
n=n/10;
}
printf(“\n”);
}
Problem 6.2: The factorial of an integer m is the product of consecutive integers from 1 to m. That is, factorial m=m!=m x (m-1) x …….. x 1.
Write a program that computes and prints a lable of factorials for any given m.
Solution: #include <stdio.h>
int main()
{
int m,i,f=1;
printf(“enter the number=\n”);
scanf(“%d”,&m);
for(i=m; i>=1; i–)
{
f=f*i;
}
printf(“Factorial=%d\n”,f);
}
Problem 6.3: Write a program to compute the sum of the digits of a given integer number.
Solution: #include<stdio.h>
void main()
{
int n,digit,sum=0,temp;
printf(“enter your number \n”);
scanf(“%d”,&n);
temp=n;
while(n!=0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
printf(“sum of all digit %d is: %d\n”,temp,sum);
}
Problem 6.4: The numbers in the sequence
1 1 2 3 5 8 13 21 ………………
Solution: #include <stdio.h>
int main()
{
int i, n, a = 0, b = 1, c,sum=0;
printf(“Enter the number: “);
scanf(“%d”, &n);
printf(“Fibonacci Series is till %d: “,n);
for (i = 1; i <= n; i++)
{
sum=sum+a;
printf(“%d “,a );
c = a + b;
a = b;
b = c;
}
printf(“\nsum of this series is :%d\n”,sum);
return 0;
}
Problem 6.5: Rewrite a program of the example 6.1 using the for statement.
Solution: #include <stdio.h>
int main()
{
int i,n;
printf(“enter your number to reverce\n”);
scanf(“%d”,&n);
printf(“reverce of %d is :”,n);
for(i=0;n!=0;i++)
{
printf(“%d”,n%10);
n=n/10;
}
return 0;
}
Problem 6.7: write a program to print the following outputs using for loop
(a)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Solution: #include <stdio.h>
int main()
{
int i,j,n;
printf(“enter your num=\n”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
printf(“%d “,i);
printf(“\n”);
}
return 0;
}
(b)
******
*****
****
***
**
*
Solution:
#include<stdio.h>
main()
{
int i,j,limit;
for(i=1;i<=6;i++)
{
for(j=1;j<=i;j++)
{
printf(” “);
}
for(j=6;j>=i;j–)
{
printf(“*”);
}
printf(“\n”);
}
}
Problem 6.8: Write a program to the age of 100 persons and count the number of persons in the age group 50 to 60. Use for and continue statement.
Solution: #include<stdio.h>
void main()
{
int age,cnt1=0,cnt2=0,i;
for(i=1;i<=5;i++)
{
printf(“Enter the age : “);
scanf(“%d”,&age);
if(age>60||age<50)
{
cnt2++;
continue;
}
cnt1++;
}
printf(“Total persons are %d\nAnd continued %d times”,cnt1,cnt2);
}
Problem 6.11: Write a program that will read a positive integer determine and print its binary equivalent .
Solution: #include<stdio.h>
main()
{
int num,bin,base=1;
double fbin;
printf(“Enter the number : \n”);
scanf(“%d”,&num);
while(num>0)
{
bin=num%2;
fbin=fbin+(bin*base);
base=base*10;
num=num/2;
}
printf(“Binary form is %f”,fbin);
}
Problem 6.12: write a program using for and if statement to display the capital letter S in a grid of 15 rows and 18 columns as shown below:
| *************** *************** *************** **** **** **** *************** *************** *************** **** **** **** *************** *************** *************** | Solution: #include<stdio.h> main() { int row,col,i,j,k; row=15; col=18; for(i=1;i<=row;i++){ if((i<=3)||(i>=7&&i<=9)||(i>=13&&i<=15)){ for(j=1;j<col;j++) printf(“*”); printf(“\n”); } else if(i>=4&&i<=6){ for(j=1;j<=4;j++) printf(“*”); printf(“\n”); } else { for(j=1;j<=13;j++) printf(” “); for(j=1;j<=4;j++) printf(“*”); printf(“\n”); } } } |
Problem 6.13: Write a program to compute the value of Euler’s number e, that is use as the base of natural logarithms. Use the following formula: e= 1+1/2!+ 1/3! + 1/4! + ————- + 1/n!
Use a suitable loop construct. The loop must terminate when the difference between two successive values of e is less than 0.00001.
Solution: #include<stdio.h>
float fact(float i){
float f=1;
int k;
for(k=1;k<=i;k++)
f*=k;
return(f);
}
void main()
{
float e,a,b,d=1,temp;
int i,j,k;
e=1;i=2;b=1;
while(d>=0.00001)
{
temp=fact(i);
a=1/temp;
e=e+a;
d=b-a;
if(d>=0.00001)
b=a;
i++;
}
printf(“\n\nthe result = %f”,e);
}
Problem 6.16: Write a program to print a square of size 5 by using the character S as shown below:
| (a) S S S S S S S S S S S S S S S S Solution: #include <stdio.h> int main() { int i,j; for(i=1; i<=4; i++){ for (j=1;j<=4; j++) printf(“S “); printf(“\n”); } return 0; } | (b) S S S S S S S S S S S S Solution: #include <stdio.h> int main() { int i,j; for(i=1; i<=5; i++){ for (j=1;j<=5; j++) if((i==1||i==5)||(j==1||j==5)) printf(“* “); else printf(” “); printf(“\n”); } return 0; } |
Problem 6.17: Write a program to graph the function
Y=sin(x)
In the interval 0 t0 180 degrees in steps of 15 degrees. Use the concepts discussed in the case study 4 in chapter 6.
Solution: #include<stdio.h>
#include<math.h>
void main()
{
float y;
int x,i;
printf(“X Sin(X)\n”);
for(i=0;i<=180;i+=15)
{
x=i;
y=sin(x);
printf(“%d %0.3f\n”,x,y);
}
}
Problem 6.18: write a program to print all integers that are not divisible by either 2 or 3 and lie between 1 and 100. Program should also account the number of such integers and print the result.
Solution: #include<stdio.h>
void main()
{
int i,count,sum;
sum=0;
count=0;
for(i=1;i<=100;i++)
{
if(i%2!=0&&i%3!=0)
{
sum+=i;
count++;
printf(“%d\n”,i);
}
}
printf(“the sum of the value is :%d \nthe countable numbers is: %d\n”,sum,count);
}
Problem 6.19: Modify the program of exercise 6.16 to print the character O instate of S at the center of the square as shown below.
Solution: #include<stdio.h>
main()
{
int i,j;
for(i=1; i<=5; i++)
{
for(j=1; j<=5; j++)
if(i==3&&j==3)
printf(“O “);
else
printf(“S “);
printf(“\n”);
}
}
Problem 6.20: Given a set of 10 two digit integers containing both positive and negative values, write a program using for loop to compute the sum of all values and print the sum and the number of values added. The program should use scanf to the values and terminate when the sum 999. Do not use goto statements.
Solution: #include<stdio.h>
void main()
{
int sum=0,i,n,count=0;
printf(“Input ten number both positive and negative:”);
for(i=0;i<10;i++)
{
scanf(“%d”,&n);
{
sum+=n; count++;}
if(sum>999)
break;
}
printf(“\n\nThe value of positive numbers is:%d\nand the countable number is: %d”,sum,count);
}