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

Write C Programs For Following-Loop

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)
8 views

Write C Programs For Following-Loop

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/ 5

Write c programs for following:

1) Accept a number from user and find its factorial using recursion feature:
int fact (int);
void main()
{
int n, factorial;
printf("Enter a value");
scanf("%d",&n);
factorial = fact(n);
printf("The answer is:%d",factorial);
getch();
}
int fact( int a)
{ int i;
if(a==1) return(1);
else
i = a*fact(a-1);
return(i);
}
2) Accept a number and print its reverse.
#include <stdio.h>

void main() {

int n, reverse = 0, rem;


printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}

printf("Reversed number = %d", reverse);

getch();
}
3) To check whether number is Armstrong or not
#include <stdio.h>
void main() {
int num, o, rem, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
o = num;
while (o != 0) {
rem = o % 10;
result += rem * rem * rem;
o /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

getch();
}
4) To print the pattern:
3 2 1
2 1
1

int main() {
int i,j;
clrscr();
for(i=3;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("%d \t",j);
printf("\n");
}
getch();
}

5) 1
22
333
4444
5 5 5 55

int main() {
int i,j;
clrscr();
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= i; j++)

printf("%d",i);
printf("\n");
}

getch();
}

6) 1 2 3 4 5 6
12345
1234
123
12
1
int main () {
int i,j;
clrscr();
for(i = 6; i >= 1; i--)
{
for(j = 1; j <= i; j++)
printf ("%d \t",j);
printf("\n");
}
getch();
}

7) To print a pattern:
X XXX
X XX
XX
X

# include <stdio.h>
void main()
{
int a,b;
for(a=5;a>1;a--)
{
for(b=a;b>=1;b--)
printf("X \t");
printf("\n");
}
getch();
}

8) To print a pattern:
1
23
456

#include <stdio.h>
void main() {
int rows, i, j, n = 1;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", n);
++n;
}
printf("\n");
}
getch();
}

9) 3 2 1
32
3

int main() {
int a,i,j,b=3;
for(i=3;i>=1;i--)
{
a=b;
for(j=1;j<=i;j++)
{
printf("%d \t",a);
a--;
}
printf("\n");
}
return 0;
}
10) 1
123
int main() {
int a,i,j;
for(i=1;i<=1;i++)
{
printf("%d \n",i);
for(j=1;j<=3;j++)
{
printf("%d\t",j);
}
}
return 0;
}
11) 1
123
1
123
1
123
int main() {
int a,i,j,b;
for(b=1;b<=3;b++)
{
for(i=1;i<=1;i++)
{
printf("%d \n",i);
for(j=1;j<=3;j++)
{
printf("%d\t",j);
}
}
printf("\n");
}
return 0;
}

You might also like