I wrote this small program to count the number of trailing zeroes. I got my algorithm correct. But I cannot get the output right. The first line we enter is for the number of inputs (T). Later the user enters the number (whose number of trailing zeroes in factorial is to be calculated.) And then print the answer (count). But after I input the value for N, I get the answer on the third line (I used just one '\n'). I need to get my output right.
#include <stdio.h>
int main()
{
int T;
int i,j,temp,count=0;
long int N;
scanf("%d",&T);
for(i=0;i<T;i++)
{
scanf("\n%ld",&N);
for(j=5;j<=N;j+=5)
{
temp=j;
while(j > 1)
{
if(j%5 == 0)
count++;
j=j/5;
}
j=temp;
}
printf("\n%d",count);
count =0;
}
return 0;
}
Change:
printf("\n%d",count);
to:
printf("%d\n",count);
and:
scanf("\n%ld",&N);
to:
scanf("%ld",&N);
Related
I tried to scan and print the characters of array using below code but input characters are not matching with output characters
#include <stdio.h>
int main() {
char s[10];
int i, n;
printf("enter the value of n:\n");
scanf("%d", &n);
printf("start entering the characters:\n");
for (i = 0; i < n; i++) {
scanf("%c", &s[i]);
}
for (i = 0; i < n; i++) {
printf("%c", s[i]);
}
return 0;
}
OUTPUT
enter the value of n:
5
start entering the characters:
ABCDE(scanf values)
ABCD(printf values)
Can anyone please clarify my doubt why is the output not matching with input
Since you are wanting to read data into a character array with "scanf" you probably could just reference the string identifier instead and simplify things. Following are a few tweaks to your code that still inputs the data and prints it back out.
#include <stdio.h>
#include <string.h>
int main()
{
char s[10];
int i, n;
printf("enter the value of n:\n");
scanf("%d", &n);
printf("start entering the characters:\n");
scanf("%s", s); /* In lieu of using a loop */
if (strlen(s) < n) /* Just in case less characters are entered than was noted */
n = strlen(s);
for (i = 0; i < n; i++)
{
printf("%c", s[i]);
}
printf("\n");
return 0;
}
The program just scans in the complete string instead of a character at a time. Also, I included the "<string.h> file so as to use functions such as "strlen" (get the length of the string) to provide a bit more robustness to the code. Running the program netted the same character set that was entered.
:~/C_Programs/Console/InputOutput/bin/Release$ ./InputOutput
enter the value of n:
7
start entering the characters:
ABCDEFG
ABCDEFG
You might give that a try.
Regards.
By using the given source code I am trying to do some arithmetic operations. The given input is:
1
234*34
Since there is no space between 234 and * and 34, scanf is not reading it properly. What should I do so that scanf will read this input and give correct answer without providing the space between the input numbers?
#include <stdio.h>
int arithematic(){
int a,c,t,i=0,k,l;
char b[100];
scanf("%d%s%d",&a,&b[0],&c);
if(b[0] =='+')
t = a+c;
if(b[0] == '-')
t=a-c;
if(b[0]=='*')
t=a*c;
k=a;
l=c;
while(k>0||l>0)
{
k/=10;l/=10;
i++;
}
printf("%d\n",a);
printf("%s",b);
printf("%d\n",c);
while(i>0)
{
printf("-");
i--;
}
printf("\n%d\n",t);
}
int main(void)
{
int n;
scanf("%d",&n);
while(n--)
{
arithematic();
printf("\n");
}
}
Replace scanf("%d%s%d",&a,&b[0],&c); with scanf("%d %1[+-*/]%d",&a,b,&c);. The meaning of %1[+-*/] is to consume exactly one character belonging to the set of the four operators. The space preceding it is to consume optional white spaces.
I want to print multiple character using printf. My approach up to now is this-
#include <stdio.h>
int main()
{
printf("%*c\n", 10, '#');
return 0;
}
But this only prints 9 spaces before the #.
I want to print it like this-
##########
I am unable to figure out how to do this. Please help me?
You can not use printf like that to print repetitive characters in Ansi C. I suggest you to use a loop like this -
#include <stdio.h>
int main()
{
int i;
for(i = 0; i < 10; i++) putchar('#');
return 0;
}
Or if you have absolutely no desire to use loops, you can do something like this-
#include <stdio.h>
int main()
{
char out[100];
memset(out, '#', 10);
out[10] = 0;
printf("%s", out);
return 0;
}
By the way, using printf like this also works-
#include <stdio.h>
int main()
{
printf("%.*s", 10, "############################################");
return 0;
}
I think the best approach, if you have an upper limit to the number of characters to be output is:
printf("%.*s", number_of_asterisks_to_be_printed,
"**********************************************************************");
I think this will also be the most efficient, portable way to do that.
this will print ten # characters, followed by a newline
char tenPounds[] = "##########";
printf( "%s\n", tenPounds);
I am working on a similar problem in "The C Programming Language" book (exercises 1-13 and 1-14). My own program, to start simply, is to count the occurrences of the digits 0 to 9 in a given input, and print a horizontal histogram made of '=' bars according to each count.
To do this, I created the following program;
main() {
int c, ix, k;
int nDigit[10];
//Instantiate zero values for nDigits array
for(ix = 0; ix < 10; ix++) {
nDigit[ix] = 0;
}
//Pull in input, counting digit occurrences and
//incrementing the corresponding value in the nDigit array
while ((c = getchar()) != EOF) {
if (c >= '0' && c <= '9') {
++nDigit[c-'0'];
}
}
//For each digit value in the array, print that many
//'=' symbols, then a new line when completed
for (ix = 0; ix < 10; ix++) {
k = 0;
while (k <= nDigit[ix]) {
putchar('=');
k++;
}
printf("\n");
}
}
Note that this is a work in progress. A proper histogram should include axes labels, and most importantly this program does not account for digits of zero count. If the input includes five 1's but no 0's, there is no visual way to show that we have no zeros. Still, the mechanism for printing multiple symbols works.
given a number, i was aked to find the next number which is a palindrome.Thisis the code i have writen. my code works fine but the website I am working on says "time limit exceeded"...how do I correct this?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 50
void reverse (char s[]){
int c,i,j;
for (i=0,j=strlen(s)-1;i<j;i++,j--){
c= s[i];
s[i]=s[j];
s[j]=c;
}
}
void itoa (int n, char s[]) {
int i;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[i] = '\0';
reverse(s);
}
int main (void) {
int t,j;
scanf("%d",&t);
for(j=0;j<t;j++){
int k,c=1,i;
char s[MAXLEN];
scanf("%d",&k);
int a= k+1;
while (c!=0){
itoa(a,s);
int e=strlen(s)-1;
for(i=0;i<(e+1)/2;i++){
if (s[i]==s[e-i]){
c=0;
}
else{
c=1;
goto state;
}
}
state:a++;
}
printf("%s\n",s);
}
return 0;
}
You need to use a smarter way than brute force!
Here is an algorithm with time complexity log(N):
Read the number as a string.
Take the first half of the number.
Make a palindrome by adding the first half reversed. (Consider the two cases: even/odd length of the original number)
If this palindrome is greater than the original number you are done.
If it is not:
Take the first half of the number and add 1.
Make a palindrome by adding the first half reversed. (Again consider the two cases: even/odd length of the original number).
Done!
I'm new in C programming language.
I need to get every digit separately that user have entered.
Here is my code:
#include <stdio.h>
int main()
{
int n[100];
printf("Enter a number: ");
scanf("%d",&n);
printf("%d %d %d",n[1],n[2],n[3]);
return 0;
} //i know that my code is not assigning like i want.
and now for example user entered a number like 123, i want the output like 1 2 3, How can i assign every digit to n[i] ? Without using string to int or int to string like atoi? Here is what Im going to do: User will enter a number and the program will search from Matrix 100x100 in row or column. i think i need to get the every digit separately to search.
No need to go to character array. The lats digit of a number n can be computed using n%10. Then you can remove the last digit using n /= 10. So this cycle would print the digits in reverse order:
void print_rev_digits(int n) {
while (n) {
printf("%d\n", n%10);
n /= 10;
}
}
And using a stack you can print the digits in the correct order. You can also use recursion for this(which will use stack for you). I am deliberately not posting a complete solution.
In this case you should read the user input character by character:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main()
{
char input[100];
int n[100];
printf("Enter a number: ");
if (fgets(input, sizeof(input), stdin)) { // attempt to read a line
int i;
for (i = 0; input[i]; i++) { // for each entered character
if (input[i] >= '0' && input[i] <= '9') { // is a digit
n[i] = input[i] - '0';
printf("%d ", input[i] - '0');
}
else if (isspace(input[i])) // end of entered integer
break;
else {
printf(stderr, "Input is not a number\n");
return -1;
}
}
printf("\n");
} else {
fprintf(stderr, "User did not enter valid input.\n");
}
return 0;
}