Lab 6 Report
Lab 6 Report
Subject: C Programming
Lab Report 6
Tittle: Nested Looping Structure
Checked by
Content of Lab Report:
Background Information
C Programming
Editor Used
Compiler
Nested Loop Structure
Analysis
Conclusion
Background Information
What is C Programming?
C programming is a general-purpose, procedural, imperative computer programming
language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to
develop the UNIX operating system. C is the most widely used computer language.
Why to Learn C Programming?
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
Editor
Here, I have used Visual Studio Code as my editor. You can download the editor from
Download Visual Studio Code - Mac, Linux, Windows . Select your operating system
and download it.
Compiler
Here, I have used gcc as my compiler provided by MinGWw64. You can download it via
Download MinGW-w64 - for 32 and 64 bit Windows from SourceForge.net. Your
download will start automatically. Run the downloaded .exe file. After, you have
installed MinGW-w64, you need to configure it.
In the Windows search bar, type 'settings' to open your Windows Settings.
Search for Edit environment variables for your account.
Choose the Path variable and then select Edit.
Select New and add the Mingw-w64 destination folder path to the system
path. The exact path depends on which version of Mingw-w64 you have installed
and where you installed it. If you used the settings above to install Mingw-
w64, then add this to the path: C:\Program Files\mingw-w64\x86_64-8.1.0-
posix-seh-rt_v6-rev0\mingw64\bin.
Select OK to save the updated PATH. You will need to reopen any console
windows for the new PATH location to be available.
Check your installation
Open command prompt or power shell and type:
int main ()
{
int m, n;
system("cls");
printf("Multiplication Table of: ");
scanf("%d", &m);
printf("Upto : ");
scanf("%d", &n);
printf("Multiplication Table of %d from 1 to %d is: \n", m, n);
for (int i = 1; i <= n; i++)
{
printf("%d * %d = %d\n", m, i, m * i);
}
getch();
return 0;
}
Output:
5.1
Question:
1,2,3,4,5, 6...................n
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("\nNumber for 1 to n is listed below: \n\n", n);
for (int i = 1; i <= n; i++)
{
printf("%d ", i);
}
getch();
return 0;
}
Output:
5.2
Questions:
2,4,6,8,10 ..................,2n
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("\nNumber for 2 to 2n is listed below: \n\n", n);
for (int i = 1; i <= n; i++)
{
printf("%d ", 2 * i);
}
getch();
return 0;
}
Output:
5.3
Question:
1,2,5,10,17,26.......
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n, term = 1;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("Sequence is \n\n");
for (int i = 0; i < n; i++)
{
printf("%d ", term);
term = term + (2 * i + 1);
}
getch();
return 0;
}
Output:
5.4
(1^2 + 2^2)/2, (2^2 + 3^2)/3, ........
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include<math.h>
int main ()
{
int n;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("\nSequence is:\n");
for (int i = 1; i <= n; i++)
{
printf("(%d^2 + %d^2)/%d ", i, i + 1, i + 1);
}
5.5
1,1/3,1/5............,1/(2n-1)
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("\nSequence is: \n\n", n);
for (int i = 1; i <= n; i++)
{
printf("1/%d ", 2 * i -1);
}
getch();
return 0;
}
Output:
6.1
Question:
2+4+6+8+10+....................+ 2n
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n;
system("cls");/*CLear the screen*/
printf("Value of n: ");
scanf("%d", &n);
printf("\nSequence is: \n\n");
for (int i = 1; i <= n; i++)
{
printf("%d ", 2 * i);
if (n == i)
{
break;
}
else
{
printf("+ ");
}
}
getch();
return 0;
}
Output:
6.2
Question:
1-1/1! +1/2! -1/3!......................(-1) ^n/ (n-1)! n =0,1,2,3......
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
int n;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("\n\nSequence is:\n");
for (int i = 1; i <= n; i++)
{
if (i == 1)
{
printf("1 ");
}
else
{
printf("1/(%d)! ", (i - 1));
}
if (n == i + 1)
{
break;
}
else
{
if (i % 2! = 0)
{
printf(" + ");
}
else
{
printf(" - ");
}
}
}
printf("\n\nRespective Value:\n"); /* Information*/
for (int i = 0; i < n; i++)
{
printf("%f ", ((float)1 / tgamma(i+1))); /*Print the value
if (n == i+1)
{
break;
}
else
{
if (i%2! = 0)
{
printf(" + ");
}
else
{
printf(" - ");
}
}
}
getch();
return 0;
}
Output:
6.3
1-x^2/2+x^4/4!.............(-1)^i*x^(2i)/(2i)! i =0,1,2,3....
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
int x, n;
system("cls");
printf("Value of x: ");
scanf("%d", &x);
printf("Value of n: ");
scanf("%d", &n);
printf("\nSequence is:\n\n");
for (int j = 0; j < n; j++)
{
if (j == 0)
{
printf("1 ");
}
else
{
printf("x^%d/(%d)! ", 2 * j, 2 * j);
}
if (n == j + 1)
{
break;
}
else {
if (pow (-1, j) > 0)
{
printf(" - ");
}
else
{
printf(" + ");
}
}
}
printf("\n\nRespective Value:\n");
7.
Questions:
1+ x/1! +x^2/2! + x^3/3!.......... till sum of terms is less than 10^-6
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include<math.h>
int main ()
{
int power =0;
double x, term = 1, sum_of_term = 0, condition;
system("cls");
printf("Value of x: ");
scanf("%lf", &x);
printf("\n\nSequence with value of x till term>10^-6: \n");
while (condition >(double)0) {
term = pow (x, power) / tgamma(power + 1);
printf("%.10f +", term);
sum_of_term = term + sum_of_term;
condition=term - 0.000001;
power++;
}
printf("\n\nSum of term: %.5f\n", sum_of_term);
printf("\nAt %d term,term is less than 10^-6\n", power);
getch();
return 0;
}
Output:
8.1
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("Sequence is:\n\n");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
getch();
return 0;
}
Output:
8.2
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("Sequence is:\n\n");
for (int i = 0; i < n; i++)
{
for (int j = n; j>i; j--)
{
printf("%d ", j);
}
printf("\n");
}
getch();
return 0;
}
Output:
8.3
N
E E
P P P
A A A A
L L L L L
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
system("cls");
printf("Sequence is:\n");
for (int i = 1; i <= 5; i++)
{
for (int j = 0; j <i; j++)
{
if (i == 1)
{
printf("N ");
}
else if (i == 2)
{
printf("E ");
}
else if (i == 3)
{
printf("P ");
}
else if (i == 4)
{
printf("A ");
}
else if (i == 5)
{
printf("L ");
}
}
printf("\n");
}
getch();
return 0;
}
Output:
8.4
A
A B
A b C
A B C D
A b C d E
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
system("cls");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <= i; j++)
{
if (i % 2 == 0 && j % 2! = 0)
{
printf("%c ", 65 + j + 32);
}
else
{
printf("%c ", 65 + j);
}
}
printf("\n");
}
getch();
return 0;
}
Output:
8.5
Pattern:
####*
###**
##***
#****
*****
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
system("cls");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if(i+j<4) {
printf("# ");
}
else {
printf("* ");
}
}
printf("\n");
}
getch();
return 0;
}
Output:
8.6
4
34
234
1234
234
34
4
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
system("cls");
for (int i = 0; i < 7; i++)
{
for (int j = 1; j <= 4; j++)
{
if (i + j >= 4 && i - j! = 3 && i - j! = 4 && i - j! = 5)
{
printf("%d ", j);
}
else
{
printf(" ");
}
}
printf("\n");
}
getch();
return 0;
}
Output:
/*
*****
****
***
**
*
*/
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int n, space;
system("cls");
printf("Value of n: ");
scanf("%d", &n);
printf("Sequence is: \n");
for (int i = n; i >= 0; i--)
{
space = n - i;
while (space! = 0)
{
printf(" ");
space--;
}
for (int j = 0; j < i; j++)
{
printf("* ");
}
printf("\n");
}
getch();
return 0;
}
Output:
8.8
Print the following pattern:
0
1 1
2 2
3 3
2 4 4 2
1 5 5 1
0 6 0
1 5 5 1
2 4 6 2
3 3
2 2
1 1
0
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
system("cls");
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 13; j++)
{
if (i <= 6)
{
if ((i + j) == 6 || (j - i) == 6)
{
if (i <= 3)
{
printf("%2d ", i);
}
else
{
if ((i + j) == 6) {
printf("%2d ", j);
}
else {
printf("%2d", 12 - j);
}
}
}
else if (i > 3 && (i == j || (i + j) == 12))
{
printf("%2d ", i);
}
else
{
printf(" ");
}
}
else if (i > 6)
{
if (((i - j) == 6 || (i + j) == 18))
{
if (i <= 9)
{
if(i-j ==6) {
printf("%2d ", j);
}
else {
printf("%2d ", 12 - j);
}
}
else
{
printf("%2d ", 12 - i);
}
}
else if (i < 10 && (i == j || (i + j) == 12))
{
if(i+j ==12) {
printf("%2d ", j);
}
else {
printf("%2d ", j - 2);
}
}
else
{
printf(" ");
}
}
}
printf("\n");
}
getch();
return 0;
}
Output:
8.9
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
system("cls");
for (int i = 1; i <= 4; i++)
{
for (int j = 0; j < 7; j++)
{
if (i + j >= 4 && j - i != 3 && j - i != 4 && j - i != 5)
{
if (j == 3)
{
printf(" %d ", i);
}
else if (j == 2 || j==4)
{
printf(" %d ", i - 1);
}
else if (j == 1 || j ==5)
{
printf(" %d ", i - 2);
}
else
{
printf(" %d ", i - 3);
}
}
else
{
printf(" ");
}
}
printf("\n");
}
getch();
return 0;
}
Output:
9.Write a program to find the sum of all positive number entered by the user. Read
the numbers and keep calculating the sum until the user enter n.
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main ()
{
int number,sum = 0, count = 0;
char input_number;
system("cls");
while (1)
{
printf("Positive Number: ");
scanf(" %[n,0-9]", &input_number);
if (input_number == 'n')
{
break;
}
else
{
number = input_number - 48;
sum = sum + number;
count++;
}
}
printf("\nTotal sum is %d\n", sum);
printf("Total number of positive numbers is %d", count);
getch();
return 0;
}
Output:
Analysis
Through this lab activities we are able to understands concept behind nested loop
Why should we use it?
When we should use it what kind of program can be done using it?
Also, it builds our logic development in nested looping
Conclusion
We learn about nested loop.