The document contains 36 code snippets that demonstrate various programming concepts in C including:
- Printing divisors of a number
- Checking if a number is a palindrome
- Counting digits in a number
- Checking if a number is perfect
- Printing prime numbers in a range and calculating their sum
- Calculating the sum of first and last digits of a number
- Calculating the sum of digits in a number
- Printing the reverse of a number
- Printing star patterns using nested loops
Each code snippet contains a brief description, sample input/output and the C code to implement the described functionality in 3 sentences or less.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
16 views
C Programming 2
The document contains 36 code snippets that demonstrate various programming concepts in C including:
- Printing divisors of a number
- Checking if a number is a palindrome
- Counting digits in a number
- Checking if a number is perfect
- Printing prime numbers in a range and calculating their sum
- Calculating the sum of first and last digits of a number
- Calculating the sum of digits in a number
- Printing the reverse of a number
- Printing star patterns using nested loops
Each code snippet contains a brief description, sample input/output and the C code to implement the described functionality in 3 sentences or less.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
27) Take a number from the user and print the proper 32.
32. Write a program to check whether a given number
divisor of that number. is a palindrome or not. #include <stdio.h> #include <stdio.h> int main() { int main() { Sample Input: Sample Output: Sample Input: Sample Output: int number, originalNumber, reverse Enter a Number: 404 Palindrome int number; Enter a Number: 9 Divisors = 1, 3 = 0; scanf("%d", printf("Enter a number: "); &number); scanf("%d", &number); printf("Proper divisors of %d: ", number); originalNumber = number; for (int i = 1; i < number; i++) { while (number != 0) { int digit = number % 10; if (number % i == 0) { reverse = reverse * 10 + digit; printf("%d ", i);} number /= 10;} } if (originalNumber == reverse) { printf("\n"); printf("Palindrome\n"); } else { return 0;} printf("Not Palindrome\n"); 28) Write a program } return 0;} to count the number Sample Input: Sample Output: of digits Enter a Number: 123452 Total digits: 6 33) Take a number from the user and check whether in a given number. it is a perfect number or not. #include <stdio.h> #include <stdio.h> int main() { int main() { int number, sum = 0; Sample Input: Sample Output: int number, count = 0; printf("Enter a number: "); Enter a Number: 6 6 is Perfect Number printf("Enter a number: "); scanf("%d", &number); scanf("%d", &number); if (number == 0) { for (int i = 1; i < number; i++) { count = 1; if (number % i == 0) { } else { sum += i;} while (number != 0) { } if (sum == number) { number /= 10; printf("%d is a perfect number.\n", number); count++;} } else { } printf("%d is not a perfect number.\n", number); printf("Number of digits: %d\n", count); } return 0;} return 0;} 29. Write a program to calculate the sum of the first 34) Write a program to print all the prime numbers in and the last digits in a given number. a range of 1-n & sum of all prime numbers. #include <stdio.h> #include <stdio.h> int main() { int main() { int number, firstDigit, lastDigit, sum; int n; printf("Enter a number: "); int isPrime; scanf("%d", &number); int sum =0; Sample Input: Sample Output: scanf("%d", &n); firstDigit = number; Enter a Number: 403 Sum = 7 printf("Prime numbers between 1 and %d are:\n", n); while (firstDigit >= 10) { for (int i = 2; i <= n; i++) { firstDigit /= 10; isPrime = 1; } for (int j = 2; j < i; j++) { lastDigit = number % 10; if (i % j == 0) { sum = firstDigit + lastDigit; isPrime = 0; printf("Sum of first and last digits: %d\n", sum); break;} } return 0;} if (isPrime) { 30. Write a program to calculate the sum of the digits printf("%d\n", i); in a given number. sum += i; } #include <stdio.h> Sample Input: Sample Output: } int main() { Enter a Number: 9012 Sum = 12 printf("Sum of prime: %d\n", sum); int number, sum = 0, digit; return 0;} scanf("%d", &number); while (number != 0) { 35. Write a C program to print the following shape of digit = number % 10; the star [use nested loop]. sum += digit; #include <stdio.h> number /= 10; int main() { Sample Input Sample Output } int rows; Enter a number: 4 **** printf("Sum of the digits: %d\n", sum); printf("Enter a number: "); **** return 0;} scanf("%d", &rows); **** 31. Write a program to print the reverse of a given for (int i = 1; i <= rows; i++) { **** for (int j = 1; j <= rows; j++) { number. printf("*");} #include <stdio.h> printf("\n"); int main() { Sample Input: Sample Output: } int number, reverse = 0; Enter a Number: 231 Reverse = 123 return 0;} scanf("%d", &number); 36. Write a C program to print the following shape of the star[use while (number != 0) { int digit = number % 10; nested error] #include <stdio.h> Sample Input Sample Output reverse = reverse * 10 + digit; int main() { Enter a number: 4 * number /= 10; int rows; ** printf("Enter a number: "); } scanf("%d", &rows); *** printf("Reverse: %d\n", reverse); **** return 0;} for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("*");} printf("\n"); } 42. Write a program in C to make such a pattern like a pyramid with numbers increased by 1. 37. Write a C program to print the following shape of the The pattern like : 1 #include <stdio.h> star[use nested error] int main() { for (int i = 1; i <= rows; i++) { Sample Input Sample Output int rows, number = 0; 23 for (int j = 1; j <= rows - i; j++) { Enter a number: 4 * ** printf("Enter a number of row: "); printf(" "); scanf("%d", &rows); *** 456 } **** for (int k = 1; k <= i; k++) { printf("*"); for (int i = 1; i <= rows; i++) { 7 8 9 10 } for (int j = 1; j <= i; j++) { printf("\n"); printf(“%d “, number); } number++; printf("\n"); } 38. Write a C program to print the following shape of the star[use nested error] 43.Write a C program to print the following shape of the # include <stdio.h> Sample Input Sample Output
int main() { Enter a number: 3 * star[use nested
*** Sample Input Sample Output int rows; ***** error] printf("Enter a number: "); for (int i = 1; i <= n; i+ scanf("%d", &rows); +) { 5 $@@@@ # $ @@@ # # $@@ for (int i = 1; i <= rows; i++) { # # # $ @ for (int j = 1; j <= n; for (int j = 1; j <= rows - 1; j++) { # # # # $ j++) { printf(" ");} } for(int j =1; j <= 2*i -1; j++){ if (j == i) { printf(“*”);} printf(“\n”); for (int i = 1; i <= rows; i--) { printf("$ "); for (int j = 1; j <= rows - 1; j++) { printf(" ");} } } else if (j > i) { for(int j =1; j <= 2*i -1; j++){ printf(“*”);} printf("@ "); printf(“\n”); } return 0;} } else { 39. Write a c-program to print the following shape. But in this program you can use only one loop . printf("# "); } #include <stdio.h> int main() { Input: 4 int n; } Output: printf("Enter a number: "); 1234 scanf("%d", &n); 1234 printf("\n"); for (int i = 1; i <= n; i++) { 1234 for (int j = 1; j <= n; j++) { 1234 printf("%d ", j); } } printf("\n");} printf("\n"); 1111 return 0;} for (int i = 1; i <= n; i++) { 2222 for (int j = 1; j <= n; j++) { 3333 printf("%d ", i); 4444 } printf("\n"); } return 0;} 40. Write a c-program to print the following shape. But in this program you can use only one loop . #include <stdio.h> int main() { Input: 4 int n; Output: printf("Enter a number: "); 1 scanf("%d", &n); 12 for (int i = 1; i <= n; i++) { 123 for (int j = 1; j <= i; j++) { 1234 printf("%d ", j); } printf("\n"); } return 0;} 41. Write a c-program to print the following shape. But in this program you can use only one loop . #include <stdio.h> int main() { Input: 4 int n; Output: printf("Enter a number: "); 1234 scanf("%d", &n); 123 for (int i = n; i >= 1; i--) { 12 for (int j = 1; j <= i; j++) { 1 printf("%d ", j); } printf("\n"); } return 0;}