Its a program to convert integer stored in a string into an int. Please help.
#include<stdio.h>
#include<math.h>
int main()
{
int num, n, i;
num = 0;
printf("Enter n\n");
scanf("%d", &n);
char string[n+1];
printf("Enter the number\n");
scanf("%s", &string);
for(i=0;string[i]!='\0';i++)
{
num = num + string[i]*pow(10,n-i-1);
}
printf("The required number is %d", num);
return 0;
}
In typical environment, character codes for digits are not equal to the numbers the digits represent for.
Character codes for digits in C are defined to be continuous, so you can convert the character codes to the corresponding numbers by subtracting '0' from the character code.
num = num + (string[i]-'0')*pow(10,n-i-1);
By the way, there are some better ways to do the conversion:
sscanf(string, "%d", &num); /* available in stdio.h */
num = atoi(string); /* stdlib.h is required for atoi() */
Also note that scanf("%s", &string); invokes undefined behavior because a pointer to array is passed where char* (a pointer to char) is required. The & should be removed.
In your code the size of your string must be equal to n+1 ,so you can removed all this
printf("Enter n\n");
scanf("%d", &n);
char string[n+1];
for(int i=strlen(string)-1;i>=0;i--)
I started from the last caracter in my string to the first caracter and I multiplied each caracter by j (j=1 then j=10 then j=100 .....)
#include<stdio.h>
#include<math.h>
int main()
{
int j=1;
char string[100];
printf("Enter the number\n");
scanf(" %s", string);
int num=0;
for(int i=strlen(string)-1;i>=0;i--)
{
int k=string[i]-'0';//subtract each case by '0'
num=num+k*j;
j=j*10;
}
printf("The required number is %d", num);
return 0;
}
Related
#include <stdio.h>
#include <string.h>
int countLetters(char *string1, char letter){
int count=0;
for(int i=0; i<strlen(string1); i++){
if(string1[i]=letter){
count++;
}
}
return count;
}
int main(){
char string1[200];
char letter;
int count;
printf("\nEnter the string: \n");
fgets(string1, 200, stdin);
printf("\nEnter character to be searched: \n");
scanf("%c", &letter);
count = countLetters(string1, letter);
printf("\nThe number of occurrences: %d", count);
}
I was expecting for the function to output the number of times each letter of the array was equal to the char(letter) inputted by the user, but it is just giving me the length of the string.
Change the line:
if(string1[i]=letter){
to
if(string1[i]==letter){
Note, that the string1[i]=letter was overwriting data in string1[i].
You have to use equal equal to operator instead of assignment operator in if condition like this
if(string1==latter)
in your if condition if(string1=latter) value of latter variable is assign to string1[i]
This code with C language is supposed to count all characters equal to A as well as number characters in a table .. ~~ I've started by reading the table's characters giving by the user using a for loop and then i've used another for loop to count the number of characters equal to A as well as numbers~~
THE PROBLEM is with SCANF! what should I do to write scanf one time and not twice ???
#include <stdio.h>
#include <stdlib.h>
int main()
{
char T[100] = {0};
int i=0,N=0,b=0,n=0,x=0,j=0,k=0;
printf("give the number of your table's columns \n");
scanf("%d", &N);
if (N > 0 && N <= 100) {
for (i; i < N; i++) {
scanf("%c",T[i]);
printf("give the character of the column number %d /n", i);
scanf("%c",T[i]);
}
for (i = 0; i < N; i++) {
if (T[i] == 'A') b++;
else if (T[i]<='9' && T[i]>='0') n++;
}
printf("the number of characters equal to A is %d\n",b);
printf("The number of numeric characters is %d\n",n);
}
return 0;
}
your code needing only few editing
#include <stdio.h>
#include <stdlib.h>
int main()
{
char T[100]={0};
int i=0,N=0,b=0,n=0,x=0,j=0,k=0;
printf("give the number of your table's columns:\n");
scanf(" %d", &N);
if (N > 0 && N <= 100)
{
for (i=0; i < N; i++) {
//one scanf removed
printf("give the character of the column number %d \n", i);//<--/n to \n
scanf(" %c",&T[i]); //<-- & added to store the value
}
for (i = 0; i < N; i++) {
if (T[i] == 'A') b++;
else if (T[i]<='9' && T[i]>='0') n++;
}
printf("the number of characters equal to A is %d\n",b);
printf("The number of numeric characters is %d\n",n);
}
return 0;
}
while using scanf use & so it can store value while using scanf give a space before the scanf(" %c",&T[i]);
for (i; i < N; i++) {
scanf("%c",T[i]);
printf("give the character of the column number %d /n", i);
scanf("%c",T[i]);
}
here you tried to override T[i] twice without & in scanf removing one scanf and adding & and also for(i=0;i<n;i++) will be better way to go use \n instead of /n
Is this what you were looking to do?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i, A_bin=0, Num_bin=0,length;
char array[100];
printf("Enter a string of numbers and letters: ");
scanf("%s",array); //Storing the Char array in "array"
length=strlen(array); //Getting the length of the Char array
for(i=0;i<=length;i++)
{
if (array[i]=='A') A_bin+=1;
else if (array[i]>= '0' && array[i]<='9') Num_bin+=1;
}
printf("The amount of 'A's in the string is: %d \n" ,A_bin);
printf("The amount of digits in the string is: %d" ,Num_bin);
return 0;
}
Instead of that story I wrote you. Here is what I think you were looking for right. This only looks for capital 'A' although could be adjusted for any characters you wanted by following the scheme. You could always adjust the array size as well for very large inputs. May have errors, play with it and see how it works. Hope the example I provided helped.
Output Example:
Enter a string of numbers and letters: AAJUR874EYRIAA
The amount of 'A's in the string is: 4
The amount of digits in the string is: 3
So I'm having a problem about showing what my original variable value is after changing it in the code.
#include <stdio.h>
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
while (n!=0){
n/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
Example input:123
Output of this code "your number 0 has 3 digits"
I want to know how to be able to refer the variable "n" in the printf to the original value of '123' so the output will be "your number 123 has 3 digits"
I would recommend that you use a separate variable to save your value or count with a different variable.
This code would look something like this:
#include <stdio.h>
int main()
{
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
int buffer = n
while (buffer!=0)
{
buffer/=10;
count++;
}
printf("your number %d has %d digits", n, count);
return 0;
}
This way you save your variable in your code and you only used a buffer and not the actual value n.
You can keep a copy of the original variable and use that copy of the variable while printing.
You can do this:
int main(){
int n, count =0;
printf("enter an integer = ");
scanf("%d", &n);
printf("your number %d has ", n);
while (n!=0){
n/=10;
count++;
}
printf("%d digits", count);
return 0;
}
Of course, you may have to do some error checks..
I am trying to make a simple calculator in c as i want to test my programming skills. I keep getting a error though.
#include <stdio.h>
int calc()
{
int *fnum;
int *snum;
printf("Enter your First Number: ");
scanf("%d", fnum);
printf("Enter your Second Number: ");
scanf("%d", snum);
int answer = *fnum + *snum;
printf("%d", answer);
return 0;
}
int main()
{
int *calcType;
printf("Type of Calculation: 1=A, 2=S, 3=M, 4=D: ");
scanf("%d", calcType);
if (*calcType == 1)
{
calc();
}
return 0;
}
But Then i get this error:
Segmentation fault (core dumped)
Please help, i have no idea what this means.
You called scanf to read an integer into the memory pointed to by calcType, but you never set calcType to point to a valid address.
Where the snum and fnum (int * pointers) point to? you have to declare a variable and pass its address (by reference operator &) to scanf.
The code should be something like this
int calc(){
int fnum;
int snum;
printf("Enter your First Number: ");
scanf("%d", &fnum);
printf("Enter your Second Number: ");
scanf("%d", &snum);
int answer = fnum + snum;
printf("%d", answer);
return 0;
}
Also same problem with calcType pointer.
You should be inputing ints, not int* (int pointers):
int fnum; /* This is now an int */
int snum; /* so is this */
printf("Enter your First Number: ");
scanf("%d", &fnum); /* Note that fnum's address is passed */
printf("Enter your Second Number: ");
scanf("%d", &snum); /* Same for snum */
int answer = fnum + snum;
printf("%d", answer);
return 0;
That is a generic error message. You are incorrectly using pointers for fnum and snum, and attempting to add their addresses in memory.
You missed
&
Without it the programm wont know where to storage the data
scanf("%d", &fnum);
I also dont understand why you put
*
Well it is a problem about finding the biggest and smallest number in a group of numbers, but we do not know how many numbers the user wants-
So far this is what i have done:
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int i;
int maxi=0;
int minim=0;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf ("%d", &num);
while (num>0)
{
printf ("\nEnter number:");
scanf ("%d", &i);
if (num>i)
minim=i++;
else
if (i>num)
max=i++;
cont++;
}
printf ("\nBiggest number is es: %d", maxi);
printf ("\nSmallest number is: %d", minim);
getch();
return 0;
}
I did my program to ask how many numbers the user will want to put and i made the program to read them, BUT when it reads the biggest or/and smallest numbers it will sometimes changes biggest with small and it will not read negative numbers.
How do i do to make my program better?
You're comparing against the wrong values.
do
{
printf("Enter a number.\n");
scanf("%i", &input);
if min > input
min = input
if max < input
max = input
} while (input > 0);
#include <stdio.h>
#include <conio.h>
#include <limits.h>
int main(){
int num;
int i;
int maxi=0;
int minim=INT_MAX;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf("%d", &num);
if(num > 0){
while (num>0){
printf ("\nEnter number:");
if(scanf("%d", &i) == 1 && !(i<0)){
if(minim > i)
minim = i;
if (maxi < i)
maxi = i;
++cont;
--num;
} else {
//fprintf(stderr, "redo input!\n")
;
}
scanf("%*[^\n]%*c");
}
printf ("\nBiggest number is : %d", maxi);
printf ("\nSmallest number is : %d\n", minim);
}
getch();
return 0;
}
You should initialize mini to the largest possible int, i.e. INT_MAX and maxi to the smallest possible int, i.e., INT_MIN. This way, even if the first number is negative, it will be considered for maxi, and if the first number is positive it will still be considered for mini. The constants INT_MAX and INT_MIN are included in <climits> or <limits.h>.
Also, you are comparing the current entered number with num, which is the counter of numbers entered by user, not one of the values he wants to compare. A better modified code would be :
#include<limits.h>
#include<stdio.h>
int main()
{
int num;
int maxi=INT_MIN; //initialize max value
int mini=INT_MAX; //initialize min value
int temp;
scanf("%d", &num); //take in number of numbers
while(num--) //loop "num" times, num decrements once each iteration of loop
{
scanf("%d", &temp); //Take in new number
if(temp>maxi) //see if it is new maximum
maxi=temp; //set to new maximum
if(temp<mini) //see if new minimum
mini=temp; //set to new minimum
}
printf("\nMaxi is:\t%d\nMini is:\t%d\n", maxi, mini); //print answer
return 0;
}