0% found this document useful (0 votes)
2 views6 pages

C Looping Programs With Solutions

Uploaded by

kgautam21080410
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)
2 views6 pages

C Looping Programs With Solutions

Uploaded by

kgautam21080410
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

C Programming – Looping Statements Practice Programs with Solutions

1. Program to Print Fibonacci Series up to N terms


#include <stdio.h>
int main() {
int n, i;
int a = 0, b = 1, next;
]= printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for(i = 1; i <= n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
return 0;
}

2. Program to Check Whether a Number is Armstrong or Not


#include <stdio.h>
int main() {
int num, original, remainder, result = 0;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while(original != 0) {
remainder = original % 10;
result += remainder * remainder * remainder;
original /= 10;
}
if(result == num)
printf("Armstrong Number");
else
printf("Not an Armstrong Number");
return 0;
}

3. Program to Print Prime Numbers Between 1 and N


#include <stdio.h>
int main() {
int n, i, j, flag;
printf("Enter value of N: ");
scanf("%d", &n);
for(i = 2; i <= n; i++) {
flag = 0;
for(j = 2; j <= i/2; j++) {
if(i % j == 0) {
flag = 1;
break;
}
}
if(flag == 0)
printf("%d ", i);
}
return 0;
}

4. Program to Reverse a Number


#include <stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed Number = %d", reverse);
return 0;
}

5. Program to Find Sum of Digits of a Number


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

printf("Enter a number: ");


scanf("%d", &num);

while(num != 0) {
remainder = num % 10;
sum += remainder;
num /= 10;
}

printf("Sum of digits = %d", sum);


return 0;
}

6. Program to Print the Following Pattern


*
**
***
****
#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;
}

7. Program to Find GCD of Two Numbers using Loop


#include <stdio.h>
int main() {
int a, b, i, gcd;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
for(i = 1; i <= a && i <= b; i++) {
if(a % i == 0 && b % i == 0)
gcd = i;
}
printf("GCD = %d", gcd);
return 0;
}

8. Program to Check Whether a Number is Palindrome


#include <stdio.h>
int main() {
int num, original, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while(num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
if(original == reverse)
printf("Palindrome Number");
else
printf("Not a Palindrome");

return 0;
}

C Looping Programs – outputs for above prog


1. Fibonacci Series

Sample Run:

Enter number of terms: 5


Fibonacci Series: 0 1 1 2 3
2. Armstrong Number

Sample Run:

Enter a number: 153


Armstrong Number

3. Prime Numbers Between 1 and N

Sample Run:

Enter value of N: 10
2357

4. Reverse a Number

Sample Run:

Enter a number: 1234


Reversed Number = 4321

5. Sum of Digits

Sample Run:

Enter a number: 4587


Sum of digits = 24

6. Star Pattern

Sample Run:

*
**
***
****

7. GCD of Two Numbers

Sample Run:

Enter two numbers: 12 18


GCD = 6

8. Palindrome Number

Sample Run:

Enter a number: 121


Palindrome Number

9. C Program for Floyd’s Triangle


#include <stdio.h>

int main() {

int rows, i, j;

int num = 1;

printf("Enter number of rows: ");

scanf("%d", &rows);

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

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

printf("%d ", num);

num++;

printf("\n");

return 0;

output

23

456

7 8 9 10

11 12 13 14 15

If user enters 4

Iteration 1:

 i=1

 Prints: 1

Iteration 2:

 i=2

 Prints: 2 3

Iteration 3:
 i=3

 Prints: 4 5 6

Iteration 4:

 i=4

 Prints: 7 8 9 10

You might also like