You are on page 1of 6

Exercise 13 (for loop)

76. Write a program to print the multiplication table of the number entered by the
user. The table should get displayed in the following form. 29 * 1 = 29 29 * 2 = 58 …
Program:
#include<stdio.h>
int main()
{
    int i=1,n;

    printf("Enter number: ");


    scanf("%d", &n);

    while(i!=11)
    {
        printf("%d x %d = %d\n",n,i, n*i);
        i++;
    }
    return 0;
}
Output:
Enter number: 29
29 x 1 = 29
29 x 2 = 58
29 x 3 = 87
29 x 4 = 116
29 x 5 = 145
29 x 6 = 174
29 x 7 = 203
29 x 8 = 232
29 x 9 = 261
29 x 10 = 290
=============================================================
77. C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!
Program:
#include<stdio.h>
int main()
{  
    int n=1, count,func;  
    float sum=0, factorial;  
    printf("ENTER THE NUMBER OF FUNCTIONS\n");
    scanf("%d",&func);
 
    while(n <= func)  
    {  
        factorial = 1;  
        for(count = 1; count <= n; count++)  
        {  
            factorial = factorial*count;  
        }  
 
        sum =sum + (n/factorial);  
        n++;  
    }  
        printf("Sum of series is %f\n", sum);  
 
    return 0;  
}  
Output:
ENTER THE NUMBER OF FUNCTIONS
5
Sum of series is 2.708333
=============================================================
78. Write a program to generate all combinations of 1, 2 and 3 using for loop.
Program:
#include<stdio.h>  
int main()  
{  
int i, j, k;  
 
   for(i = 1; i <= 3; i++)  
   {  
    for(j = 1; j <= 3; j++)  
    {  
     for(k = 1; k <= 3; k++)  
     {  
      printf("%d %d %d\n", i, j, k);  
     }  
    }  
   }  
    return 0;  
}  
Output:
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333
=============================================================
79. Write a program in C to display the n terms of harmonic series and their sum.
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Program:
#include <stdio.h>
int main()
{
   int i,n;
   float s=0.0;
   printf("Input the number of terms : ");
   scanf("%d",&n);
   printf("\n\n");
   for(i=1;i<=n;i++)
   {
       if(i<n)
       {
     printf("1/%d + ",i);
     s+=1/(float)i;
       }
     if(i==n)
     {
     printf("1/%d ",i);
     s+=1/(float)i;
     }
     }
        printf("\nSum of Series upto %d terms : %f \n",n,s);
        return 0;
}  
Output:
1/1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of Series upto 5 terms : 2.283334
=============================================================
80. When interest compounds q times per year at an annual rate of r % for n years, the
principle p compounds to an amount a as per the following formula a = p ( 1 + r / q )
nq Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    float p,n,r,q,a;
    int i;
    for(i=1;i<=10;i++)
    {
        printf("Set: %d\n",i);
        printf("Enter Principle: ");
        scanf("%f",&p);
        printf("Enter Rate: ");
        scanf("%f",&r);
        printf("Enter Time(in year): ");
        scanf("%f",&n);
        printf("Enter Compound Interest: ");
        scanf("%f",&q);

        a = p*(pow((1+r/q),n*q));
        printf("Amount: %.2f\n\n",a);
    }
    return 0;
}
Output:
Set: 1
Enter Principle: 100
Enter Rate: 5
Enter Time(in year): 5
Enter Compound Interest: 25
Amount: 790052143104.00
\\ there are similar total 10 sets
=============================================================
81. Write a program to print all prime numbers from 1 to 300. (Hint: Use nested
loops, break and continue)
Program:
#include<stdio.h>
int main()
{
    int num, max=300, i, flag;

    for(num=1;num<=max;num++)
    {
        flag=0;
        for(i=2;i<=num/2;i++)
       {
           if(num%i==0)
        {
           flag=1;
           break;
       }
     }
        if(flag==0 & num!=1)
            printf("%d\t", num);
    }
    return 0;
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41
43 47 53 59 61 67 71 73 79 83 89 97 101
103 107 109 113 127 131 137 139 149 151 157 163
167 173 179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281 283 293
=============================================================

You might also like