C programming 2021 Solution

2nd Semester

2) WAP to display all the numbers from 100 to 200 divisible by 4

    #include<stdio.h>
    void main(){
        for(int i=100;i<=200;i++)
            i%4==0? printf("%d ",i):i;
    }

3) Create a function int greater(int ,int ) that returns greater number.

    int greater(int a,int b){
        return a>b? a : b;
    }

5)check if string is palindrome or not

    #include<stdio.h>
   #include<string.h>
    void main(){
    char string[100],temp[100];
    printf("enter string");
    gets(string);
    strcpy(temp,string);
    strrev(string);
    if(strcmp(string,temp)==0)
     printf("the string is palindrome");
    else printf("the string is not palindrome");
    }

6)A data file contains name ,age ,address,cell number of some students WAP to list all the students who are from”Pokhara”.

#include<stdio.h>
    void main(){
     char name[20],address[20];
     int age,number;
        FILE *ptr;
        ptr= fopen("stdent.dat","r");
        if(ptr==NUll) exit(0);
        while(!feof(ptr)){
            fscanf(ptr,"%s %d %s %d\n",name,&age,&address,&number)
            if(strcmp(address,"Pokhara")==0)
            printf("%s %d %s %d\n",name,age,address,number)
        }
        fclose(ptr);
    }

7)WAP to input 10 integers in an array and find the maximum and minimum.

#include<stdio.h>
    void main(){
        int a[10],max,min,i;
        printf("enter 10  numbers\n");
        for(i=0;i<10;i++)
        scanf("%d",&a[i]);
        max = a[0];
        min = a[0];
        for(i=1;i<10;i++){
            if(a[i]>max)
            max = a[i];
            if(a[i]<min)
            min = a[i];
        }
        printf("the max is %d and min is %d",max,min);
    }

8) Create a structure named Employee with data members name,salary and address .take the data of 5 employees and display the number of employee whose salary > 10000.

    #include<stdio.h>
    struct Employee{
        char name[20],address[20];
        int salary;
    }s[5];
    void main(){
        int i;
        printf("enter records of 5 employees\n");
    for(i=0;i<5;i++)
        scanf("%s %s %d",s[i].name,s[i].address,&s[i].salary);
    for(i=0;i<5;i++){
        if(s[i].salary > 10000)
            printf("%s",s[i].name);
        }
    }

 

Leave a Reply

Your email address will not be published.