0% found this document useful (0 votes)
16 views12 pages

C Program

The document contains various C programming code snippets demonstrating different methods to calculate sums and perform comparisons. It includes examples using for loops, while loops, do-while loops, and goto statements for summing squares, powers of two, geometric series, and arithmetic series. Additionally, it shows how to find the greatest of three numbers and calculate the area of a triangle given its sides.

Uploaded by

Mohammad Jahid
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)
16 views12 pages

C Program

The document contains various C programming code snippets demonstrating different methods to calculate sums and perform comparisons. It includes examples using for loops, while loops, do-while loops, and goto statements for summing squares, powers of two, geometric series, and arithmetic series. Additionally, it shows how to find the greatest of three numbers and calculate the area of a triangle given its sides.

Uploaded by

Mohammad Jahid
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

1. 2² + 3² + 4² + 5² + ...

+ n²

For Loop

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;

for(i = 2; i <= n; i++)


{
s = s + i * i;
}

printf("%d", s);

return 0;
}

While loop

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 2;

while(i <= n)
{
s = s + i * i;
i = i + 1;
}

printf("%d", s);
return 0;
}

Do…while
#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 2;

do
{
s = s + i * i;
i = i + 1;
}
while(i <= n);

printf("%d", s);

return 0;
}

If…goto

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 2;

level:
if(i <= n)
{
s = s + i * i;
i = i + 1;
goto level;
}

printf("%d", s);

return 0;
}

Another way:

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 2;

repeat:
if(i <= n)
{
s = s + i * i;
i = i + 1;
goto repeat;
}

printf("%d", s);

return 0;
}

2. 2¹ + 2² + 2³ + ... + 2ⁿ

For loop

#include <stdio.h>

main()
{
int n, s, i, power;
scanf("%d", &n);

s = 0;
power = 2;

for(i = 1; i <= n; i++)


{
s = s + power;
power = power * 2;
}

printf("%d", s);

return 0;
}

While

#include <stdio.h>

main()
{
int n, s, i, power;
scanf("%d", &n);

s = 0;
i = 1;
power = 2;

while(i <= n)
{
s = s + power;
power = power * 2;
i = i + 1;
}

printf("%d", s);

return 0;
}

Do…while
#include <stdio.h>

main()
{
int n, s, i, power;
scanf("%d", &n);

s = 0;
i = 1;
power = 2;

do
{
s = s + power;
power = power * 2;
i = i + 1;
}
while(i <= n);

printf("%d", s);

return 0;
}

If….goto

#include <stdio.h>

main()
{
int n, s, i, power;
scanf("%d", &n);

s = 0;
i = 1;
power = 2;

level:
s = s + power;
power = power * 2;
i = i + 1;
if(i <= n)
goto level;

printf("%d", s);

return 0;
}

3. 2+6+18.....N

For loop

#include <stdio.h>
main()
{
int n, s, i;
scanf("%d", &n);

s = 0;

for(i = 2; i <= n; i = i * 3)
{
s = s + i;
}

printf("%d", s);

return 0;
}

While

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);
s = 0;
i = 2;

while(i <= n)
{
s = s + i;
i = i * 3;
}

printf("%d", s);

return 0;
}

Do…while
#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 2;

do
{
s = s + i;
i = i * 3;
}
while(i <= n);

printf("%d", s);

return 0;
}

If….goto

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 2;

level:
if(i > n)
goto end;

s = s + i;
i = i * 3;

goto level;

end:
printf("%d", s);

return 0;
}

4. 150+147+144+...n

For Loop

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;

for(i = 150; i >= n; i = i - 3)


{
s = s + i;
}

printf("%d", s);
return 0;
}

While

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 150;

while(i >= n)
{
s = s + i;
i = i - 3;
}

printf("%d", s);

return 0;
}

Do…while

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 150;

do
{
s = s + i;
i = i - 3;
}
while(i >= n);

printf("%d", s);

return 0;
}

If…goto

#include <stdio.h>

main()
{
int n, s, i;
scanf("%d", &n);

s = 0;
i = 150;

level:
if(i < n)
goto end;

s = s + i;
i = i - 3;

goto level;

end:
printf("%d", s);

return 0;
}

[Link] of three numbers

With if

#include <stdio.h>
main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);

if(a >= b && a >= c)


printf("%d", a);

if(b >= a && b >= c)


printf("%d", b);

if(c >= a && c >= b)


printf("%d", c);

return 0;
}

With if ..else if….esle

#include <stdio.h>

main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);

if(a >= b && a >= c)


printf("%d", a);
else if(b >= a && b >= c)
printf("%d", b);
else
printf("%d", c);

return 0;
}

6. Area of a triangle whose three side a,b,c are given.

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

main()
{
float a, b, c, s, area;
scanf("%f %f %f", &a, &b, &c);

s = (a + b + c) / 2;

area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("%f", area);

return 0;
}

You might also like