Loop Assignment Solve
Loop Assignment Solve
#include <stdio.h>
main() {
int N, i;
printf("Enter the value of N: ");
scanf("%d", &N);
for(i = 1; i <= N; i++) {
printf("%d ", i);
}
return 0;
}
2. Print the series N, N-1, N-2, …..3, 2, 1 Where N will come through keyboard.
#include <stdio.h>
main() {
int N, i;
printf("Enter the value of N: ");
scanf("%d", &N);
for(i = N; i >= 1; i--) {
printf("%d ", i);
}
return 0;
}
7. Find out the factorial of a number N. Where N will come from key board.
#include <stdio.h>
int main() {
int N;
unsigned long long factorial = 1;
printf("Enter a integer: ");
scanf("%d",&N);
if (N < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
for (int i = 1; i <= N; i++) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", N, factorial);
}
return 0;
}
N
8. Find out the summation of the series 1
x Where N will come from keyboard.
#include <stdio.h>
main() {
int N, sum = 0;
printf("Enter a positive integer N: ");
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
sum += i;
}
printf("The summation of the series 1 + 2 + ... + %d is: %d\n", N, sum);
return 0;
}
main(){
scanf("%d", &n);
for(row = 1; row <= n; row++){
printf(" ");
printf("%d", col);
printf("\n");
return 0;
#include <stdio.h>
int main() {
int N,sum=0;
scanf("%d", &N);
sum += i;
if (j == i) {
return 0;
#include <stdio.h>
int main() {
int N;
long long product = 1;
printf("Enter the value of N: ");
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
product = 1;
printf("%d: ", i);
for (int j = 1; j <= i; j++) {
product *= j;
if (j == i) {
printf("%d ", j);
} else {
printf("%d * ", j);
}
}
printf("= %lld\n", product);
}
return 0;
}
15. Find out the value of sinx, cosx, tanx, cots, secx, cosecx Where the value of x is in radian
but you have to take the degree value from key board.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main() {
double degree, radian;
double sinx, cosx, tanx, cotx, secx, cosecx;
printf("Enter the value of angle in degrees: ");
scanf("%lf", °ree);
radian = degree * (PI / 180.0);
sinx = sin(radian);
cosx = cos(radian);
tanx = tan(radian);
if (tanx != 0) {
cotx = 1.0 / tanx;
} else {
cotx = INFINITY;
}
if (cosx != 0) {
secx = 1.0 / cosx;
} else {
secx = INFINITY;
}
if (sinx != 0) {
cosecx = 1.0 / sinx;
} else {
cosecx = INFINITY;
}
printf("sin(x) = %lf\n", sinx);
printf("cos(x) = %lf\n", cosx);
printf("tan(x) = %lf\n", tanx);
printf("cot(x) = %lf\n", cotx);
printf("sec(x) = %lf\n", secx);
printf("cosec(x) = %lf\n", cosecx);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
void findPrimes(int N) {
bool isPrime[N + 1];
for (int i = 0; i <= N; i++) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= N; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= N; j += i) {
isPrime[j] = false;
}
}
}
printf("Prime numbers up to %d are:\n", N);
for (int i = 1; i <= N; i++) {
if (isPrime[i]) {
printf("%d ", i);
}
}
printf("\n");
}
int main() {
int N;
printf("Enter the value of N: ");
scanf("%d", &N);
findPrimes(N);
return 0;
}
#include <stdio.h>
int main() {
int N, reversed = 0, digit;
printf("Enter an integer: ");
scanf("%d", &N);
while (N != 0) {
digit = N % 10;
reversed = reversed * 10 + digit;
N /= 10;
}
printf("Reversed integer: %d\n", reversed);
return 0;
}
19 Convert a decimal number into binary number
Value of N Output
5 101
6 110
7 111
#include <stdio.h>
int main() {
int N, binary[32];
int index = 0;
printf("Enter a decimal number: ");
scanf("%d", &N);
if (N < 0) {
printf("Please enter a non-negative integer.\n");
return 1;
}
while (N > 0) {
binary[index] = N % 2;
N = N / 2;
index++;
}
printf("Binary representation: ");
for (int i = index - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
printf("\n");
return 0;
}
20 Find the factors of an integer number.
Value of N Output
20 1 2 4 5 10 20
30 1 2 3 5 6 10 15
50 1 2 5 10 25 50
#include <stdio.h>
int main() {
int N;
printf("Enter an integer: ");
scanf("%d", &N);
if (N <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
printf("Factors of %d: ", N);
for (int i = 1; i <= N; i++) {
if (N % i == 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
21 Divide an integer number N into two parts n1 and n2 in such a way so that the n1
and n2 are themselves prime number and the sum of n1 and n2 is equal to N.
Value of N Output
20 (1, 19), (3, 17), (7, 13)
50 (3, 47), (7, 43), (13, 37)
60 (1, 59), (7, 53), (17, 43), (19, 41), (23, 37), (29, 31)
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int N;
printf("Enter an integer: ");
scanf("%d", &N);
if (N < 2) {
printf("Please enter an integer greater than or equal to 2.\n");
return 1;
}
printf("Pairs of prime numbers that sum to %d: ", N);
for (int n1 = 1; n1 <= N; n1++) {
int n2 = N - n1;
if (isPrime(n1) && isPrime(n2)) {
printf("(%d, %d) ", n1, n2);
}
}
printf("\n");
return 0;
}