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

Loops 1

The document contains 20 programming problems involving loops in C language. For each problem, the requirement is defined, the C code solution is provided, and the expected output is listed. The problems cover a range of concepts involving loops - printing patterns, finding sums, factorials, checking prime/palindrome/Armstrong numbers, reversing digits etc. The solutions utilize for, while and nested loops to iterate through numbers and perform the required calculations.

Uploaded by

Arijit Pramanik
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)
43 views

Loops 1

The document contains 20 programming problems involving loops in C language. For each problem, the requirement is defined, the C code solution is provided, and the expected output is listed. The problems cover a range of concepts involving loops - printing patterns, finding sums, factorials, checking prime/palindrome/Armstrong numbers, reversing digits etc. The solutions utilize for, while and nested loops to iterate through numbers and perform the required calculations.

Uploaded by

Arijit Pramanik
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/ 20

LOOPS

ASSIGNMENT-1

1. WAP in c to print your institution name for 10 times.(using for)

Solution:

#include<stdio.h>
int main(){
for(int i=1;i<=10;i++){
printf("Uttar Banga Krishi Viswavidyalaya\n");
}
return 0;
}

Output:
2. WAP in C to print all the numbers in between 50 to 70.(using for)

Solution:

#include<stdio.h>
int main(){
for(int i=50;i<=70;i++){
printf("%d\t",i);
}
return 0;
}

Output:
3. WAP in C to print the sum of all the numbers between 1 to 5.(using for loops)

Solution:

#include<stdio.h>
int main(){
int sum=0;
for(int i=1;i<=5;i++){
sum+=i;
}
printf("Sum of first 5 numbers is %d",sum);
return 0;
}

Output:
4. Write a Program in C to print the multiplication table in the following format (by using

loop):

Ex: 3*1=3

3*2=6

…….

3*10=30

Solution:

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

Output:
5. Write a Program in C to find the factorial of a given number by using for loop.

Solution:

#include<stdio.h>
int main(){
int fact=1;
int n;
printf("please enter the number whose factorial you want to calculate: ");
scanf("%d",&n);
for(int i=1;i<=n;i++){
fact*=i;
}
printf("The factorial of the number is: %d",fact);
}

Output:
6. Write a Program in C that asks the user to enter a range and print the numbers which are

divisible by 5 only.

Solution:

#include<stdio.h>
int main(){
printf("please specify the lower and upper limits: ");
int l;
int u;
scanf("%d %d",&l,&u);
for(int i=l;i<=u;i++){
if(i%5==0){
printf("%d\t",i);
}
}
return 0;
}

Output:
7. Write a Program in C to check whether a number is prime or not.

Solution:

#include<stdio.h>
#include<math.h>
int main(){
printf("Enter the number: ");
float n;
scanf("%f",&n);
float u=sqrt(n);
int l=0;
for(int i=1;i<=u;i++){
if((int)n%i==0){
l++;
}
if(l>1){
break;
}
}
if(l==1){
printf("its a prime number");
}
else{
printf("its not prime");
}
}

Output:
8. Write a Program in C to find the factorial of a given number by while loop.

Solution:

#include<stdio.h>
int main(){
int fact=1;
int n;
printf("please enter the number whose factorial you want to calculate: ");
scanf("%d",&n);
int i=1;
while (i<=n)
{
fact*=i;
i++;
}
printf("The factorial of the number is: %d",fact);
}

Output:
9. Write a Program in C to enter a number through keyboard .Print the sum of the digits of the

number.

Solution:

#include<stdio.h>
int main(){
int sum=0;
printf("specify the upper bounds: ");
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
sum+=i;
}
printf("Sum of first %d numbers is %d",n,sum);
return 0;
}

Output:
10. Write a Program in C that asks the user for a range. The program would display all even

numbers falling within the range.

Solution:

#include<stdio.h>
int main(){
printf("please specify the lower and upper limits: ");
int l;
int u;
scanf("%d %d",&l,&u);
for(int i=l;i<=u;i++){
if(i%2==0){
printf("%d\t",i);
}
}
return 0;
}

Output:
11. WAP to reverse a number.

Solution:

#include<stdio.h>
int main(){
printf("enter a number: ");
int n;
scanf("%d",&n);
int n_copy=n;
int n2=0;
while(n>0){
int d= n%10;
n2 = (n2*10)+d;
n= n/10;
}
printf("The reversed form of %d is %d",n_copy,n2);
return 0;
}

Output:
12. WAP to compute the sum of the first n terms of the following series

S=1+1/2+1/3+1/4+…..

Solution:

#include<stdio.h>
int main(){
float sum=0;
printf("specify the upper bounds: ");
float n;
scanf("%f",&n);
for(int i=1;i<=n;i++){
sum+=(1/i);
}
printf("Sum of reciprocals of numbers within the range is %0.3f",sum);
return 0;
}

Output:
13. WAP in c to compute the sum of all even numbers between 1 to n.

Solution:

#include<stdio.h>
int main(){
int sum=0;
printf("specify the upper bounds: ");
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
if(i%2==0)
sum+=i;
}
printf("Sum of first even numbers within the range is %d",sum);
return 0;
}

Output:
14. WAP in c to count the number of digits in a number.

Solution:

#include<stdio.h>
int main(){
printf("enter a number: ");
int n;
scanf("%d",&n);
int d=0;
int n2=0;
while(n>0){
d++;
n= n/10;
}
printf("The number %d has %d digits",n,d);
return 0;
}

Output:
15. WAP in c to print the sum of first digit and last digit of a numeric number.

Solution:

#include<stdio.h>
#include<math.h>
int digits(int n){
float d=0;
int n2=0;
while(n>0){
d++;
n= n/10;
}
return d;
}
int main(){
printf("enter a number: ");
int n;
scanf("%d",&n);
int n_copy=n;
float d = digits(n);
int l = (n_copy%10);
int f= n_copy/powl(10,(d-1));
int sum = f+l;
printf("The sum of the first and last digits of the number are: %d",sum);
printf("thevalue of %d",n);
return 0;
}

Output:
16.WAP in c to check whether a number is a palindrome or not.

Solution:

//palindrome check

#include<stdio.h>
int main(){
printf("enter a number: ");
int n;
scanf("%d",&n);
int n_copy=n;
int n2=0;
while(n>0){
int d= n%10;
n2 = (n2*10)+d;
n= n/10;
}
if(n_copy==n2){
printf("its a palindrome number");
}
else{
printf("its not a palindrome number");
}
return 0;
}

Output:
17.WAP in c to check whether a number is an Armstrong number or not.

Solution:

//armstrong number check


#include<stdio.h>
#include<math.h>
int main(){
printf("enter a number: ");
int n;
scanf("%d",&n);
int n_copy=n;
int n2=0;
while(n>0){
int d= n%10;
n2+=(d*d*d);
n= n/10;
}
if(n_copy==n2){
printf("its an armstrong number");
}
else{
printf("its not an armstrong number");
}
return 0;
}

Output:
18. WAP in c to check whether a number is a strong number or not.

Solution:

//Strong number check


#include<stdio.h>
int factorial(int k){
int f=1;
for(int i=1;i<=k;i++){
f*=i;
}
return f;
}
int main(){
printf("enter a number: ");
int n;
scanf("%d",&n);
int n_copy=n;
int n2=0;
while(n>0){
int d= n%10;
n2+= factorial(d);
n= n/10;
}
if(n_copy==n2){
printf("its a Strong number");
}
else{
printf("its not a Strong number");
}
return 0;
}

Output;
19.WAP in c to find out the frequency of each digit in a number.

Solution:

#include<stdio.h>
#include<math.h>
void check_and_count(int d,int n){
int count=0;
int n2 =n;
while(n>0){
int d2= n%10;
if(d2==d){
count++;
}
n= n/10;
}
printf("There are %d %ds in %d\n",count,d,n2);
}
int main(){
printf("enter a number: ");
int n;
scanf("%d",&n);
int n_copy =n;
while (n>0)
{
int d = n%10;
check_and_count(d,n_copy);
n= n/10;
}
return 0;
}

Output:
20.WAP to find all factors of a number.

Solution:

#include<stdio.h>
int main(){
int n;
printf("enter a number ");
scanf("%d",&n);
int factors=0;
for(int i=1;i<=n;i++){
if(n%i==0){
factors++;
}
}
printf("The number %d has %d factors",n,factors);
return 0;
}

Output:

You might also like