General code 4
CODE NO:- 19
Q)A prime number is a whole number greater than 1, whose only two whole-number factors
are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11 and 13.
Given two integers, print the sum of all prime numbers between n1 and n2 (both inclusive).
Input
The first line of the input contains an integer t, number of test cases next t lines follow with
each line containing two integers n1 and n2
Output
For each test case print the answer in a new line. Example
Input:
Q)A prime number is a whole number greater than 1, whose only two whole-number factors
are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11 and 13.
Given two integers, print the sum of all prime numbers between n1 and n2 (both inclusive).
Input
The first line of the input contains an integer t, number of test cases next t lines follow with
each line containing two integers n1 and n2
Output
For each test case print the answer in a new line. Example
Input:
2
1 6
6 6
Output: 10
0
Test Cases:
---------------
1. Valid Input:
a) Only integer will be given as input. Constraints:
1 < t <= 10
0 < n1 <= n2 <= 100
2. Invalid inputs:
a) Negative number (t, n1, or n2)
b) Fraction
c) String
3. You should generate output as follows:
a) For right output print just the sum to STDOUT without any other text.
b) If any error: print 'ERROR' to the STDOUT without any other text.
#Program( In C):
In C language we use ' // ' for Single line comment and /*---*/ for multi line comment
#include<stdio.h> // header file for i/p and o/p
#include<stdio.h> // header file for i/p and o/p
#include<stdlib.h> // for exit()
#include<math.h>// for sqrt() func
int t=0;// global variable
int main(int argc, char* argv[])
{
int i,j,f,sum=0,lb,ub;
for(j=0;argv[1][j]!='\0';j++) // loop for checking invalid cases for test case value(t)
{
if(argv[1][j]=='/')
{
printf("\nt: fraction value are not accepted");
exit(0);
}
if(argv[1][j]=='-')
{
printf("\n t:negative value are not accepted");
exit(0);
}
if((argv[1][j]>=65 && argv[1][j]<=90)||(argv[1][j]>=97 && argv[1][j]<=122))
{
printf("\nt:string value are not accepted");
exit(0);
}
}
for(j=0;argv[2][j]!='\0';j++) // loop for checking invalid cases for lower bound
{
if(argv[2][j]=='/')
{
printf("\n lb:fraction value are not accepted");
exit(0);
}
if(argv[2][j]=='-')
{
printf("\n lb:negative value are not accepted");
exit(0);
}
if((argv[2][j]>=65 && argv[2][j]<=90)||(argv[2][j]>=97 && argv[2][j]<=122))
{
printf("\nlb:string value are not accepted");
exit(0);
}
}
for(j=0;argv[3][j]!='\0';j++) //loop for checking invalid cases for upper bound
{
if(argv[3][j]=='/')
{
printf("\n ub:fraction value are not accepted");
exit(0);
}
if(argv[3][j]=='-')
{
printf("\n ub:negative value are not accepted");
exit(0);
}
if((argv[3][j]>=65 && argv[3][j]<=90)||(argv[3][j]>=97 && argv[3][j]<=122))
{
printf("\nub:string value are not accepted");
exit(0);
}
}
if(argc == 4)
t = atoi(argv[1]);
argc=5;
//atoi means ascii to integer
lb=atoi(argv[2]), ub=atoi(argv[3]);// lb=lower bound , ub=upper bound
printf("t=%d lb=%d ub=%d\n", t, lb, ub);
printf("prime no. within the range are:");
for(i=lb;i<=ub;i++)
{
f=0;
for(j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{
f=1;
break;
}
}
if(f==0)
{
printf("%d ",i);
sum+=i;
}
}
if(lb==1)
printf("\n sum of prime no. b/w %d and %d is:%d",lb,ub,sum-1);
//we are subtracting 1 from sum because 1 is not a prime no.
else
printf("\n sum of prime no. b/w %d and %d is:%d",lb,ub,sum);
t--;
if(t == 0) // end of test case
return(0);
printf("\n./Test ");// if ./Test appears then enter next pair of prime number
scanf("%s %s", argv[2], argv[3]); // accepting next pair of prime number
main(argc, argv);// calling main()
return(0);
}
We got this output by using DEV C++ (IDE)
Baap re
ReplyDelete😎😎
DeleteWell explained
ReplyDeleteThanks
DeleteVery nice
ReplyDeleteGood work
ReplyDeleteGreat knowledge is given here
ReplyDeleteVery good
ReplyDeleteGood nice work
ReplyDelete