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

Loop Assignment Solve

Uploaded by

fuadsadik39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Loop Assignment Solve

Uploaded by

fuadsadik39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.Print the series 1 2 3 4 5 …. N Where N will be given through keyboard.

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

3.Print all odd numbers among 1 and N in ascending order.


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

4.Print all even numbers among 1 and N in descending order.


#include <stdio.h>
main() {
int N, i;
printf("Enter the value of N: ");
scanf("%d", &N);
for(i = N; i >= 1; i--) {
if(i % 2 == 0) {
printf("%d ", i);
}
}
return 0;
}
5.Write a C program to find the number of and sum of all integers greater than 50 and less
than 300 that are divisible by 9.
#include <stdio.h>
main() {
int count = 0, sum = 0;
for (int i = 51; i < 300; i++) {
if (i % 9 == 0) {
count++;
sum += i;
}
}
printf("Number of integers divisible by 9 between 50 and 300: %d\n", count);
printf("Sum of integers divisible by 9 between 50 and 300: %d\n", sum);
return 0;
}
6. Write a C program to find the number of and sum of all integers greater than 100 and
less than 200 that are divisible by 7.
#include <stdio.h>
int main() {
int count = 0, sum = 0;
for (int i = 101; i < 200; i++) {
if (i % 7 == 0) {
count++;
sum += i;
}
}
printf("Number of integers divisible by 7 between 100 and 200: %d\n", count);
printf("Sum of these integers: %d\n", sum);
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;
}

9. Print the following half pyramid.


1
12
123
1234
12345
…………
……………
1 2 3 4 5 6 7…N
Where N will come from keyboard.
#include <stdio.h>
main() {
int N;
printf("Enter the value of N: ");
scanf("%d",&N);
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

10. Test a number whether it is prime number or non-prime number.


#include <stdio.h>
main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d",&num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
11. Write the Fibonacci series up to N Where N will be given through the key board
#include <stdio.h>
main() {
int n, i;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d", t1, t2);
for (i = 3; i <= n; ++i) {
printf(", %d", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
printf("\n");
return 0;
}

12. Print the following full pyramid.


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
……………………
………….……………
1 2 3 4 5 6 7……N
Where N will come from keyboard.
#include<stdio.h>

main(){

int row, col, n;

printf("Enter the value of N: ");

scanf("%d", &n);
for(row = 1; row <= n; row++){

for(col = 1; col <= n - row; col++){

printf(" ");

for(col = 1; col <= 2 * row - 1; col++){

printf("%d", col);

printf("\n");

return 0;

13 After receiving the value of N the output will be as follows.


1 1=1
2 1+2=3
3 1+2+3=6
4 1 + 2 + 3 + 4 = 10
.
.
.
N 1 +2 +3 +4 +5+ ……+ N = 1 x
N

#include <stdio.h>

int main() {

int N,sum=0;

printf("Enter the value of N: ");

scanf("%d", &N);

for (int i = 1; i <= N; i++) {

sum += i;

printf("%d: ", i);

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

if (j == i) {

printf("%d = %d\n", j, sum);


} else {

printf("%d + ", j);

return 0;

14 After receiving the value of N the output will be as follows.


Value of N Corresponding output
1 1 =1
2 1*2=2
3 1*2*3=6
4 1 * 2 * 3 * 4 = 24
.
.
.
N 1 *2 *3 *4 *5* ……* N = 1 x
N

#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", &degree);
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;
}

16. Calculate the value of the series of e x , e − x , log(1 + x) , log(1 − x)


#include <stdio.h>
#include <math.h>
int main() {
double x;
printf("Enter the value of x: ");
scanf("%lf", &x);
double exp_x = exp(x);
double exp_neg_x = exp(-x);
double log_1_plus_x = log(1 + x);
double log_1_minus_x = log(1 - x);
printf("e^x = %lf\n", exp_x);
printf("e^(-x) = %lf\n", exp_neg_x);
printf("log(1 + x) = %lf\n", log_1_plus_x);
printf("log(1 - x) = %lf\n", log_1_minus_x);
return 0;
}
17. Find the all the prime numbers in number N Where N will be given through key board.

Value of N Corresponding output


20 1 2 3 5 7 11 13 17
50 1 2 3 5 7 11 13 17 23 29 31 37 41 43 47
.
N 1 2 3 5 7 11 13 17 23 29 31 37 41 43 47 ……. N

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

18 Write a program that can reverse an integer number.


Value of N Output
23 32
123 321
489 984

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

You might also like