0% found this document useful (0 votes)
3 views

Assigment 6 Nested Loops

Uploaded by

HarishPatil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assigment 6 Nested Loops

Uploaded by

HarishPatil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assigment 6 Nested Loops

Write a program to generate following triangle up to n lines.


1
12
123
#include <stdio.h>
#include<conio.h>
int main() {
int n,I,j;
Clrscr();
printf("Enter the number of lines: ");
scanf("%d", &n);

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


for ( j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

getch();
}

2. Write a program to generate following triangle up to n lines.


1
23
456

#include <stdio.h>
#include<conio.h>
int main() {
int n, i, j, num = 1;
Clrscr();
// Input the number of lines
printf("Enter the number of lines: ");
scanf("%d", &n);
// Outer loop for each row
for(i = 1; i <= n; i++) {
// Inner loop for printing numbers in each row
for(j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
// Move to the next line after each row
printf("\n");
}

getch();
}

3) Write a program to ngenerate following triangle up to n


lines.

ABC

DE

#include <stdio.h>
#include<conio.h>
int main() {
int n,i,j;
char ch = 'A';
Clrscr();
printf("Enter the number of lines: ");
scanf("%d", &n);

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


for ( j = 1; j <= i; j++) {
printf("%c ", ch++);
}
printf("\n");
}

getch();;
}
4) Write a program to generate following pattern.
5
45
345
2345
12345

#include <stdio.h>
#include<conio.h>
int main() {
int n, i, j;

// Input the number of lines (n)


printf("Enter the number of lines: ");
scanf("%d", &n);

// Outer loop for each row (starts from n and decreases to 1)


for(i = n; i >= 1; i--) {
// Inner loop for printing numbers in each row
for(j = i; j <= n; j++) {
printf("%d ", j);
}
// Move to the next line after each row
printf("\n");
}

getch();
}
5) Write a program to generate following pattern.
Aa
Aa Bb
Aa Bb Cc
Aa Bb Cc Dd

#include <stdio.h>
#include<conio.h>
int main() {
int n, i, j;
char ch_upper, ch_lower;

// Input the number of lines (n)


printf("Enter the number of lines: ");
scanf("%d", &n);

// Outer loop for each row


for(i = 1; i <= n; i++) {
// Inner loop for printing pairs of characters in each row
for(j = 1; j <= i; j++) {
ch_upper = 'A' + (j - 1); // Uppercase letter
ch_lower = 'a' + (j - 1); // Lowercase letter
printf("%c%c ", ch_upper, ch_lower); // Print uppercase
and lowercase pair
}
// Move to the next line after each row
printf("\n");
}

getch();
}
6) Write a program to accept two numbers as range and display
multiplication table of all numbers within that range.

#include <stdio.h>
#include<conio.h>
int main() {
int start, end, i, j;
Clrscr();
// Input the range of numbers
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);

// Outer loop for each number in the range


for(i = start; i <= end; i++) {
printf("Multiplication table for %d:\n", i);
// Inner loop to display the multiplication table for the
current number
for(j = 1; j <= 10; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n"); // Add a blank line after each table for
readability
}

getch();
}
7) Write a program to display all Armstrong between 1 and
1000
#include <stdio.h>
#include<conio.h>
int main() {
int num, originalNum, remainder, arm=0;
Clrscr();
// Loop through numbers from 1 to 1000
for(num = 1; num <= 1000; num++) {
originalNum = num;

// Calculate the sum of cubes of the digits


while (originalNum != 0) {
remainder = originalNum % 10;
arm = arm+(remainder * remainder * remainder);
originalNum = originalNum /10;
}

// If the sum of cubes equals the number, it's an Armstrong


number
if(arm == num) {
printf("%d ", num);
}
}

Getch() ;
}

8) Write a program to calculate sum of all digits of a input


number till it reduce to a single digit.
(Ex i/p 489 output sum:21 sum :3)

#include <stdio.h>
#include<conio.h>
int main() {
int num, sum=0, digit;
Clrscr();
// Input the number
printf("Enter a number: ");
scanf("%d", &num);

// Outer loop to keep reducing the sum to a single digit


while(num >= 10) {

// Inner loop to calculate the sum of digits


while(num > 0) {
digit = num % 10; // Extract the last digit
sum =sum+digit; // Add it to the sum
num = num/10; // Remove the last digit
}
// Print the sum after each round of digit sum calculation
printf("Sum of digits: %d\n", sum);
// Set the number to the new sum for further reduction
num = sum;
}

Getch();
}

You might also like