0% found this document useful (0 votes)
19 views4 pages

Practice 2

Uploaded by

Tanim Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Practice 2

Uploaded by

Tanim Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 2

[Topics : Conditional statements, Loops, Functions, Arrays, Strings]

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:

4. Calculate the sum of the following series, where x and n is provided as


user input.

5. What is a function in C? What are the types of functions in C? Give examples


of each.
6. Point out the errors, if any, in the following programs:
a. # include <stdio.h>
int addmult ( int, int )
int main( )
{
int i = 3, j = 4, k, l ;
k = addmult ( i, j ) ;
l = addmult ( i, j ) ;
printf ( "%d %d\n", k, l ) ;
return 0 ;
}
int addmult ( int ii, int jj )
{
int kk, ll ;
kk = ii + jj ;
ll = ii * jj ;
return ( kk, ll ) ;}
b. # include <stdio.h>
int main( )
{
float a = 15.5 ;
char ch = 'C' ;
printit ( a, ch ) ;
return 0 ;
}
printit ( a, ch )
{
printf ( "%f %c\n", a, ch ) ;
}

c. # include <stdio.h>
void message( ) ;
int main( )
{
message ( message( ) ) ;
return 0 ;
}
void message( )
{
printf ( "It's a small world after all...\n" ) ;
}

7. Write a C program to find maximum and minimum between two numbers


using a function.
8. A positive integer is entered through the keyboard. Write a function
primeFactors(int n) to obtain the prime factors of this number (n).
9. Any year is entered through the keyboard. Write a function checkLeap(int
year) to determine whether the year is a leap year or not.
10. What is an array? How do you declare and initialize an array in C? How
many types of array are there?
11. Write a C program to print all the negative elements of a given array.
12. Write a C program to count all the even and odd numbers in a given
array.
13. What will be the outputs of the following programs:
a.
# include <stdio.h>
int main( )
{
int num[ 26 ], temp ;
num[ 0 ] = 100 ;
num[ 25 ] = 200 ;
temp = num[ 25 ] ;
num[ 25 ] = num[ 0 ] ;
num[ 0 ] = temp ;
printf ( "%d %d\n", num[ 0 ], num[ 25 ] ) ;
return 0 ;
}
b.
# include <stdio.h>
int main( )
{
int array[ 10 ], i ;
for ( i = 0 ; i <10 ; i++ )
{
array[ i ] = 'A' + i ;
printf ( "%d %c\n", array[ i ], array[ i ] ) ;
}
return 0 ;
}

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.

You might also like