C Program To Calculate Sum of First 7 Terms of Natural Logarithm

The Natural Logarithm can be approximated by the following series:

(x – 1 / x) + 1/2 (x – 1 / x)2 + 1/2 (x – 1 / x)3 + 1/2 (x – 1 / x)4 + ..

If x is input through the keyboard, write a C program to calculate the sum of first seven terms of this series.

Related Read:
For Loop In C Programming Language
Basic Arithmetic Operations In C

Video Tutorial: C Program To Calculate Sum of First 7 Terms of Natural Logarithm


[youtube https://www.youtube.com/watch?v=8mVIyfub9o4]

YouTube Link: https://www.youtube.com/watch?v=8mVIyfub9o4 [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Sum of First 7 Terms of Natural Logarithm

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

int main()
{
    int count;
    float x, result = 0.0;

    printf("Enter value of x\n");
    scanf("%f", &x);

    for(count = 1; count <= 7; count++)
    {
        if(count == 1)
        {
            result = (x - 1) / x;
        }
        else
        {
            result = result + pow( (x - 1) / x, count) * 0.5;
        }
    }

    printf("Result of first 7 terms = %0.2f\n", result);

    return 0;
}

Output 1
Enter value of x
5
Result of first 7 terms = 1.98

Output 2
Enter value of x
14
Result of first 7 terms = 3.10

Logic To Calculate Sum of First 7 Terms of Natural Logarithm

According to the problem statement it is clear that we need sum of first 7 terms of Natural Logarithm. So we initialize the loop counter variable count to 1 and iterate through the for loop until count value is less than or equal to 7.

Inside for loop we calculate the value of first 7 terms in the series and store it inside variable result.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Numbers, using For Loop

Lets write a C program to draw / display / print a four row pyramid formed from numbers 1 to 10.

Related Read:
Nested For Loop In C Programming Language
C Program To Draw Pyramid of Numbers, using While Loop

Video Tutorial: C Program To Draw Pyramid of Numbers, using For Loop


[youtube https://www.youtube.com/watch?v=Z-uVxVnXqgE]

YouTube Link: https://www.youtube.com/watch?v=Z-uVxVnXqgE [Watch the Video In Full Screen.]

Source Code: C Program To Draw Pyramid of Numbers, using For Loop

#include<stdio.h>
int main()
{
    int num = 4, row, col, space, count = 1;
    float i = -7;


    for(row = 1; row <= num; row++)
    {
        for(space = i; space <= (num-row); space++)
        {
            printf(" ");
        }
        for(col = 1; col <= row; col++)
        {
            printf(" %d ", count++);
        }
        i += 0.8;

        printf("\n");
    }

    return 0;
}

Output

                    1
                  2  3
                 4  5  6
               7  8  9  10

Logic To Draw Pyramid of Numbers, using For Loop

Here we already know that the Pyramid we need to print has 4 rows and is formed of numbers 1 to 10.

Outer for loop selects the row number and the number of elements to be printed for that particular selected row. For example, 1st row has 1 element. 2nd row has 2 elements. 3rd row has 3 elements and so on. So the row number and the number of elements in that particular row are always the same.

First inner for loop prints the adequate spacing required for the pyramid. Second inner for loop prints the actual numbers.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Stars, using For Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from stars, using nested for loop.

Related Read:
Nested For Loop In C Programming Language
C Program To Draw Pyramid of Stars, using While Loop

Expected Output for the Input

User Input:
Enter no of rows of Pyramid
5

Output:

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

Pyramid With 20 Rows
pyramid of stars

Video Tutorial: C Program To Draw Pyramid of Stars, using For Loop


[youtube https://www.youtube.com/watch?v=omDJLEDBZNw]

YouTube Link: https://www.youtube.com/watch?v=omDJLEDBZNw [Watch the Video In Full Screen.]

Logic To Draw Pyramid of Stars, using For Loop

If num = 5;

First Iteration of outer for loop
row = 1;

1st inner for loop
condition:
num – row;
= 5 – 1
= 4
Four spaces gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 1 – 1;
= 2 – 1
= 1
One star gets printed.

Second Iteration of outer for loop
row = 2;

1st inner for loop
condition:
num – row;
= 5 – 2
= 3
Three spaces gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 2 – 1;
= 4 – 1
= 3
Three stars gets printed.

Third Iteration of outer for loop
row = 3;

1st inner for loop
condition:
num – row;
= 5 – 3
= 2
Two spaces gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 3 – 1;
= 6 – 1
= 5
Five stars gets printed.

Forth Iteration of outer for loop
row = 4;

1st inner for loop
condition:
num – row;
= 5 – 4
= 1
One space gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 4 – 1;
= 8 – 1
= 7
Seven stars gets printed.

Fifth Iteration of outer for loop
row = 5;

1st inner for loop
condition:
num – row;
= 5 – 5
= 0
Zero space gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 5 – 1;
= 10 – 1
= 9
Nine stars gets printed.

Source Code: C Program To Draw Pyramid of Stars, using For Loop

#include<stdio.h>

int main()
{
    int num, row, col, space;

    printf("Enter no of rows of Pyramid\n");
    scanf("%d", &num);

    for(row = 1; row <= num; row++)
    {
        for(space = 1; space <= (num-row); space++)
        {
            printf(" ");
        }
        for(col = 1; col <= (2*row-1); col++)
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter no of rows of Pyramid
5

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

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print 1+4+9+16 Series, using For Loop

Lets write C program to print/display number series 1 + 4 + 9 + 16 + 25 + using for loop.

Related Read:
For Loop In C Programming Language
C Program To Print 1+4+9+16 Series, using While Loop

Video Tutorial: C Program To Print 1+4+9+16+25 Series, using for Loop


[youtube https://www.youtube.com/watch?v=rUrSZ1PMnOU]

YouTube Link: https://www.youtube.com/watch?v=rUrSZ1PMnOU [Watch the Video In Full Screen.]

Logic To Print 1+4+9+16 number series using for loop

If we analyze the number series, its just addition of square of natural numbers. i.e., (1 x 1) + (2 x 2) + (3 x 3) + (4 x 4) + (5 x 5) + .. etc

We ask the user to enter a number. If user enters num = 5, then we display the first 5 numbers in the series i.e., 1 + 4 + 9 + 16 + 25 +

If user enters num = 10, then we display the first 10 numbers in the series i.e., 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 +

For Loop Logic

We initialize count to 1, as the number series starts from 1. We iterate for loop until count is less than or equal to user entered limit. For each iteration of the for loop we increment the value of count by 1.

Inside for loop we square the value of count and print the result, for each iteration of for loop.

Source Code: C Program To Print 1+4+9+16 Series, using for Loop

#include<stdio.h>

int main()
{
    int limit, count;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    for(count = 1; count <= limit; count++)
    {
        printf("%d + ", (count*count));
    }
    printf("\n");

    return 0;
}

Output 1:
Enter the limit
5
1 + 4 + 9 + 16 + 25 +

Output 2:
Enter the limit
10
1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 +

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print All ASCII Characters and Value using For Loop

Lets write a C program to print/display all ASCII characters and its corresponding value using For loop.

Note: In C programming language, every alphabet, number and symbol has corresponding ASCII value(a integer number representing the character).

In C programming language, there are 256 ASCII Characters and ASCII Values. i.e., from 0 to 255.

Related Read:
For Loop In C Programming Language
C Program To Print All ASCII Characters and Value

Video Tutorial: C Program To Print All ASCII Characters and Value using For Loop


[youtube https://www.youtube.com/watch?v=xTRbser_9us]

YouTube Link: https://www.youtube.com/watch?v=xTRbser_9us [Watch the Video In Full Screen.]

Source Code: C Program To Print All ASCII Characters and Value using For Loop

#include<stdio.h>

int main()
{
    int count;

    for(count = 0; count <= 255; count++)
    {
        printf("ASCII value of %c is %d\n", count, count);
    }

    return 0;
}

Output
ASCII value of is 0

ASCII value of is 1

ASCII value of is 2

ASCII value of is 3

ASCII value of is 4

ASCII value of is 5

ASCII value of is 6

ASCII value of is 7

ASCII value of is 8

ASCII value of is 9

ASCII value of
is 10

ASCII value of is 11

ASCII value of is 12

is 13

ASCII value of is 14

ASCII value of is 15

ASCII value of is 16

ASCII value of is 17

ASCII value of is 18

ASCII value of is 19

ASCII value of is 20

ASCII value of § is 21

ASCII value of is 22

ASCII value of is 23

ASCII value of is 24

ASCII value of is 25

ASCII value of is 26

ASCII value of is 27

ASCII value of is 28

ASCII value of is 29

ASCII value of is 30

ASCII value of is 31

ASCII value of is 32

ASCII value of ! is 33

ASCII value of is 34

ASCII value of # is 35

ASCII value of $ is 36

ASCII value of % is 37

ASCII value of & is 38

ASCII value of is 39

ASCII value of ( is 40

ASCII value of ) is 41

ASCII value of * is 42

ASCII value of + is 43

ASCII value of , is 44

ASCII value of is 45

ASCII value of . is 46

ASCII value of / is 47

ASCII value of 0 is 48

ASCII value of 1 is 49

ASCII value of 2 is 50

ASCII value of 3 is 51

ASCII value of 4 is 52

ASCII value of 5 is 53

ASCII value of 6 is 54

ASCII value of 7 is 55

ASCII value of 8 is 56

ASCII value of 9 is 57

ASCII value of : is 58

ASCII value of ; is 59

ASCII value of < is 60

ASCII value of = is 61

ASCII value of > is 62

ASCII value of ? is 63

ASCII value of @ is 64

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

ASCII value of [ is 91

ASCII value of \ is 92

ASCII value of ] is 93

ASCII value of ^ is 94

ASCII value of _ is 95

ASCII value of ` is 96

ASCII value of a is 97

ASCII value of b is 98

ASCII value of c is 99

ASCII value of d is 100

ASCII value of e is 101

ASCII value of f is 102

ASCII value of g is 103

ASCII value of h is 104

ASCII value of i is 105

ASCII value of j is 106

ASCII value of k is 107

ASCII value of l is 108

ASCII value of m is 109

ASCII value of n is 110

ASCII value of o is 111

ASCII value of p is 112

ASCII value of q is 113

ASCII value of r is 114

ASCII value of s is 115

ASCII value of t is 116

ASCII value of u is 117

ASCII value of v is 118

ASCII value of w is 119

ASCII value of x is 120

ASCII value of y is 121

ASCII value of z is 122

ASCII value of { is 123

ASCII value of | is 124

ASCII value of } is 125

ASCII value of ~ is 126

ASCII value of  is 127

ASCII value of Ç is 128

ASCII value of ü is 129

ASCII value of é is 130

ASCII value of â is 131

ASCII value of ä is 132

ASCII value of à is 133

ASCII value of å is 134

ASCII value of ç is 135

ASCII value of ê is 136

ASCII value of ë is 137

ASCII value of è is 138

ASCII value of ï is 139

ASCII value of î is 140

ASCII value of ì is 141

ASCII value of Ä is 142

ASCII value of Å is 143

ASCII value of É is 144

ASCII value of æ is 145

ASCII value of Æ is 146

ASCII value of ô is 147

ASCII value of ö is 148

ASCII value of ò is 149

ASCII value of û is 150

ASCII value of ù is 151

ASCII value of ÿ is 152

ASCII value of Ö is 153

ASCII value of Ü is 154

ASCII value of ¢ is 155

ASCII value of £ is 156

ASCII value of ¥ is 157

ASCII value of is 158

ASCII value of ƒ is 159

ASCII value of á is 160

ASCII value of í is 161

ASCII value of ó is 162

ASCII value of ú is 163

ASCII value of ñ is 164

ASCII value of Ñ is 165

ASCII value of ª is 166

ASCII value of º is 167

ASCII value of ¿ is 168

ASCII value of is 169

ASCII value of ¬ is 170

ASCII value of ½ is 171

ASCII value of ¼ is 172

ASCII value of ¡ is 173

ASCII value of « is 174

ASCII value of » is 175

ASCII value of is 176

ASCII value of is 177

ASCII value of is 178

ASCII value of is 179

ASCII value of is 180

ASCII value of is 181

ASCII value of is 182

ASCII value of is 183

ASCII value of is 184

ASCII value of is 185

ASCII value of is 186

ASCII value of is 187

ASCII value of is 188

ASCII value of is 189

ASCII value of is 190

ASCII value of is 191

ASCII value of is 192

ASCII value of is 193

ASCII value of is 194

ASCII value of is 195

ASCII value of is 196

ASCII value of is 197

ASCII value of is 198

ASCII value of is 199

ASCII value of is 200

ASCII value of is 201

ASCII value of is 202

ASCII value of is 203

ASCII value of is 204

ASCII value of is 205

ASCII value of is 206

ASCII value of is 207

ASCII value of is 208

ASCII value of is 209

ASCII value of is 210

ASCII value of is 211

ASCII value of is 212

ASCII value of is 213

ASCII value of is 214

ASCII value of is 215

ASCII value of is 216

ASCII value of is 217

ASCII value of is 218

ASCII value of is 219

ASCII value of is 220

ASCII value of is 221

ASCII value of is 222

ASCII value of is 223

ASCII value of α is 224

ASCII value of ß is 225

ASCII value of Γ is 226

ASCII value of π is 227

ASCII value of Σ is 228

ASCII value of σ is 229

ASCII value of µ is 230

ASCII value of τ is 231

ASCII value of Φ is 232

ASCII value of Θ is 233

ASCII value of Ω is 234

ASCII value of δ is 235

ASCII value of is 236

ASCII value of φ is 237

ASCII value of ε is 238

ASCII value of is 239

ASCII value of is 240

ASCII value of ± is 241

ASCII value of is 242

ASCII value of is 243

ASCII value of is 244

ASCII value of is 245

ASCII value of ÷ is 246

ASCII value of is 247

ASCII value of ° is 248

ASCII value of is 249

ASCII value of · is 250

ASCII value of is 251

ASCII value of is 252

ASCII value of ² is 253

ASCII value of is 254

ASCII value of   is 255

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert