C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
instead i have one more effective one logic i suggest the code like
for finding perfect number we are saying that given number is strong if the sum of the factorials of its digit = number
so i have suggested a code if you find suitable
//while(num!=0) { r=num%10; t=factorial(r); sum+=t; num=num/10; } after this inside this loop function call int factorial(int r) { if(r==0||r==1) return 1; else return r*factorial(r-1); } // bcoz lets take an eg. 145 while(145){ r=num%10 // 145%10=5 t=factorial(5);//on fucn call recursively factorial function recurisvely solves it to 5!=120 then sum=sum+t//sum=0+120=120 so now sum becomes 120 then num=num/10=145/10=14 now again loop starts while(14) and finally you see answer comes out to be 145 and this equals the given number so if anyone feel about the complexity of my code please suggest me over this
: 1. Begin 2. Repeat steps 3 through 9 for 1000 times 3. Accept values of UserID, UserName, LastMonthMeterReading, CurrentMonthMeterReading and store in respective variables 4. Display user types as below to user and accept his/her choice and store in UserType: 3.1 1 = BPL Home 3.2 2 = APL Home 3.3 3 = Commercial Small 3.4 4 = Commercial Large 5. Assign FixedCharge based on the foll. conditions: 5.1 If UserType = 1, FixedCharge = 100 5.2 If UserType = 2, FixedCharge = 120 5.3 If UserType = 3, FixedCharge = 200 5.4 If UserType = 4, FixedCharge = 100 5.5 If UserType is anything else, print error message and exit 6. If LastMonthMeterReading < CurrentMonthMeterReading, goto step 6 --- else print an error message and exit 7. Assign difference of CurrentMonthMeterReading and LastMonthMeterReading to UnitsConsumed 8. Calculate the net amount for the month and store it as shown:
1.5 Psuedo code to introduce different types of user with different slabs TASK LIST: READ user id,user name,unit consumed,user type is userid=set of characters and numbers? yes:validate user name no:exit is user name=set of characters? yes:validate last month meter reading no:exit is last month meter reading =number? yes:validate current month meter reading no:exit is current month meter reading=number? yes:validate user type no:exit is user type=set of characters? yes:calculate the unit consumed no:exit CREATE function bplhome(int fixed charge,int unit consumed,float net amount) { fixed charge=100 if(unit consumed is<=100) net amount=(unit consumed*0.75)+fixed charge else if(unit consumed is >100&&<=150) net amount=(unit consumed*0.75+fixed charge)+(unitconsumed-100)*1.25+fixed charge else if(unit consumed is >150) net amount=(unit consumed*0.75+fixed charge)+(unitconsumed-100)*1.25+fixed charge (unit consumed-50)*2.00)+fixes charge
} end function CREATE function aplhome(int fixed charge,int unit consumed,float net amount) { fixed charge=120 if(unit consumed is <=100) net amount=(unit consumed*0.95)+fixed charge else if(unit consumed is >100&&<=150) net amount=(unit consumed*0.95+fixed charge)+(unitconsumed-100)*1.50+fixed charge else if(unit consumed is >150) net amount=(unit consumed*0.95+fixed charge)+(unitconsumed-100)*1.50+fixed charge (unit consumed-50)*2.00)+fixes charge } end function CREATE function commercial small(int fixed charge,int unit consumed,float net amount) {fixed charge=200 if(unit consumed is <=100) net amount=(unit consumed*1.25)+fixed charge else if(unit consumed is >100&&<=150) net amount=(unit consumed*1.25+fixed charge)+(unitconsumed-100)*2.00+fixed charge else if(unit consumed is >150) net amount=(unit consumed*1.25+fixed charge)+(unitconsumed-100)*2.00+fixed charge (unit consumed-50)*4.00)+fixes charge } end function CREATE function commercilal large(int fixed charge,int unit consumed,float net amount) { fixed charge=500 if(unit consumed is <=100) net amount=(unit consumed*2.50)+fixed charge else if(unit consumed is >100&&<=150) net amount=(unit consumed*2.50+fixed charge)+(unitconsumed-100)*2.75+fixed charge else if(unit consumed is >150) net amount=(unit consumed*2.50+fixed charge)+(unitconsumed-100)*2.75+fixed charge (unit consumed-50)*5.00)+fixes charge } end function
PSUEDO CODE net amount=0,unit consumed=0,fixed charge=0,net amount unit consumed=current month meter reading-last month meter reading if(user type= bpl home) { call function bpl home } endif else if(user type= apl home) { call function apl home } endif else if(user type=commercial small) { call function commercial small } endif else if(user type=commercial large) { call function commercila large } endif WRITE user id: user name: unit consumed: net amount:
13 comments:
could u please explain with giving an example
strong number like 145=1!+4!+5!=1+24+120=145(individuals digits factorials is equal to number.
use two loop(outer loop) that would extract reminder(individual digit) second loop for calculating factorials
while(num>0)
{
r=num%10; 145%10=5;
while(i<=5) 1<=5
f=f*i f=1*1
i++ i=2
as loop complete f=120
sum=sum+f (sum=120 in first time)
num=num/10;
}
At end iteration sum is 145 equal to original num so it is strong number
could u give d definition of strong number............. plz........
/*the below program holds good for any no. of digits*/
#include
int main()
{
int m,t,i=1,s=0,n,r,j=1,k,fact;
printf("enter the no: ");
scanf("%d", &n);
m=t=n;
while(j!=0)
{
r=n%10;
if(n<10)
{
break;
}
n=n/10;
j++;
}
printf("no of digits=%d\n",j);
while(i<=j)
{
r=m%10;
k=1,fact=1;
while(k<=r)
{
fact=fact*k;
k++;
}
printf("%d\n",fact);
m=m/10;
s=s+fact;
i++;
}
printf("sum=%d\n",s);
if(s==t)
{
printf("valid");
}
else
{
printf("invalid");
}
getch();
return 0;
}
can anyone explain about how to find out strong number in a given range
i have my own logic that i can show you
instead i have one more effective one logic i suggest the code like
for finding perfect number we are saying that given number is strong if the sum of the factorials of its digit = number
so i have suggested a code if you find suitable
//while(num!=0)
{ r=num%10;
t=factorial(r);
sum+=t;
num=num/10;
}
after this inside this loop function call
int factorial(int r)
{ if(r==0||r==1)
return 1;
else
return r*factorial(r-1);
}
//
bcoz lets take an eg.
145
while(145){
r=num%10 // 145%10=5
t=factorial(5);//on fucn call recursively factorial function recurisvely solves it to 5!=120
then
sum=sum+t//sum=0+120=120 so now sum becomes 120
then num=num/10=145/10=14
now again loop starts
while(14)
and finally you see answer comes out to be 145 and this equals the given number
so if anyone feel about the complexity of my code please suggest me over this
strong no. is that what adding the factorial of a number u will get the same no. for example(take a no. 145 1!+4!+5!=145)so 145 is the strong number.
:
1. Begin
2. Repeat steps 3 through 9 for 1000 times
3. Accept values of UserID, UserName, LastMonthMeterReading, CurrentMonthMeterReading and store in respective variables
4. Display user types as below to user and accept his/her choice and store in UserType:
3.1 1 = BPL Home
3.2 2 = APL Home
3.3 3 = Commercial Small
3.4 4 = Commercial Large
5. Assign FixedCharge based on the foll. conditions:
5.1 If UserType = 1, FixedCharge = 100
5.2 If UserType = 2, FixedCharge = 120
5.3 If UserType = 3, FixedCharge = 200
5.4 If UserType = 4, FixedCharge = 100
5.5 If UserType is anything else, print error message and exit
6. If LastMonthMeterReading < CurrentMonthMeterReading, goto step 6 --- else print an error message and exit
7. Assign difference of CurrentMonthMeterReading and LastMonthMeterReading to UnitsConsumed
8. Calculate the net amount for the month and store it as shown:
8.1 If UserType = 1:
8.1.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * .75 + FixedCharge
8.1.2 If 100 < UnitsConsumed <=150, NetAmount = 75 + ((UnitsConsumed-100) * 1.25) + FixedCharge
8.1.3 If UnitsConsumed > 150, NetAmount = 75 + 62.5 + ((UnitsConsumed-150) * 2) + FixedCharge
8.2 If UserType = 2:
8.2.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * .95 + FixedCharge
8.2.2 If 100 < UnitsConsumed <=150, NetAmount = 95 + ((UnitsConsumed-100) * 1.5) + FixedCharge
8.2.3 If UnitsConsumed > 150, NetAmount = 95 + 75 + ((UnitsConsumed-150) * 2) + FixedCharge
8.3 If UserType = 3:
8.3.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * 1.25 + FixedCharge
8.3.2 If 100 < UnitsConsumed <=150, NetAmount = 125 + ((UnitsConsumed-100) * 2) + FixedCharge
8.3.3 If UnitsConsumed > 150, NetAmount = 125 + 100 + ((UnitsConsumed-150) * 4) + FixedCharge
8.4 If UserType = 4:
8.4.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * 2.5+ FixedCharge
8.4.2 If 100 < UnitsConsumed <=150, NetAmount = 250 + ((UnitsConsumed-100) * 2.75) + FixedCharge
8.4.3 If UnitsConsumed > 150, NetAmount = 250 + 137.5 + ((UnitsConsumed-150) * 5) + FixedCharge
9. Print the values in format as shown below:
User ID:
User Name:
User Type:
Units Consumed:
Net Amount:
10. End program
1.5 Psuedo code to introduce different types of user with different slabs
TASK LIST:
READ user id,user name,unit consumed,user type
is userid=set of characters and numbers?
yes:validate user name
no:exit
is user name=set of characters?
yes:validate last month meter reading
no:exit
is last month meter reading =number?
yes:validate current month meter reading
no:exit
is current month meter reading=number?
yes:validate user type
no:exit
is user type=set of characters?
yes:calculate the unit consumed
no:exit
CREATE function bplhome(int fixed charge,int unit consumed,float net amount)
{
fixed charge=100
if(unit consumed is<=100)
net amount=(unit consumed*0.75)+fixed charge
else
if(unit consumed is >100&&<=150)
net amount=(unit consumed*0.75+fixed charge)+(unitconsumed-100)*1.25+fixed charge
else
if(unit consumed is >150)
net amount=(unit consumed*0.75+fixed charge)+(unitconsumed-100)*1.25+fixed charge
(unit consumed-50)*2.00)+fixes charge
}
end function
CREATE function aplhome(int fixed charge,int unit consumed,float net amount)
{
fixed charge=120
if(unit consumed is <=100)
net amount=(unit consumed*0.95)+fixed charge
else
if(unit consumed is >100&&<=150)
net amount=(unit consumed*0.95+fixed charge)+(unitconsumed-100)*1.50+fixed charge
else
if(unit consumed is >150)
net amount=(unit consumed*0.95+fixed charge)+(unitconsumed-100)*1.50+fixed charge
(unit consumed-50)*2.00)+fixes charge
}
end function
CREATE function commercial small(int fixed charge,int unit consumed,float net amount)
{fixed charge=200
if(unit consumed is <=100)
net amount=(unit consumed*1.25)+fixed charge
else
if(unit consumed is >100&&<=150)
net amount=(unit consumed*1.25+fixed charge)+(unitconsumed-100)*2.00+fixed charge
else
if(unit consumed is >150)
net amount=(unit consumed*1.25+fixed charge)+(unitconsumed-100)*2.00+fixed charge
(unit consumed-50)*4.00)+fixes charge
}
end function
CREATE function commercilal large(int fixed charge,int unit consumed,float net amount)
{
fixed charge=500
if(unit consumed is <=100)
net amount=(unit consumed*2.50)+fixed charge
else
if(unit consumed is >100&&<=150)
net amount=(unit consumed*2.50+fixed charge)+(unitconsumed-100)*2.75+fixed charge
else
if(unit consumed is >150)
net amount=(unit consumed*2.50+fixed charge)+(unitconsumed-100)*2.75+fixed charge
(unit consumed-50)*5.00)+fixes charge
}
end function
PSUEDO CODE
net amount=0,unit consumed=0,fixed charge=0,net amount
unit consumed=current month meter reading-last month meter reading
if(user type= bpl home)
{
call function bpl home
}
endif
else
if(user type= apl home)
{
call function apl home
}
endif
else
if(user type=commercial small)
{
call function commercial small
}
endif
else
if(user type=commercial large)
{
call function commercila large
}
endif
WRITE
user id:
user name:
unit consumed:
net amount:
READ UserId,UserName,LastMonthMeterReading,CurrentMonthReading
Enter valid UserId
FixedCharge=100
Lable 1:READ UserTtyoe
IF usertype is not string
WRITE type valid usertype
goto lable 1
else
Lable 2:READ username
IF username is not valid
WRITE valid username
goto lable 2
else
Lable 3 READ lastmonth readind
IFlastmonthreading not integer
WRITE enter valid lastmonthreading
goto lable 3
else
lable 4 READ currentmonthreading
IF currentmonthreading is notb integer
WRITE enter valid currentmonthreading
goto lable 4
else
UnitConsumed=CurrentReading-LastMonthReading
NetAmount=UnitConsumed*1.15+FixedCharge
WRITE UserID
WRITE UserName
WRITE UnitConsumed
WRITE NetAmount
main()
{
int n,q;
int r;
int sum=0;
int fact(int);
scanf("%d",&n);
q=n;
while(q!=0)
{
r=q%10;
sum=sum+fact(r);
q=q/10;
}
if(sum==n)
printf("yes no is strong=%d",n);
else
printf("no it is not strong=%d",n);
}
int fact(int x)
{
int y;
if(x==1)
return(1);
else
y=x*fact(x-1);
return(y);
}
main()
{
int n,q;
int r;
int sum=0;
int fact(int);
scanf("%d",&n);
q=n;
while(q!=0)
{
r=q%10;
sum=sum+fact(r);
q=q/10;
}
if(sum==n)
printf("yes no is strong=%d",n);
else
printf("no it is not strong=%d",n);
}
int fact(int x)
{
int y;
if(x==1)
return(1);
else
y=x*fact(x-1);
return(y);
}
Post a Comment