Encode printing of alphabets in C - c

In this program i have encoded
Using the numerical value of each letter (A=1, B=2, ... Z= 26) the rules are as follows:
A–E Multiply its numerical value by 2
F–J Divide its numerical value by 3. Multiply the integer remainder by 5
K–O Divide its numerical value by 4. Multiply the integer remainder by 8.
P–T Add 10.
U- Z Find the largest integer factor of its numerical value less than the value itself. Multiply it by 12.
As an example if the letter to encode is a B, the B has a numerical value of 2 and encodes to a 4 and becomes a D, the 4th letter of the alphabet.
The G has a numerical value of 7. It encodes to a 5 and becomes an E.
The numerical value of Z is 26. Its largest factor is 13. You must count 156 (13*12) letters. This has the effect of wrapping around the alphabet 6 complete times and ending at Z. If a numerical value of zero is evaluated print a # symbol.
Problem with the code
- I am not able to print #.
- whenever i am entering Y i am getting J why???????
Here is my code:
#include<stdio.h>
void main()
{
char c;
int n,i,max=0,j,y;
int a[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
scanf("%c",&c);
n=c;
for(i=0;i<5;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[((i+1)*2)-1]);
break;
}
}
for(i=5;i<10;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[(((i+1)%3)*5)-1]);
break;
}
}
for(i=10;i<15;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[(((i+1)%4)*8)-1]);
break;
}
}
for(i=15;i<20;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[((i+1)+10)-1]);
break;
}
}
for(i=15;i<26;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
for(j=1;j<=(i+1)/2;j++)
{
if((i+1)%j==0)
{
if(max<j)
{
max=j;
}
}
}
}
if(flag==1)
{
y=(max*12)/6;
if(y>=0)
{
printf("%c",a[y-1]);
}
else
{
printf("#");
}
break;
}
}
}
Help me out guy's.

You have a variety of rules for encoding the characters, but at the end of the day, each letter of the alphabet maps to a single output character. So you can simply create a table that contains the outputs that correspond to each letter. Thus the code reduces to the following
int main( void )
{
char table[] = "BDFHJ#EJ#EX#HPXZABCDFBLJHZ";
char c;
if ( scanf("%c",&c) == 1 && c >= 'A' && c <= 'Z' )
printf( "%c\n", table[c - 'A'] );
}
Note that the table in this code attempts to follow the rules that you specified. Those rules don’t create a 1-to-1 mapping of input to output, but of course it’s trivial to change the table to achieve any mapping you like.

#include<stdio.h>
int large(int n);
int main()
{
char c;
int n,y;
scanf("%c",&c);
n=c;
if(n>=65 && n<70)
{
int val;
val=(((n-65)+1)*2)+64;
printf("%c\n",val);
}
else if(n>=70 && n<75)
{
int val=(n-65+1);
if((val)%3!=0)
{
printf("%c\n",(((val)%3)*5)+64);
}
else printf("#\n");
}
else if(n>=75 && n<80)
{
int val=(n-65+1);
if((val)%4!=0)
{
printf("%c\n",(((val)%4)*8)+64);
}
else
printf("#\n");
}
else if(n>=80 && n<85)
{
int val=n-65+1;
if(val>16)
{
val=val-26;
printf("%c",val+10+64);
}
else
printf("%c\n",(val+10)+64);
}
if(n>=85 && n<91)
{
int d,b=0,m;
int val=n-65+1;
d=large(val);
y=(d*12);
while(b<y)
{
b=b+26;
}
b=b-26;
m=y-b;
printf("%c\n",m+64);
}
return 0;
}
int large(int n)
{
int j,max=0;
for(j=1;j<=n/2;j++)
{
if(n%j==0)
{
if(max<j)
{
max=j;
}
}
}
return max;
}

Try this code:
#include<stdio.h>
int maxFac(int n)
{
int i,max=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
if(max<i)
max=i;
}
}
return max;
}
int main()
{
char c;
scanf("%c",&c);
if((int)c>=65 && (int)c<=69)
{
c=(((int)c-64)*2)+64;
}
else if ((int)c>=70 && (int)c<=74)
{
c= (((int)c-64)%3)*5;
if(c==0)
c='#';
else
c=(int)c+64;
}
else if((int)c>=75 && (int)c<=79)
{
c= (((int)c-64)%4)*8;
if(c==0)
c='#';
else
c=(int)c+64;
}
else if((int)c>=80 && (int)c<=84)
{
c= c+10;
if(c>90)
c=c-26;
}
else if((int)c>=85 && (int)c<=90)
{
int z;
c=c-64;
z=maxFac(c);
c=z;
c=(z*12)%26 + 64;
if(c=='#')
c=c+26;
}
printf("%c",c);
}

Related

Getting negative numbers or zero as output of factorial in C

I've written a very simple program which calculate the factorial of a number, the problem is that from 30 to 60 approximately, it returns a negative number and from 70 it returns 0.
I don't what I've done wrong. Could this problem depend on the computing power of my computer?
Here's the code:
#include <stdio.h>
int main(){
int x, i;
long long int f = 1;
printf("Insert a number:");
scanf("%d", &x);
if (x == 0){
printf("0! = 1");
}
else {
for (i = 1; i <= x; i++){
f *= i;
}
printf("%d! è = %lli", x, f);
}
return 0;
}
Here is my code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void Carry(int bit[],int pos)
{
int i,carray=0;
for(i=0;i<=pos;i++)
{
bit[i]+=carray;
if(bit[i]<=9)
{
carray=0;
}
else if(bit[i]>9&&i<pos)
{
carray=bit[i]/10;
bit[i]%=10;
}
else if(bit[i]>9&&i>=pos)
{
while(bit[i]>9)
{
carray=bit[i]/10;
bit[i]%=10;
i++;
bit[i]=carray;
}
}
}
}
int main()
{
int num,pos,digit,i,j,m,n;
double sum=0;
int *fact;
printf("input want to calculute factorial num:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum+=log10(i);
}
digit=(int)sum+1;
if(!(fact=(int *)malloc((digit+1)*sizeof(int))))
{
printf("malloc failed\n");
return 0;
}
for(i=0;i<=digit;i++)
{
fact[i]=0;
}
fact[0]=1;
for(i=2;i<=num;i++)
{
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
for(j=0;j<=pos;j++)
{
fact[j]*=i;
}
Carry(fact,pos);
}
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
m=0;
n=0;
for(i=pos;i>=0;i--)
{
printf("%d",fact[i]);
m++;
if(m%4==0)
{
printf(" ");
}
if(m==40)
{
printf("\n");
m=0;
n++;
if(n==10)
{
printf("\n");
n=0;
}
}
}
printf("\n\n");
return 0;
}

I am not able to complete this loop perfectly

write a program to enter five marks and by taking its average percent to determine the grade(only using nested if else) by following conditions:
if 90 and above: grade A
if 80-70: grade B
if 70-80: grade C
if 50-70: grade D
if below 50: fail
I did try the loop starting from backward by using if(avg>=50) but it did not give me the desired output because it will always be stuck at you failed which I kept it in else condition.
if ( avg <= 90 )
{
if ( avg <= 80 )
{
if ( avg <= 70 )
{
if ( avg <= 50 )
{
printf( "Fail" );
}
else
{
printf( "D" );
}
}
else
{
printf( "C" );
}
}
else
{
printf( "B" );
}
}
else
{
printf( "A" );
}
If I enter marks above 90 then I expect the output to come A but instead it comes you have "failed"
You should try using else if ladder
if(avg>=90)
{
printf("A");
}
else if(avg>=80)
{
printf("B");
}
else if(avg>=70)
{
printf("C");
}
else if(avg>=50)
{
printf("D");
}
else{
printf("Fail");
}
This should work:
int main()
{
int marks[5];
int i,j;
float avg;
int total=0;
for(i=0;i<5;i++)
{
scanf("%d",&marks[i]);
}
for(j=0;j<5;j++)
{
total = total+marks[j];
}
avg = total/5;
if(avg<50)
{
printf("Fail");
}
else
{
if(avg>=50 &&avg<70)
{
printf("C");
}
else if(avg>=70)
{
if(avg>=80)
{
if(avg<90)
{
printf("B");
}
else if (avg>=90)
{
printf("A");
}
}
}
}
}
In your case, I think switch statement may be the best solution.
# include <stdio.h>
const char * mark(int grade){
switch (grade/10){
case 10:
case 9:
return "A";
case 8:
return "B";
case 7:
case 6:
case 5:
return "C";
default:
return "Fail";
}
}
int main(int argc, char* argv[]){
int grade;
for(int i = 1; i < argc; ++i){
if (sscanf(argv[i], "%d", &grade) < 1){
fprintf(stderr, "\"%s\" can not be converted to an int.\n", argv[i]);
return -1;
}else{
printf("%s\n", mark(grade));
}
}
return 0;
}

Find specifi ID info using structure

I could not find specific student_Id info from a rage of given info.suppose, I am taking input from 1 to 100 all student info. now I want to find only 50th number student_ID all info.i could not do it. how it possible.here show my code. what's wrong with my code and how fixed it.thanks
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
struct student
{
char student_id[100];
char name[10];
int m[50],credit[100],sub[100];
int total,sumCredit;
float GP[100];
char result[5];
char grade[100][10];
double sumCreditxGP;
}*p,*s;
float gradesToGP(float marks);
char *markToG(float gp);
void lines(void);
void main()
{
int i,j,l,n,sub,k;
// clrscr();
printf("Enter the no. of students : ");
scanf("%d",&n);
p=(struct student*) malloc(n*sizeof(struct student));
//printf("%d",p);
//exit(0);
s=p;
for(i=0; i<n; i++)
{
printf("Enter a student id : ");
scanf("%s",&p->student_id);
printf("Enter a name : ");
scanf("%s",&p->name);
printf("Enter the no. of subject : ");
scanf("%d",&p->sub[i]);
p-> total=0;
p-> sumCredit=0;
p-> sumCreditxGP=0;
l=0;
for(j=0; j<p->sub[i]; j++)
{
one:
printf("Enter Marks of %d Subject\t%d : ",j+1,p->sub[i]);
scanf("%d",&p->m[j]);
printf("Enter Credit Hour: ");
scanf("%d",&p->credit[j]);
p->GP[j] = gradesToGP((float)p->m[j]);
strcpy(p->grade[j],markToG(p->m[j]));
if((p->m[j])>100)
{
printf("---------------Wrong Value Entered-----------\n");
goto one;
}
p->total+=p->m[j];
p->sumCredit+=p->credit[j];
p->sumCreditxGP+=p->credit[j]*p->GP[j];
if(p->m[j]<40)
l=1;
}
if(l==0)
strcpy(p->result,"PASS");
else
strcpy(p->result,"FAIL");
p++;
}
char search_id[10];
printf("Enter id to find specific student ");
scanf("%s",search_id);
//PROBLEM START HERE
for(i=0; i<n; i++)
{
if(p->student_id==search_id){
printf("found");
printf("%s",s->student_id);
}else{
printf("Not found");
}
s++;
}
getch();
}
float gradesToGP(float marks)
{
if (marks>=80 && marks<=100)
{
return(float)4.00;
}
else if (marks>=75 && marks<=79)
{
return(float)3.67;
}
else if (marks>=70 && marks<=74)
{
return(float)3.33;
}
else if (marks>=65 && marks<=69)
{
return(float)3.00;
}
else if (marks>=60 && marks<=64)
{
return(float)2.67;
}
else if (marks>=55 && marks<=59)
{
return(float)2.33;
}
else
{
return(float)5.00;
}
}
char *markToG(float marks)
{
if (marks>=80 && marks<=100)
{
return "A";
}
else if (marks>=75 && marks<=79)
{
return "A-";
}
else if (marks>=70 && marks<=74)
{
return "B+";
}
else if (marks>=65 && marks<=69)
{
return "B";
}
else if (marks>=60 && marks<=64)
{
return "B-";
}
else if (marks>=55 && marks<=59)
{
return "C+";
}
else
{
return "null";
}
}
void lines(void)
{ printf("**********************************************************************");
}
Please tell me how can I fixed it.thanks in advanced.
if(p->student_id==search_id){
printf("found");
Now, that's not how you compare strings in C. Use the strcmp() function for string comparison. You may read about strcmp() here.
The issue is your equality check: if(p->student_id==search_id)
Because both student_id and search_id are char arrays, the types will decay to pointers (char *) and this will never work as you expect. Instead, you need to use strcmp (or better, strncmp).
if(strncmp(p->student_id, search_id, 10) == 0) { /* equality */ }

A calculator for big numbers

i have to code a calculator that does adding or multiplication to numbers with maximum digits of 30 .(maximum number of operands i can enter is 20).
and this is what i came up with (it is long):
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void Adding(int Result[31],int temp[30][31],int cnt)
{
int remainder,i,j,ResultsA[60]={0};
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
ResultsA[j]=(ResultsA[j]+temp[i][j])%10;
remainder=(ResultsA[j]+temp[i][j])/10;
ResultsA[j+1] += remainder;
if(ResultsA[30])
{
cnt++;
}
}
}
for(i=0;i<30;i++)
{
Result[i]=ResultsA[i];
}
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
temp[i][j]=0;
}
}
return;
}
void Mul(int num,int Results[31],int Num_int[30][31],int cnt)
{
int remainder=0,i,j,rows,x;
for(i=0;i<1;i++)
{
Results[i]=1;
}
int temp[30][31]={0};
for(rows=0;rows<num;rows++)
{
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
temp[i][i+j]=remainder+(Num_int[rows][j]*Results[i])%10;
remainder=(Num_int[rows][j]*Results[i])/10;
}
}
Adding(Results,temp,cnt);
}
return;
}
void Plus(int num,int Nums_int[30][31],int Results[31],int cnt)
{
int i,j,remainder,len=0;
for(i=0;i<num;i++)
{
for(j=0;j<30;j++)
{
Results[j]=(Results[j]+Nums_int[i][j])%10;
remainder=(Results[j]+Nums_int[i][j])/10;
Results[j+1]+=remainder;
if(Results[30])
cnt++;
}
}
return;
}
void str_to_int(int Nums_int[20][31],char Nums_str[20][31])
{
int length[20]={0},i,j;
for(i=0;i<20;i++)
{
for(j=19;0<=j;j--)
{
while(Nums_str[i][j]<48||57<Nums_str[i][j])
length[i]++;
}
}
for(i=0;i<20;i++)
{
for(j=0;j<length[i];j++)
{
Nums_int[i][length[i]-j]=Nums_str[i][j]-'0';
}
}
return;
}
void calcBig(char op, int operands[][31], int numOperands, int result[31],int cnt)
{
if (op=='+')
{
Plus(numOperands,operands,result,cnt);
}
if(op=='*')
{
Mul(numOperands,result,operands,cnt);
}
return;
}
int main()
{
int i,cnt=0,y;
char x;
char Nums_str[20][31]={0};
int num, Nums_int[30][31]={0};
int Results[31]={0};
printf("select operator:\n");
scanf("%c",&x);
printf("enter number of operands:\n");
scanf("%d",&num);
printf("enter numbers <seperated by new line>:\n");
for(i=0;i<num;i++)
{
scanf("%s",Nums_str[i]);
}
str_to_int(Nums_int,Nums_str);
calcBig(x,Nums_int,num,Results,cnt);
printf("result:\n");
if(cnt)
{
printf("overflow!");
}
else{
for(i=29;0<=i;i--)
{
while(Results==0)
{
y++;
}
}
for(i=y;0<=i;i--)
{
printf("%d",&Results[i]);
}
}
return 0;
}
Num_int is the 2d array containing numbers from user , num is the number of numbers entered by user.
Mul is function for multiplying numbers in Nums_int.
str_to_int converts the number recieved from user from string to int and reverse the order.
Plus is a function to add numbers in Nums_int.
Adding is function to add numbers that are the result of multiplying a number by ever digit of the other numbers (it gives the result of multiplying two numbers).
the problem is that when i run the code i get this:
select the operation:
*/+
select number of operands:
num
enter numbers:
//entering numbers
and after entering the numbers nothing happens and the program doesnt even end (no return 0). simply nothing happens after entering the numbers

Prime Generator(spoj)

For exact question see this link
Here I defined three function, called them selves one within another.
Function call is not being done
#include<stdio.h>
int primegen(int x1,int x2);
int isprime(int j);
int main(){
int x,n1,n2,i;
printf("Enter the number of test cases:");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("enter the starting point and ending point:");
scanf("%d %d",&n1,&n2);
primegen(n1,n2);
}
return 0;
}
int primegen(int x1,int x2){
int k;
if(x2>x1){
for(k=x1;k<x2;k++){
if(isprime(k))
{
printf("%d",k);
}
}
return 0;
}
}
int isprime(int j){
int i,c=0;
for (i=1;i<=j;i++)
{
if(j%i==0){
c++;
}
if(c!=2){
return 0;
}
else{
return 1;
}
}
}
Output
There is no output for this code.
Take following outside loop:
if(c!=2){
return 0;
}
else{
return 1;
}
The problem is in your loop in isprime().
In this you are using the value of c when it is still 0. So, c!=2 would result in true and 0 will be returned and you would not get any answer.
Remove this particular statement from the for loop as you need to calculate total no. of divisors.
for (i=1;i<=j;i++)
{
if(j%i==0)
c++;
}
if(c!=2)
return 0;
else
return 1;
Or you can do like this:
int isprime(int j){
int i,k;
k=sqrt(j); //include math.h for this
for(i=2;i<=k;i++)
{
if(j%i==0)
return 1;
}
return 0;
}
You only need to find any divisor up to the square root of the number.
It's better to return 0 if it is found to be divisible instead of using counter and incrementing it.
int isprime(int j)
{
int i;
for(i=2;i<j;i++)
if((j%i)==0)
return 0;
return 1;
}
#include <stdio.h>
#include <math.h>
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
unsigned int low,high,i=0,j=2,k,x=0,y=0,z;
unsigned long int a[200000],b[200000];
scanf("%d",&low);
scanf("%d",&high);
for(i=low;i<=high;i++)
a[x++]=i;
for(i=2;i<=32000;i++)
b[y++]=i;
i=0;
while(b[i]*b[i]<=high)
{
if(b[i]!=0)
{
k=i;
for(;k<y;k+=j)
{
if(k!=i)
{
b[k]=0;
}
}
}
i+=1;j+=1;
}
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=low && b[i]<=sqrt(high)))
printf("%d\n",b[i]);
}
int c=0;
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=1 && b[i]<=sqrt(high)))
b[c++]=b[i];
}
int m=a[0];
for(i=0;i<c;i++)
{
z=(m/b[i])*b[i];k=z-m;
if(k!=0)
k += b[i];
for(;k<x;)
{
if(a[k]!=0)
{
a[k]=0;
}
k+=b[i];
}
}
for(i=0;i<x;i++)
{
if(a[i]!=0 && (a[i]>=2 && a[i]<=(high)))
printf("%d\n",a[i]);
}
printf("\n");
}
return 0;
}

Resources