Practice 2
Practice 2
1. Given two points (x1, y1), (x2, y2). Write a C program to check if all the
three points fall on one straight line.
2. If the three sides of a triangle are entered through the keyboard, write a C
program to check whether the triangle is valid or not. The triangle is valid if
the sum of two sides is greater than the largest of the three sides.
3. Write a program to add first n terms of the following series using for loop:
c. # include <stdio.h>
void message( ) ;
int main( )
{
message ( message( ) ) ;
return 0 ;
}
void message( )
{
printf ( "It's a small world after all...\n" ) ;
}
14. Point out the errors in the following code segment if any:
a.
#include <stdio.h>
int char mixed[ 100 ] ;
int main( )
{
int a[ 10 ], i ;
for ( i = 1 ; i <= 10 ; i++ )
{
scanf ( "%d", a[ i ] ) ;
printf ( "%d\n", a[ i ] ) ;
}
return 0 ;
}
b.
# include <stdio.h>
int main( )
{
int size ;
scanf ( "%d", &size ) ;
int arr[ size ] ;
for ( i = 1 ; i <= size ; i++ )
{
scanf ( "%d", &arr[ i ] ) ;
printf ( "%d\n", arr[ i ] ) ;
}
return 0 ;
}
c.
# include <stdio.h>
int main( )
{
int i, a = 2, b = 3 ;
int arr[ 2 + 3 ] ;
for ( i = 0 ; i < a + b ; i++ )
{
scanf ( "%d", &arr[ i ] ) ;
printf ( "%d\n", arr[ i ] ) ;
}
return 0 ;
}
15. Write a C program to find the second largest element in a given array.
16. Write a C program to display elements of an array using a function.
17. What are Strings in C? Why is a ‘\0’ necessary at the end of the string? What is
the difference between character array and string literal?
18. Write a C program to find the length of a string and print the string in lowercase
in the output.
19. What would be the output of the following programs:
a.
# include <stdio.h>
int main( )
{
char c[ 2 ] = "A" ;
printf ( "%c\n", c[ 0 ] ) ;
printf ( "%s\n", c ) ;
return 0 ;
}
b.
# include <stdio.h>
int main( )
{
char s[ ] = "Get organised! learn C!!" ;
printf ( "%s\n", &s[ 2 ] ) ;
printf ( "%s\n", s ) ;
printf ( "%s\n", &s ) ;
printf ( "%c\n", s[ 2 ] ) ;
return 0 ;
}
20. Write a C program to check whether a string is palindrome or not.