I am not able to complete this loop perfectly - c

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;
}

Related

C program only running the first loop and then stopping

Every time I try and run only the first loop runs. It ask how many items were sold and then just stops running. I'm not really sure what I did wrong. If anyone has any tips that would be great(story if there's some formatting issues, SO wouldn't let me post the question without them).
Everything else looks fine but if any other improvements could be made please let me know.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
i++;
}
return val;
}
void error()
{
printf("Error: Sales figures must be numbers.\n");
printf("Please try again.\n");
}
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if( isdigit(term[i]) == 0)
{
return false;
i++;
}
}
return true;
}
int main()
{
int sales[3][2], costs[3] = {3, 4, 1}, weekends[2] = {0, 0};
int i, j, val;
char term[100];
while (1)
{
printf("Number of Bagel sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Saturday: ");
scanf("%s", term);
if (isnumber(term) == false)
{
error;
}
else
{
sales[2][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Bagel sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[2][1] = digit(term);
break;
}
}
for (i = 0; i < 2, i++;)
{
for (j = 0; j < 3, j++;)
{
weekends[i] += costs[j] * sales[i][j];
}
}
printf("\n");
for (i = 0; i < 3, i++;)
{
printf("%d", costs[i]);
}
printf(".");
for (i = 0; i < 3, i++;)
{
for (j = 0; j < 2, j++;)
{
printf("%d", sales[i][j]);
}
if (i == 0)
{
printf(" = ");
printf("%d %d", weekends[0], weekends[1]);
}
printf("\n ");
}
printf("\nTotal sales on Saturday: $%d", weekends[0]);
printf("\nTotal sales on Sunday: $%d", weekends[1]);
printf("\nTotal sales over the weekend: $%d", weekends[0] + weekends[1]);
return 0;
}
You have an infinite loop in isnumber(). It will return false if the first character is not a digit. But if the first character is a digit, it never increments i, so it keeps testing the first character repeatedly.
i++ should not be in the if statement.
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if(!isdigit(term[i]))
{
return false;
}
i++;
}
return true;
}
And as others have pointed out, you need to put () after error to call it.
As for your programming stopping; you use isdigit on line 30 without declaring it.
Along that, you use error everywhere without invoking it. Use error() to invoke the function and print the error-information.
Just a small formatting tip as well: instead of if (something == false) use if (!something)
Your use of error; is incorrect. You should write error(); instead.

Countries Grouping in c

People in a group are sitting in a group numbered 1 to N. It is known that people of same countries are sitting together.
Output is a single integer denoting no of distinct countries.
Input
4 (no of test cases)
2 (no of people in group)
1 1 ( in this there are 2 people from diff country)
2
1 3
7
1 1 2 2 3 3 3
7
7 7 7 7 7 7 7
Output should be
2
Invalid Data
4
1
My program:please tell me where is the error
#include<stdio.h>
#include<string.h>
int main()
{
int tcaseno,nopgrp,flag=0;
int arr[1000];
int count=0,i=0,j=0,t=0;
scanf("%d", &tcaseno);
t=tcaseno;
while(t>0)
{
scanf("%d\n", &nopgrp);
for (i = 0; i < nopgrp;i++)
{
scanf("%d", &arr[i]);
}
for (j = 0; j < nopgrp;j++)
{
if(arr[j]==1)
{
count++;
}
else if(arr[j]==2)
{
if(arr[j+1]==2)
{
count++;
}
else
{
flag=1;
}
}
else if(arr[j]==3)
{
if((arr[j+1]==3)&&(arr[j+2]==3))
{
count++;
}
else
{
flag=2;
}
}
else if(arr[j]==4)
{
if((arr[j+1]==4)&&(arr[j+2]==4)&&(arr[j+3]==4))
{
count++;
}
else
{
flag=3;
}
}
else if(arr[j]==5)
{
if((arr[j+1]==5)&&(arr[j+2]==5)&&(arr[j+3]==5)&&(arr[j+4]==5))
{
count++;
}
else
{
flag=4;
}
}
else if(arr[j]==6)
{
if((arr[j+1]==6)&&(arr[j+2]==6)&&(arr[j+3]==6)&&(arr[j+4]==6)&&(arr[j+5]==6))
{
count++;
}
else
{
flag=5;
}
}
else if(arr[j]==7)
{
if((arr[j+1]==7)&&(arr[j+2]==7)&&(arr[j+3]==7)&&(arr[j+4]==7)&&(arr[j+5]==7)&&(arr[j+6]==7))
{
count++;
}
else
{
flag=6;
}
}
else if(arr[j]==8)
{
if((arr[j+1]==8)&&(arr[j+2]==8)&&(arr[j+3]==8)&&(arr[j+4]==8)&&(arr[j+5]==8)&&(arr[j+6]==8)&&(arr[j+7]==8))
{
count++;
}
else
{
flag=7;
}
}
else if(arr[j]==9)
{
if((arr[j+1]==9)&&(arr[j+2]==9)&&(arr[j+3]==9)&&(arr[j+4]==9)&&(arr[j+5]==9)&&(arr[j+6]==9)&&(arr[j+7]==9)&&(arr[j+8]==9))
{
count++;
}
else
{
flag=8;
}
}
else if(arr[j]==0)
{
flag=9;
}
}
if(flag!=0)
{
printf("Invalid Data");
flag=0;
}
else
{
printf("%d\n",count);
count=0;
}
t--;
}
return 0;
}
if((arr[j+1]==9)&&(arr[j+2]==9)&&(arr[j+3]==9) ...)
You can simplify the above code with another for loop. Evidently you just want to see how many different numbers there are in the array.
Note that one of the main reasons to use C, or to learn C, is for efficiency. Therefore int arr[1000] is somewhat out of place because it allocates 4000 bytes. You may want to streamline that with malloc/free.
You should use printf to tell the user what to input.
I took some guesses on what you are trying achieve.
int tcaseno, nopgrp, error;
int count, i;
int *arr;
printf("no_test_cases: ");
scanf("%d", &tcaseno);
while(tcaseno > 0)
{
error = 0;
count = 1;
printf("no_people_in_group: ");
scanf("%d", &nopgrp);
if(nopgrp > 0 && nopgrp < 1000)
{
arr = malloc(nopgrp * sizeof(int));
printf("Enter %d numbers: ", nopgrp);
for(i = 0; i < nopgrp; i++)
scanf("%d", &arr[i]);
for(i = 1; i < nopgrp; i++)
{
if(arr[i - 1] > arr[i])
error = 1;
else if(arr[i - 1] != arr[i])
count++;
}
free(arr);
}
else
error = 1;
if(error)
printf("Invalid Data\n");
else
printf("Result: %d\n", count);
tcaseno--;
}

Runtime Error(SIGSEGV) in codechef

I am solving a problem https://www.codechef.com/problems/DCGAME/
Inside this, I am not able to find the reason behind getting Runtime Error(SIGSEGV). On my PC, outputs are fine. I know the meaning of this error but am still not able to find where I am making the mistake. My code is below:
#include <stdio.h>
void calc(long long int arr1[],int n,long long int arr2[][n],long long int game[],int m)
{
//printf("Pre process %d %d",n,m);
int i,j;
for(i=0;i<n;i++){game[arr1[i]]++;}
for(i=1;i<n;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(arr2[i-1][j]>arr1[i+j])
arr2[i][j]=arr2[i-1][j];
else
arr2[i][j]=arr1[i+j];
game[arr2[i][j]]++;
}
}
//for(i=0;i<=m;i++){printf(" %d ",game[i]);}
}
int main(){
int n,m,i,count=0,k;
char c,p;
scanf("%d %d",&n,&m);
long long int a[n],b[n][n],max=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if(a[i]>max)max=a[i];
b[0][i]=a[i];
}
long long int game[max+1];
for(i=0;i<=max;i++)
game[i]=0;
calc(a,n,b,game,max);
while(m--)
{
// printf("WHILE-M");
c=getchar();
//scanf("%c",&c);
scanf("%c %d %c",&c,&k,&p);
switch(c)
{//printf("SWITCH");
case '<':
//printf("CASE <");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
for(i=1;i<k;i++)
{if(game[i]>0)
count+=game[i];
}
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
case '>':
//printf("CASE >");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
for(i=k+1;i<=max;i++)
{if(game[i]>0)
count+=game[i];
}
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
case '=':
//printf("CASE =");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
count+=game[k];
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
}
}
return 0;
}
If your algorithm is otherwise correct (I didn't check) but you are running out of stack space, then a short fix is to change long long int a[n],b[n][n],max=0; to:
long long int *a, (*b)[n], max = 0;
a = malloc(n * sizeof *a);
b = malloc(n * sizeof *b);
and also change long long int game[max+1]; for(i=0;i<=max;i++) game[i]=0; to:
long long int *game = calloc(max+1, sizeof *game);
Also you should check for failure, e.g. if ( !a || !b ) exit(EXIT_FAILURE);. The rest of the code can remain unchanged.

The Next Palindrome : Segmentation fault [duplicate]

This question already has answers here:
Why I am getting the segmentation fault in following c program
(2 answers)
Closed 7 years ago.
I'm trying to solve the Next Palindrome problem on SPOJ. Here is the link to the problem SPOJ
This is my code for the problem. I get correct results when I run it on my machine for the following test cases :
9 11 99 101 808 818
This is my code :
#include<stdio.h>
#include<string.h>
char k[1000004];
int find_palin(char num[])
{
int len = strlen(num);
char str1[1000004] = {NULL};
char str3[500002] = {NULL};
char str2[500002] = {NULL};
char rev[500002] ={NULL};
if(len%2==0)
{
int half = (len)/2;
int i;
int j=0;
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
return 0;
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half;i<len;++i)
str3[j++] = num[i];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str1,rev);
for(i=0;i<len;++i)
k[i]=str1[i];
return 0;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str1,str3);
find_palin(str1);
}
}
else
{
int half = (len-1)/2;
int i;
int j=0;
if(len==1)
{
if(num[0]=='9')
{
k[0] = '1';
k[1] = '1';
return 0;
}
k[0] = ++num[0];
return 0;
}
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
return 0;
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half+1;i<len;++i)
str3[j++] = num[i];
str2[0] = num[half];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str2 , rev);
strcat(str1 , str2);
for(i=0;i<len;++i)
k[i]=str1[i];
return 0;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str2[0]!='9')
{
str2[0]++;
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
else
{
str2[0] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
}
}
}
int main()
{
char input[1000004];
int t;
scanf("%d" , &t);
int i;
for(i=0;i<t;++i)
{
scanf("%s" , &input);
find_palin(input);
printf("%s\n" , k);
}
return 0;
}
When I try to submit the code it gives Segmentation Fault. Can someone please help me as to why I'm getting this error?
The way to use large arrays is to allocate memory on the heap not on the stack. I have done this in your program to show you. Instead of doing that in main I lazily moved input[] to be a global var. You can't do that in find_palin because of the recursion, and anyway global variables are frowned on.
I also changed all your return 0 statements to goto so that a common clean-up can be done. It is not elegant, but I didn't want to change the structure of your code.
I also tweaked a few other things, such as checking the input was valid, and using a single #define from which all other sizes specified are derived.
#include<stdio.h>
#include<string.h>
#define ARRSIZE 1000004 // don't hard code stuff
char k[ARRSIZE];
char input[ARRSIZE]; // move out of main
void find_palin(char num[]) // change return type to void
{
int len = strlen(num);
char *str1 = calloc(ARRSIZE, sizeof(*str1)); // allocate and zero
char *str2 = calloc(ARRSIZE/2, sizeof(*str2));
char *str3 = calloc(ARRSIZE/2, sizeof(*str3));
char *rev = calloc(ARRSIZE/2, sizeof(*rev));
if (str1 == NULL || str2 == NULL || str3 == NULL || rev == NULL)
exit (1); // check memory allocations
if(len%2==0)
{
int half = (len)/2;
int i;
int j=0;
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
goto endfunc; // replace all return statements
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half;i<len;++i)
str3[j++] = num[i];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str1,rev);
for(i=0;i<len;++i)
k[i]=str1[i];
goto endfunc;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str1,str3);
find_palin(str1);
}
}
else
{
int half = (len-1)/2;
int i;
int j=0;
if(len==1)
{
if(num[0]=='9')
{
k[0] = '1';
k[1] = '1';
goto endfunc;
}
k[0] = ++num[0];
goto endfunc;
}
for(i=1;i<=len-1;++i)
{
if(num[i]!='0')
break;
k[i]='0';
}
if(i>len-1)
{
k[0]=num[0];
k[len-1]=num[0];
goto endfunc;
}
for(i=0;i<half;++i)
str1[i] = num[i];
for(i=half+1;i<len;++i)
str3[j++] = num[i];
str2[0] = num[half];
for(i=0 , j=half-1;i<half;++i,--j)
{
if(str3[i]>=str1[j])
break;
else
rev[i]=str1[j];
}
if(i>=half)
{
strcat(str2 , rev);
strcat(str1 , str2);
for(i=0;i<len;++i)
k[i]=str1[i];
goto endfunc;
}
else
{
for(i=0;i<half;++i)
str3[i] = '0';
if(str2[0]!='9')
{
str2[0]++;
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
else
{
str2[0] = '0';
if(str1[half-1]!='9')
str1[half-1]++;
else
{
for(i=half-1;i>=0;--i)
{
if(str1[i]=='9')
str1[i] = '0';
else
{
str1[i]++;
break;
}
}
if(i<0)
{
str1[half]='0';
str1[0] = '1';
}
}
strcat(str2,str3);
strcat(str1,str2);
find_palin(str1);
}
}
}
endfunc: // free the memory
free(rev);
free(str3);
free(str2);
free(str1);
}
int main()
{
int t;
int i;
if (1 != scanf("%d" , &t)) // check garbage input
exit (1);
for(i=0;i<t;++i)
{
if (1 != scanf("%s" , input)) // remove &
exit (1); // check garbage input
find_palin(input);
printf("%s\n" , k);
}
return 0;
}

Encode printing of alphabets in 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);
}

Resources