Advertisements

Search results for break

Example:[1]

            Write a c program function to create simple calculator using switch case.
          

            #include <stdio.h>
int main()
{
    char op;
    float num1, num2, result=0.0f;
    printf("Enter [number 1] [operator] [number 2]\n");
    scanf("%f %c %f", &num1, &op, &num2);
    switch(op)
    {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            result = num1 / num2;
            break;
        default:
            printf("Unsupported Operator");
    }
    printf("%.2f %c %.2f = %.2f\n", num1, op, num2, result);
    return 0;
}
          
Example:[2]

            Write a C program function to take a string as its input and return the first word of the string passed to it.

          

            #include <stdio.h>
void getfword(char *s, char f_word[]);
int main() {
	char str[] = "Hello how are you";
	char res[20];
	getfword(str,res);
	printf("Result is:%s\n", res);
}
void getfword(char *s, char f_word[])
{
	int i = 0;
	while(s[i] != '\0')
	{
		if(s[i] == ' ')
		{
			break;
		}else
		{
			f_word[i] = s[i];
			f_word[i + 1] = '\0';
			i++;
		}
	}
}

          

 Search Links


 Popular C search MCQs


 Popular C search examples