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

Write A C Program To Find The Sum of First N Natural Numbers

The document contains 11 programming problems related to patterns and numerical operations. It provides the problem statements and full C code solutions for generating various patterns using loops and conditionals such as right triangles with numbers, pyramids with asterisks, and calculating factorials. The problems cover basic programming concepts in C including loops, conditionals, input/output, and arithmetic operations.

Uploaded by

Khawla Alameri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views8 pages

Write A C Program To Find The Sum of First N Natural Numbers

The document contains 11 programming problems related to patterns and numerical operations. It provides the problem statements and full C code solutions for generating various patterns using loops and conditionals such as right triangles with numbers, pyramids with asterisks, and calculating factorials. The problems cover basic programming concepts in C including loops, conditionals, input/output, and arithmetic operations.

Uploaded by

Khawla Alameri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Applied Programming for Engineers EGN-2712

Fujairah Campuses
Dr. Hussein Abdul-Rahman
Assignment 7
1. Write a C program to find the sum of first n natural numbers.
Example: 
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10 
The Sum is: 55
#include <stdio.h>
int main ()
{
int a, sum = 0;
printf("The first 10 natural number:\n ");
for( a = 1; a < 11; a = a + 1 )
{
sum = sum + a;
printf("%d \t", a);
}
printf("\n The sum is: %d\n" , sum);
return 0;
}

2. Write a program in C to read 10 numbers from keyboard and find their sum
and average.
#include <stdio.h>
int main ()
{
int a, n, sum = 0;
float avg;
printf("Input the 10 number: \n ");
for( a = 1; a <= 10; a++ )
{
printf("Number-%d: ", a);
scanf("%d", &n);
sum +=n;
}
avg=sum/10.0;
printf("\n The sum of 10 no is: %d\n the avg is: %f\n" , sum, avg);
return 0; }

1
3. Write a program in C to display the pattern like right angle triangle using an
asterisk. The pattern like:

*
**
***
****
#include <stdio.h>
int main()
{
int i, j;
for(i=1; i<=4; i++)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

2
4. Write a program in C to display the pattern like right angle triangle with a
number. The pattern like:

1
12
123
1234
#include <stdio.h>
int main()
{
int x=0;
while(x<4)
{
x++;
int y=1;
while(y<=x)
{
printf("\t %d", y);
y++;
}
printf(" \n ");
}
return 0;
}

3
5. Write a program in C to make such a pattern like right angle triangle with a
number which will repeat a number in a row. The pattern like:
1
22
333
4444
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
  for(j=1;j<=i;j++)
   printf("%d",i);
  printf("\n");
}
return 0;
}

6. Write a program in C to make such a pattern like right angle triangle with
number increased by 1. The pattern like:

1
23
456
7 8 9 10
#include <stdio.h>
int main()
{
int i,j,k=1;
for(i=1;i<=4;i++)
{
  for(j=1;j<=i;j++)
   printf("%d ",k++);
  printf("\n");
}
return 0;
}

4
7. Write a program in C to make such a pattern like a pyramid with numbers
increased by 1. 

1
23
456
7 8 9 10
#include <stdio.h>
int main()
{
int i,j,spc=7,k,t=1;
for(i=1;i<=4;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
   for(j=1;j<=i;j++)
   printf("%d ",t++);
  printf("\n");
spc--;
}
return 0;
}

5
8. Write a program in C to make such a pattern like a pyramid with an asterisk. 

*
**
***
****
#include <stdio.h>
int main()
{
int i,j,spc,k;
spc=7;
for(i=1;i<=4;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
   for(j=1;j<=i;j++)
   printf("* ");
  printf("\n");
spc--;
}
return 0;
}

9. Write a C program to calculate the factorial of a given number.


#include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}

6
10. Write a program in C to make such a pattern like a pyramid with a number
which will repeat the number in the same row. 

1
22
333
4444

#include <stdio.h>
int main()
{
int i,j,spc,k;
spc=7;
for(i=1;i<=4;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
   for(j=1;j<=i;j++)
   printf("%d ",i);
  printf("\n");
spc--;
}
return 0;
}

7
11. Write a program in C to print the Floyd's Triangle. 

1
01
101
0101
10101
#include <stdio.h>
int main()
{
int i,j,p,q;
for(i=1;i<=5;i++)
{
if(i%2==0)
{ p=1;q=0;}
else
{ p=0;q=1;}
for(j=1;j<=i;j++)
   if(j%2==0)
   printf("%d",p);
   else
   printf("%d",q);
printf("\n");
}
return 0;
}

You might also like