I want to input an array of integers then print out the even numbers from the inputted numbers..
example is if I input 2466688992,
it will output 24666882;
I have a my code below:
#include<stdio.h>
int main()
{
int a[5],i;
printf("Enter array of numbers: ");
scanf("%d",&a);
for(i=0; i<sizeof(a); i++){
if(a[i]%2==0)
printf("%d",a[i]);
}
getch();
return 0;
}
It resulted into garbage : 2468000075416640419940000004225568000
This is the function that prints even numbers in an integer :
#include<stdio.h>
int main(){
int num,rem,even=0,digit;
printf(" Enter an integer number: ");
scanf("%d",&num);
printf("\n The even digits present in %d are \n",num);
while(num>0){
digit = num % 10;
num = num / 10;
rem = digit % 2;
if(rem == 0)
even++;
printf("\n %d.",digit);
}
return 0;
}
You should scan the array as a string (unless you want to impose the number of items in the array), and then parse the string to store the different numbers:
long a[50];
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
int j = 0;
for (int i = 0; i < len; ) {
long sign = 1;
long n = 0;
if (buf[i] == '+') {
++i;
}
else if (buf[i] == '-') {
sign = -1;
++i;
}
if (isdigit(buf[i])) {
while (isdigit(buf[i])) {
n = 10 * n + buf[i++] - '0';
}
a[j] = n * sign;
}
else
i++;
}
for (int i = 0; i < j; i++)
if (!(a[i] ℅ 2)) // true if even
printf("%ld ", a[i]);
This will store all your digits in your array a of size j.
Edit: if you are talking about digits then its easier:
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
for (int i = 0; i < len; i++)
if (isdigit(buf[i]) && !((buf[i] - '0') ℅ 2)) // true if even, note that '0' equals 0x30 so there is no need to sub it to check for odd/even in reality.
printf("%c ", buf[i]);
Related
I need to get an integer form the user and put the number in array of chars with "+" or "-"
and it print only + and not - if the number is under zero, and it not print the numbers that the user put at all
if can someone tell me what is the problem in my code?
Thank you
int main()
{
int number = 0, i = 0;
char numberArray[MAX_LEN] = {0};
int length = 0;
int save = 0;
int j = 0;
printf("Enter num: ");
scanf("%c", &number);
if(number >= 0)
{
numberArray[i] = '+';
}
else
{
numberArray[i] = '-';
}
save = number;
length = findLength(number);
number = save;
j = length;
for(i = 1; i <= length; i++)
{
numberArray[j] = number % 10;
number /= 10;
j--;
}
printf("%s", numberArray);
}
int findLength(int number)
{
int length = 0;
while(number > 0)
{
number /= 10;
length++;
}
return length;
}
int main(void)
{
int number;
char number_str[10];
scanf("%d", &number);
sprintf(number_str, "%+d", number);
printf("%s\n", number_str);
}
To read your binary integer:
printf("Enter num: ");
scanf("%d", &number);
To print with +/-:
printf ("%+d\n", number);
To write the formatted integer to a string:
sprintf (numberArray, "%+d\n", number);
Here is a good reference for printf format strings:
http://www.cplusplus.com/reference/cstdio/printf/
scanf("%c", &number);
will give you the ASCII value of one char given in the input, that's why the numbers that printed aren't those you write.
you should change it to
scanf("%d", &number);
this way it will get the right input
if (number < 0) {
printf("%d", -number);
} else {
printf("+%d", number);
}
should suffice. Let the standard library routines do their work.
I'm trying to make a C program to insert elements into an array until user inputs a 0 or less number, as the title says. But when I print the array out, it doesn't show the numbers I inputted. I have tried using a while as well as do-while loops but without success.
#include <stdio.h>
int main() {
int data[100];
int i;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
int n = sizeof(data[i]);
for (int i = 0; i < n; i++) {
printf("%d ", &data[i]);
}
}
Try this:
#include <stdio.h>
int main() {
int data[100];
int i;
int counter = 0;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
counter++;
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
for (int j = 0; j < counter - 1; j++) {
printf("%d ", data[j]);
}
}
The problem was that you had printf("%d ", &data[i]); instead of printf("%d ", data[i]);.
And also you've trying to get the sizeof() of an element data[i], not the size of the whole array. That's why there's counter in my code.
int n = sizeof(data[i]);
this is wrong, you want
int n = i;
sizeof(data[i]) gives you the size of an int (4 on my machine)
On the other hand, you need to check the result of scanf, if a bad input is entered do not increment the counter, something like:
int i = 0;
while (i < 100)
{
int res = scanf("%d", &data[i]);
if (res == EOF)
{
break;
}
if (res == 1)
{
if (data[i] <= 0)
{
break;
}
i++;
}
else
{
// Sanitize stdin
int c;
while ((c = getchar()) != '\n');
}
}
Finally, scanf wants a pointer to the object, but this is not the case of printf:
printf("%d ", &data[i])
should be
printf("%d ", data[i])
I need to make a program that stores numbers inside of an array. But it must have no duplicate elements.
int x;
int z[8];
for( x = 0; x<8;x++)
printf("number: ");
scanf("%d",&z[x]);
}
for( x=0;x<8;x++) {
printf("%d ",z[x]);
}
First, initialize the array, so that you do not end up reading an uninitialized value and fail the test.
int user_nums[6] = {0};
Next, you need to have another check in the for loop, to read the number again if it is a duplicate.
The code will look like this.
#include<stdio.h>
int main(){
int x,y;
int exists = 0;
int user_nums[6] = {0};
for( x = 0; x<6;x++){//for loop to get the players selected numbers
do {
exists = 0;
printf("Enter a number(from the #'s 1-42): ");
scanf("%d",&user_nums[x]);
for(y =0; y < x; y++) { //to check for duplicates
if (user_nums[x] == user_nums[y])
{
printf("Number already exists\n ");
exists = 1;
break;
}
}
}while (user_nums[x]<1 || user_nums[x]>42 || exists);//accepts only numbers from 1-42 which are not duplicates (continous to ask you for a number until condition is met).
}
printf("Your numbers: \n");
for( x=0;x<6;x++){
printf("%d ",user_nums[x]); // prints the numbers you inputed.
}
return 0;
}
The following code could work in O(n):
#include<stdio.h>
int main()
{
int user_nums[6];
int index[50];
for (int i = 0; i != sizeof(index) / sizeof(index[0]); ++i)
index[i] = -1;
for (int i = 0; i < sizeof(user_nums) / sizeof(user_nums[0]); ++i) {
for (;;) {
printf("Enter a number(from the #'s 1-42): ");
scanf("%d", user_nums + i);
if (user_nums[i] < 1 || user_nums[i] > 42) {
printf("wrong number\n");
continue;
}
if (index[user_nums[i]] != -1) {
printf("dump number\n");
continue;
}
index[user_nums[i]] = i;
break;
}
}
printf("Your numbers: \n");
for(int i = 0; i < 6; ++i)
printf("%d ", user_nums[i]);
return 0;
}
I am trying to write a program that reads a string from the user and prints the string's characters in a diagonal pattern. I set the maximum length of the string as 50 characters. The program can print the characters in a diagonal pattern, but it doesn't print the characters correctly.
#include<stdio.h>
int main () {
int i = 0, j = 0, m;
char c[50];
printf("Enter a string: ");
scanf("%c", c);
m = sizeof(c) / sizeof(c[0]);
for (i = 1; i <= m; i++) {
for (j = 1; j <= i; j++) {
if (j == i) {
printf("%c", c[i-1]);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Check the code below:
#include<stdio.h>
int main ()
{
int i=0,s=0;
char c[50];
printf("Enter a string: ");
scanf("%s",c);
while(c[i] != '\0')
{
s = i;
while(s--)
printf(" ");
printf("%c\n",c[i]);
i++;
}
return 0;
}
After taking ten numbers as input from the user, I want to add up the ones that are evenly divisible by 2.
I am able to get the input from the user, but I'm not sure how to check which numbers are divisible by two, and add only those.
#include <stdio.h>
#include <ctype.h>
int main(void) {
int i = 0;
int val;
char ch;
int numbers[10];
while(i < 10) {
val = scanf("%d%c", numbers + i, &ch);
if(val != 2 || !isspace(ch)) {
while((ch = getchar()) != '\n') // discard the invalid input
;
printf("Error! Entered number is not an integer.\n");
printf("Please enter an integer again.\n");
continue;
}
++i;
}
printf("%d\n", numbers[0]);
printf("%d\n", numbers[1]);
printf("%d\n", numbers[2]);
printf("%d\n", numbers[3]);
printf("%d\n", numbers[4]);
printf("%d\n", numbers[5]);
printf("%d\n", numbers[6]);
printf("%d\n", numbers[7]);
printf("%d\n", numbers[8]);
printf("%d\n", numbers[9]);
return 0;
}
How about:
int sum = 0;
for (int i = 0; i <= 9; i++)
{
if (numbers[i] % 2 == 0)
sum += numbers[i];
}
completely unnecessary optimization
int sum[2]={0};
for(size_t i = 0; i <=9; ++i) sum[numbers[i]&1]+=numbers[i];
int main(void)
{
int i;
int numbers[10];
int sum = 0;
for(i=0; i<10; ++i)
{
printf("Enter #%d:\n", i+1);
scanf("%d", numbers+i);
if (numbers[i] % 2 == 0) // Then Number is even
{
sum += numbers[i];
}
}
printf("The sum of only the even numbers is %d\n", sum);
getch();
return 0;
}