[go: up one dir, main page]

0% found this document useful (0 votes)
12 views8 pages

C Programming Patterns and Calculations

The document contains 18 code snippets showing different patterns that can be printed using for loops in C programming. The snippets demonstrate basic loops, nested loops, and conditional logic to print numbers, stars, boxes and pyramid shapes incrementally or decrementally.

Uploaded by

khervinmalabosa
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)
12 views8 pages

C Programming Patterns and Calculations

The document contains 18 code snippets showing different patterns that can be printed using for loops in C programming. The snippets demonstrate basic loops, nested loops, and conditional logic to print numbers, stars, boxes and pyramid shapes incrementally or decrementally.

Uploaded by

khervinmalabosa
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
You are on page 1/ 8

No.

1 - Display Number
#include <stdio.h>
int x, n;
int main() {
printf("Enter a number: ");
scanf("%d", &n);

for (int x = 1; x <= n; x++) {


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

No.2 - Sum of all numbers


#include <stdio.h>
int x, n, sum;
int main() {
sum=0;
printf("Enter a number: ");
scanf("%d", &n);

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


sum += x;
}
printf("The sum of all numbers from 1 to %d is %d\n", n, sum);
return 0;
}

No.3 - Even numbers


#include <stdio.h>
int x, n, even;
int main() {
even=2;
printf("Enter a number: ");
scanf("%d", &n);
printf("The first %d even numbers are: ", n);

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


printf("%d ", even);
even+=2;
}
return 0;
}
No.4 - Odd numbers
#include <stdio.h>
int x, n, odd;
int main() {
odd=1;
printf("Enter a number: ");
scanf("%d", &n);
printf("The first %d even numbers are: ", n);

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


printf("%d ", odd);
odd+=2;
}
return 0;
}

No. 5 - Factorial of a number


#include <stdio.h>
int x, n, factorial;
int main() {
factorial=1;
printf("Enter a number (factorial): ");
scanf("%d", &n);

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


factorial *= x;
}
printf("The product of all numbers from 1 to %d or %d! is %d\n", n, n, factorial);
return 0;
}

No. 6 - Sum of divisible by 3


#include <stdio.h>
int x, sum;
int main() {
sum=0;

for (x=1; x<=1000; x++){


if(x%3==0){
sum += x;
}
}
printf("The sum of all numbers divisible by 3 from 1 to 1000 is %d\n", sum);
return 0;
}
No. 7 - Factors of a number
#include <stdio.h>
int x, n;
int main() {
printf("Enter a number: ");
scanf("%d", &n);

printf("Factors of %d are: ", n);


for (x=1; x<=n; x++) {
if (n % x == 0) {
printf("%d ", x);
}
}
return 0;
}

No. 8 - Sum of the odd and the even


#include <stdio.h>
int x, n, even, odd;
int main() {
even=0;
odd=0;
printf("Enter a number: ");
scanf("%d", &n);

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


if(x % 2 == 0) {
even += x;
}
else {
odd += x;
}
}
printf("Sum of even and odd numbers from 1 to %d is: \n", n);
printf("Even numbers: %d\n", even);
printf("Odd numbers: %d\n", odd);
return 0;
}
No. 9 - Increment
#include <stdio.h>
int y, x, n;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

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


for (x=1; x<=y; x++) {
printf("*");
}
printf("\n");
}
return 0;
}

No. 10 - Decrement
#include <stdio.h>
int y, x, n;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

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


for (x=n; x>=1*y; x--) {
printf("*");
}
printf("\n");
}
return 0;
}
No. 11 - Space-- & Increment
#include <stdio.h>
int y, x, n, s;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

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


for (s=n; s>=1*y; s--) {
printf(" ");
}
for (x=1; x<=y; x++) {
printf("*");
}
printf("\n");
}
return 0;
}

No.12 - Space++ & Decrement


#include <stdio.h>
int y, x, n, s;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

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


for (s=1; s<=1*y; s++) {
printf(" ");
}
for (x=n; x>=y*1; x--) {
printf("*");
}
printf("\n");
}
return 0;
}
No. 13 - Decrement Number
#include <stdio.h>
int y, x, n;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

for (y=n; y>=1; y--) {


for (x=y; x>=1; x--) {
printf("%d", x);
}
printf("\n");
}
return 0;
}

No. 14 - Increment Number


#include <stdio.h>
int y, x, n;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

for (y=n; y>=1; y--) {


for (x=1; x<=y; x++) {
printf("%d", x);
}
printf("\n");
}
return 0;
}

No. 15 - Box
#include <stdio.h>
int x, y, n, m;
int main() {
printf("Enter width (n): ");
scanf("%d", &n);
printf("Enter length (m): ");
scanf("%d", &m);

for(y=1; y<=m; y++) {


for(x=1; x<=n; x++) {
printf("*");
}
printf("\n");
}
return 0;
}
No. 16 - Box Hollow
#include <stdio.h>
int x, y, n, m;
int main() {
printf("Enter width (n): ");
scanf("%d", &n);
printf("Enter length (m): ");
scanf("%d", &m);

for(y=1; y<=m; y++) {


for(x=1; x<=n; x++) {
if(y==1 || y==m || x==1 || x==n){
printf("*");
}
else{
printf(" ");
}
}
printf("\n");
}
return 0;
}

No. 17 - Pyramid Up
#include <stdio.h>
int y, x, s, n;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

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


for (s=n; s>=y; s--){
printf(" ");
}
for (x=1; x<=2*y-1; x++) {
printf("*");
}
printf("\n");
}
return 0;
}
No. 18 - Pyramid Down
#include <stdio.h>
int y, x, s, n;
int main() {
printf("Enter the value of n: ");
scanf("%d", &n);

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


for(s=1; s<=y*1; s++){
printf(" ");
}
for(x=n*2; x>=y*2; x--){
printf("*");
}
printf("\n");
}
return 0;
}

You might also like