Você está na página 1de 2

Q1:

Write a program that asks the user to type an integer N and that writes the number of prime numbers lesser or equal to N.
Solution
#include<iostream> using namespace std; int main() { int N,i,nb=0,d; bool is_prime; cout<<"Typer the value of N : ";cin>>N; for(i=2;i<=N;i++) { /* test if i is prime*/ is_prime=true; d=2; while(is_prime && d*d<=i) if(i%d==0)is_prime=false; else d++; if(is_prime==true) nb++; } cout<<"The number of prime number lesser or equal to " <<N<<" is "<<nb<<endl; return 0; }

Q2
Write a program that asks the user to type an integer N and computes the sum of the cubes from 53 to N3.
#include<iostream.h> void main() { int n=0,sum=0,cube=1,j,i; cout<<"Enter no. = "; cin>>n; for(i=5;i<=n;i++) { for(j=1;j<=3;j++) { cube=cube*i; }

Solution

sum=sum+cube; cube=1; } } cout<<"Sum of cubes from 5 to "<<n<<" = "<<sum<<endl;

Made by : Zaid Al-Ali Zeezou_tevez@yahoo.com

Você também pode gostar