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

Cse115 Lab Manual 11 Nested - Loop - Part3

Uploaded by

tasin.nextlab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Cse115 Lab Manual 11 Nested - Loop - Part3

Uploaded by

tasin.nextlab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CSE 115 Lab on nested loop (part3) – Ara2

1. C program to print all perfect numbers between 1 to n:


#include <stdio.h>

void main()
{
int i, j, n, sum = 0;

printf("Enter any number to print perfect number up to: ");


scanf("%d", &n);

printf("\nAll Perfect numbers between 1 to %d:\n", n);


//Iterates from 1 to n and print if it is perfect number
for(i=1; i<=n; i++)
{
sum = 0;

// print i if the current value of i is a Perfect number


for(j=1; j<i; j++)
{
if(i%j==0)//if j is a divisor of i then add j with sum
{
sum += j;
}
}
//now sum = (sum of all proper divisors of i)
if(sum == i) // If the current value of i is Perfect
printf("%d, ", i);
}
}//main

2. C program to print all prime numbers between 1 and n .

#include<stdio.h> for(i = 2; i <= N; i++){


#include<conio.h> isPrime = 0;
// Check if i is prime
int main(){ for(j = 2; j <= i/2; j++){
if(i % j == 0){
int N, i, j, isPrime, n; isPrime = 1;
break;
printf("Enter the value of N\n"); }
scanf("%d",&N); }

/* For each number between 2 to N, check if(isPrime==0)


if it is prime number or not */ printf("%d ",i);
printf("Prime no. from %d to %d", 1, N); }
}
3. Write a program that prints first n prime numbers (n is input). E.g. for n = 5 it should print: 2,3,5,7,11,
#include<stdio.h>

void main()
{
int n, i = 2, count=0, j, isPrime;

printf("Enter n: ");
scanf("%d",&n);

printf("First %d prime numbers: ", n);


while (count < n)
{
//if current value of i is a prime no., then print it
isPrime = 1; //let the current value of i is a prime no.
for ( j = 2 ; j <= i/2; j++ )
{
if ( i%j == 0 ){ //if i has a divisor then i isn’t prime
isPrime = 0; //so assign 0 to isPrime to indicate this
break;
}
}//for
if (isPrime)
{
printf("%d, ",i); //move this outside while loop to print n-th prime
count++;
}
i++;
}//while
}//main

Exercise:
1. Write a C program to print all prime numbers between 1 and n in reverse order (n is an input).
Sample input/output:
Enter n: 20
All prime numbers between 1 and 20 (in reverse order):19, 17, 13, 11, 7, 5, 3, 2,
2. Write a C program to compute and print the sum of all prime numbers between m and n (m, n are inputs)
3. Write a C program to print the first n perfect numbers where n is an input.
4. Write a C program to compute and print the sum of first n perfect numbers.
5. Write a C program to print the n-th perfect number where n is an input.

Assignment:
1. Write a C program to print all palindrome numbers between m and n (m, n are inputs). For e.g. 121 is a
palindrome since the reverse of 121 = 121; but 152 is not a palindrome.
2. Write a C program to compute and print the sum of palindrome numbers between m and n
3. Write a C program to print the first n palindrome numbers where n is an input.

You might also like