C assignments

1)Program to print all prime numbers between 1 and 100

#include<stdio.h>
int main()
{
int ct=0,n=0,i=1,j=1;
while(n<25)
{
j=1;
ct=0;
while(j<=i)
{
if(i%j==0)
ct++;
j++;
}
if(ct==2)
{
printf(“%d “,i);
n++;
}
i++;
}
}

Steps

Algorithm

  • STEP 1: START
  • STEP 2: SET ct =0, n=0, i=1,j=1
  • STEP 3: REPEAT STEP 4 to STEP 11 until n<25
  • STEP 4: SET j= 1
  • STEP 5: SET ct = 0
  • STEP 6: REPEAT STEP7 to STEP 8 UNTIL j<=i
  • STEP 7: if i%j = = 0 then ct =ct +1
  • STEP 8: j = j + 1
  • STEP 9: if ct= 2 then print i
  • STEP 10: n = n +1
  • STEP 11: i = i +1
  • STEP 12: END

2)Program to left rotate the elements of an array.Example 1 2 3 4 5 will be chaged 4 5 1 2 3

#include <stdio.h>  

int main()  
{  
    //Initialize array   
    int arr[] = {1, 2, 3, 4, 5};   
    //Calculate length of array arr  
    int length = sizeof(arr)/sizeof(arr[0]);  
    //n determine the number of times an array should be rotated  
    int n = 3;  

    //Displays original array  
    printf("Original array: \n");  
    for (int i = 0; i < length; i++) {   
        printf("%d ", arr[i]);   
    }    

    //Rotate the given array by n times toward left  
    for(int i = 0; i < n; i++){  
        int j, first;  
        //Stores the first element of the array  
        first = arr[0];  

        for(j = 0; j < length-1; j++){  
            //Shift element of array by one  
            arr[j] = arr[j+1];  
        }  
        //First element of array will be added to the end  
        arr[j] = first;  
    }  

    printf("\n");  

    //Displays resulting array after rotation  
    printf("Array after left rotation: \n");  
    for(int i = 0; i < length; i++){  
        printf("%d ", arr[i]);  
    }  
    return 0;  
}  

3)Program to count the total number of vowels and consonants in a string.

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
//Counter variable to store the count of vowels and consonant
int i, vCount = 0, cCount = 0;
char str[] = “This is a really simple sentence”;
for(i = 0; i < strlen(str); i++){ str[i] = tolower(str[i]); if(str[i] == ‘a’ || str[i] == ‘e’ || str[i] == ‘i’ || str[i] == ‘o’ || str[i] == ‘u’) { //Increments the vowel counter vCount++; } else if(str[i] >= ‘a’ && str[i] <= ‘z’){
//Increments the consonant counter
cCount++;
}
}
printf(“Number of vowels : %d\n”, vCount);
printf(“Number of consonant : %d”, cCount);
return 0;
}

4)Program to print the duplicate elements of an array.

#include<stdio.h>

int main()  
{  
    //Initialize array   
    int arr[] = {1, 2, 3, 4, 2, 7, 8, 8, 3};   

    //Calculate length of array arr  
    int length = sizeof(arr)/sizeof(arr[0]);  

    printf("Duplicate elements in given array: \n");  
    //Searches for duplicate element  
    for(int i = 0; i < length; i++) {  
        for(int j = i + 1; j < length; j++) {  
            if(arr[i] == arr[j])  
                printf("%d\n", arr[j]);  
        }  
    }  
    return 0;  
}  

Predict output

What will be output of the following program?

#include<stdio.h>

int main()

{ int x; x=10,20,30; printf(“%d”,x); return 0; }

(A) 10 (B) 20 (C) 30 (D) 0 (E) Compilation error

Answer :(A)

MCQs

1. Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;

Answer: b
Explanation: Space, comma and $ cannot be used in a variable name.

2. What is an example of iteration in C?
a) for
b) while
c) do-while
d) all of the mentioned

Answer: d
Explanation: None.

3. What is #include <stdio.h>?
a) Preprocessor directive
b) Inclusion directive
c) File inclusion directive
d) None of the mentioned

Answer: a
Explanation: None.

4. scanf() is a predefined function in______header file.
a) stdlib. h
b) ctype. h
c) stdio. h
d) stdarg. h

Answer: c
Explanation: scanf() is a predefined function in “stdio.h” header file.printf and scanf() carry out input and output functions in C. These functions statements are present in the header file stdio.h.

5.What will be the output of the following C code?

  1. #include <stdio.h>
  2. int main()
  3. {
  4. signed char chr;
  5. chr = 128;
  6. printf(“%d\n“, chr);
  7. return 0;
  8. }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned

Answer: b
Explanation: The range of signed character is from -128 to +127. Since we are assigning a value of 128 to the variable ‘chr’, the result will be negative. 128 in binary is represented as “1000 0000” for character datatype. As you can see that the sign bit is set to 1, followed by 7 zeros (0), its final decimal value will be -128 (negative 128).
Output:
$ cc pgm2.c
$ a.out
-128

6.Which of the following refers to an empty value?
a. void
b. Void
c. char
d. float

7. Which of the following is used to provide an alias to existing data types in C?
a. enum
b. def
c. pointer
d.typedef

Practice Test

Quiz

Published by webknowl

Web Application Developer

One thought on “C assignments

Leave a comment