













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document is a comprehensive collection of loop-based programming exercises in C, designed to strengthen your understanding of for, while, and do-while loops. It includes over 15+ practical problems ranging from basic patterns to logic-based challenges, each paired with clear, well-commented solutions. Ideal for beginners and intermediate learners aiming to gain confidence and fluency in using loops effectively in C programming.
Typology: Exercises
1 / 21
This page cannot be seen from the preview
Don't miss anything!
This program prompts the user to enter a number. It calculates the number of digits in the number using a while loop. Then, it extracts the first and last digits of the number. Using a for loop, it swaps the first and last digits and prints the result. Constraints: The input number must be an integer. TEST CASE 1: Input: 12345 Output: Number after swapping first and last digits: 52341 TEST CASE 2: Input: 9876 Output: Number after swapping first and last digits: 6879 TEST CASE 3: Input: 101010 Output: Number after swapping first and last digits: 101011 #include <stdio.h> #include<math.h> int main() { int num,orig,first,last,swap,digit; printf("enter a number\n"); scanf("%d",&num); orig=num; while (num > 0) { digit++; num /= 10; } printf("digit = %d",digit); last=orig%10; printf("last %d\n",last); first=orig/pow(10,(digit-1)); printf("first %d\n",first); int power=pow(10,(digit-1)); swap=last*power; printf("%d\n",swap); swap=swap+(orig%power); printf("%d\n",swap); swap=swap-last; printf("%d\n",swap); swap=swap+first; printf("swap %d",swap); return 0; }
Write a program to print Floyd's Triangle. Floyd's Triangle is a triangular pattern of numbers where the numbers are arranged in a sequential manner, starting from 1 and incrementing by 1 for each subsequent row. The pattern is formed by printing the numbers in a triangular shape. Constraints: The number of rows for Floyd's Triangle should be a positive integer. The number of rows should not exceed the maximum limit of the integer data type (typically 32,767 for a 16-bit integer). TEST CASE 1: Input : 5 Output: 1 01 101 0101 10101 TEST CASE 2: Input : 7 Output: 1 01 101 0101 10101 010101 1010101 TEST CASE 3: Input : - Output: No output #include <stdio.h> int main() { int i,j,p,q,num; printf("enter a number"); scanf("%d",&num); for(i=1;i<=num;i++){ if(i%2==0){ p=1; q=0; }else{ p=0; q=1; }
printf("digits is %d\n",digit); a=power%100; printf("first part is %d\n",a); b=power/100; printf("second part is %d\n",b); mun=a+b; printf("added value is %d\n",mun); if(num==mun){ printf("it is kaprekar number\n"); }else{ printf("it is not a kaprekar number\n"); } return 0; }
Develop a program to check whether the given integer is palindrome or not. If it is palindrome, then multiply each digit of the given number. If it is not palindrome, then sum each digit of the given number. Constraints: The number should be an integer. The program should handle both positive and negative integers. The program should not use built-in functions to reverse the number directly. The program should use loops for iteration. TEST CASE 1: Input: 12321 Expected Output: 12 Explanation: Since 12321 is a palindrome, we multiply each digit: 1 * 2 * 3 * 2 * 1 = 12 TEST CASE 2: Input: 45678 Expected Output: 30 Explanation: Since 45678 is not a palindrome, we sum each digit: 4 + 5 + 6 + 7 + 8 = 30 TEST CASE 3: Input: 1221 Expected Output: 4 Explanation: Since 1221 is a palindrome, we multiply each digit: 1 * 2 * 2 * 1 = 4
#include <stdio.h> int main() { int n,r,temp,t; int sum=0; int product=1; int add=0; printf("enter a number"); scanf("%d",&n); temp=n; while(n>0){ r=n%10; sum=(sum10)+r; n=n/10; } if(temp==sum){ while(temp>0){ t=temp%10; product=productt; temp=temp/10; } printf("it is palindrome\n"); printf("product is %d",product); } else{ while(temp>0){ t=temp%10; add=add+t; temp=temp/10; } printf("it is not a palindrome\n"); printf("Addition is %d",add); } return 0; }
Write a program that implements a program to count the number of digits in a given integer using a do-while loop. Constraints: The input integer can be positive or negative. The input integer should be within the range supported by the int data type.. The program should handle negative numbers by converting them to positive before counting the digits. The program should use a do-while loop to count the digits. The program should handle input validation, ensuring that the user enters a valid integer. TEST CASE 1: Input:
The program should not use any external libraries or functions to compute the sum of cubes of even numbers. TEST CASE 1: Input: Limit: 10 Output: Sum of cubes of even numbers up to 10: 1800 Explanation: (2^3+4^3+6^3+8^3+10^3)=(8+64+216+512+1000)= TEST CASE 2: Input: Limit: 5 Output: Sum of cubes of even numbers up to 5: 72 TEST CASE 3: Input:
Output: Invalid input #include <stdio.h> int main() { int num,add,c; int i=0; int k=0; printf("enter a number"); scanf("%d",&num); if(num>0){ while(i<=num){ i=i+2; if(i<=num){ printf("even number is %d\n",i); c=iii; printf("cube is %d\n",c); k=k+c; } } printf("Addition is %d",k); }else{ printf("invalid input"); } return 0;
Develop a program to convert a three-digit number into its word representation Constraints: The input number must be a three-digit number. The input number must be between 100 and 999, inclusive. TEST CASE 1: Input: Number: 123 Output: "one hundred twenty-three" TEST CASE 2: Input: Number: 709 Output: "seven hundred nine" TEST CASE 3: Input: Number: 450 Output: "four hundred fifty" #include <stdio.h> int main() { int num; printf("enter a number"); scanf("%d",&num); int hundred=num/100; switch(hundred){ case 0: printf("zero "); break; case 1: printf("One hundred "); break; case 2: printf("Two hundred "); break; case 3: printf("Three hundred "); break; case 4: printf("Four hundred"); break; case 5: printf("Five hundred "); break;
case 6: printf("sixty"); break; case 7: printf("seventy"); break; case 8: printf("eighty"); break; case 9: printf("ninety"); break; } int ones=num%10; switch(ones){ case 0: printf(" "); break; case 1: printf(" one"); break; case 2: printf(" two"); break; case 3: printf(" three"); break; case 4: printf(" Four"); break; case 5: printf(" five"); break; case 6: printf(" six"); break; case 7: printf(" Seven"); break; case 8: printf(" Eight"); break; case 9: printf(" Nine"); break; } return 0; }
Write a program to print the sum of the last and the first digit of a number given by the user.
Constraints: The input number should be an integer. The input number should not exceed the range of integers supported by the system. TEST CASE 1: Input: 12345 Output: Sum of first and last digit: 6 Explanation: The first digit is 1 and the last digit is 5. Their sum is 1 + 5 = 6. TEST CASE 2: Input: - Output: Sum of first and last digit: - Explanation: The first digit is -9 and the last digit is -1. Their sum is -9 + (-1) = -8. TEST CASE 3: Input: - Output: Sum of first and last digit: 0 Explanation: The first digit is -5 and the last digit is -5. Their sum is -5 + (-5)=
#include <stdio.h> #include<math.h> int main() { int num,first,last,add,digit; int orig; printf("enter a number"); scanf("%d",&num); orig=num; if(num>0){ num=num1; }else{ num=num(-1); } while(num>0){ digit++; num/=10; } printf("No. of digit is %d",digit); int power; power=pow(10,(digit-1)); last=orig%10; first=orig/power; printf("\nFirst digit is %d",first); printf("\nLast digit is %d",last); add=first+last; printf("\nAddition of first and last digit is %d",add); return 0; }
num-=five5; printf("5 * %d\n",five); } if(num>=1){ one=num/1; num-=one1; printf("1 * %d\n",one); } add=num+ten+five+one; printf("Add is %d\n",add); printf("No of denominations %d:%d",tem,add); return 0; }
Write a program to Generate and display the first n terms of the Fibonacci series, where each term is the sum of the two preceding terms. Constraints: The number of terms (n) in the Fibonacci series should be a positive integer. Each term in the series should not exceed the maximum integer value. The maximum number of terms is limited to 20. TEST CASE 1: Input: Number of terms to display : 10 Output : Here is the Fibonacci series upto to 10 terms : 0 1 1 2 3 5 8 13 21 34 TEST CASE 2: Input: Number of terms to display : 100 Output : Invalid Input TEST CASE 3: Input: Number of terms to display : 20 Output : Here is the Fibonacci series upto to 20 terms : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 #include <stdio.h> int main() { int t1,t2,num; int i; int next; t1=0; t2=1;
printf("enter a number"); scanf("%d",&num); if(num<=20){ printf("%d %d",t1,t2); for(i=2;i<num;i++){ next=t1+t2; t1=t2; t2=next; printf(" %d",next); } }else{ printf("invalid input"); } return 0; }
Print a square pattern of asterisks (*) with a hollow interior filled with spaces (). Constraints: Minimum Size: The user must enter a side length greater than or equal to 2 (adjustable as needed). This ensures a hollow interior is possible. TEST CASE 1: Side Length: 3: Output:
TEST CASE 2: Side Length: 5 (Even): Output:
TEST CASE 3: Side Length: 6 (Odd): Output:
Ones complement: 10111100 TEST CASE 2: Input:
Output: Invalid Input TEST CASE 3: Input: 1010 Output: Ones complement: 0101 #include <stdio.h> int main() { char a[80]; int i; printf("enter a number"); scanf("%s",&a); for(i=0;a[i];i++){ a[i]=(a[i]=='0')?'1':'0'; } printf("%s",a); return 0; }
Calculate the sum of a series where each term is formed by concatenating the digit 9 to the previous term. Print Invalid input if the input is <=0. Constraints: The number of terms in the series should be a positive integer. Each term in the series should not exceed the maximum integer value. The maximum number of terms is limited to 10. TEST CASE 1: Input: Number or terms : Output : 9 99 999 9999 99999 The sum of the series = 111105 TEST CASE 2: Input: Number or terms : Output : 9 99 999 The sum of the series = 1107
Input: Number or terms : Output : Invalid Input #include<stdio.h> int main() { int num; int a=9; int add=0,k; int i,j; printf("enter a number"); scanf("%d",&num); if(num>0){ for(i=0;i<num;i++){ for(j=0;j<=i;j++){ printf("%d",a); } printf(" "); } } else{ printf("invalid input"); } for(k=1;k<=num;k++){ add= add+a; a=a*10 + 9; } printf("\nThe sum of the series = %d",add); return 0; }
You're working on a project for a data analysis firm that specializes in processing binary data obtained from various sources such as sensors, digital devices, and network logs. As part of the data processing pipeline, you need to
Constraints: The input binary number should be a non-negative integer. The binary number should be within the range supported by the long int data type. The program should handle input validation, ensuring that the user enters a valid binary number. TEST CASE 1: Input: 01000011 Output: Ones complement: 10111100 TEST CASE 2: Input:
Output: Invalid Input TEST CASE 3: Input: 1010 Output: Ones complement: 0101 #include <stdio.h> int main() { int n; printf("enter number of bits "); scanf("%d",&n); char a[100]; char c[n]; int i=0; int carry=1; printf("enter a binary number "); scanf("%s",a); printf("%s\n",a); for(i=0;a[i];i++){ a[i]=(a[i]=='0')?'1':'0'; } printf("%s\n",a); for(i=n-1;i>=0;i--){ if((a[i]=='1')&&(carry==1)){ c[i]='0'; } else if((a[i]=='0')&&(carry==1)){ c[i]='1'; carry=0;
else{ c[i]=a[i]; } } c[n]='\0'; printf("%s",c); return 0; }
You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits). Return the largest possible value of num after any number of swaps. Constraints: 1 <= num <= 109 TEST CASE 1: Input: num = 1234 Output: 3412 Explanation: Swap the digit 3 with the digit 1, this results in the number 3214. Swap the digit 2 with the digit 4, this results in the number 3412. Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number. Also note that we may not swap the digit 4 with the digit 1 since they are of different parities. TEST CASE 2: Input: num = 65875 Output: 87655 Explanation: Swap the digit 8 with the digit 6, this results in the number 85675. Swap the first digit 5 with the digit 7, this results in the number 87655. Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number TEST CASE 3: Input: num = 6587 Output: 8765 #include<stdio.h> #include<string.h> int main() { char n[100],tem; printf("enter a number:"); scanf("%s",&n); for(int i=0;i<strlen(n)-1;i++){ for(int j=i+1;j<strlen(n);j++){ if(n[i]%2==n[j]%2 && n[i]<n[j]){