-->

Friday, April 3, 2020

off topics tips/tricks

1) understanding about malloc() function:

                                                                                                                    solution

                                                                                                                    solution


2)to understand free() function

                                                                                                                  solution

3)WAP to understand malloc() using struct pointer.

                                                                                                                   solution

                                                                                                                   solution

program to understand about malloc() function

//understanding malloc()
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *x ;
    x= (int*)malloc(sizeof(int));//allocates the memory(4 bytes) to heap
    if(x==NULL)//tests whether the memory is available
    {
        printf("error in allocating");//displays error if no memory
    }
    else
    {
        *x=10;//assigns value
    printf("%d\n", *x);//prints value 10
    }
    return 0;
}

Monday, January 20, 2020

WAP to input a string and to print its address(character's) with its value.

//progrm to print address and value of a string given by user.
#include <stdio.h>
int main()
{
    char name[30];
    char *p;
    int i=0;
    printf("enter the string\n");
    scanf("%s",name);
    p=name;//assigning base address
    while(*p!='\0')//condition testing
    {
    printf("the address=%d and value=%c\n",p+i,*p);//printing value and address
   *p++;//changing the value
    i++;//changing the value of i
    }
 

    return 0;
}

Wednesday, January 1, 2020

python program to print sum of two numbers 3 and 4

type following code in IDLE.
-----------------------------------------
#program to print sum of two numbers 3 and 4 #comment of program

a=3   #assigning value to a variable
b=5    #assigning value to variable
sum=a+b#forumaltion
print("the sum of two numbers is=\n",sum)#printing the sum
print("the sum of numbers",a,"and",b,"is",sum)#we can print values by putting comma





python program to print hello python with single line comment

type following code in IDLE
------------------------------------------------------------------

#program to print python #comment of program

print("hello python")   #prints the string hello python

python program to print hello python with comment

"""
first program in python  #multi line comment using triple quotes
"""
print("hello python")   #prints the string hello python

Saturday, December 28, 2019

WAP to enter student id, student name and address for any 10 students. Then search a data and its location.

in codeblocks:
--------------------------------------------------------------------------------------------------
// program to search data in given  detail of some  students       //comment abt. program
#include <stdio.h>                                 //header file
#include<string.h>
struct data                                        //struct tag named data
{
    char name[80];                                //member1 of struct
    char address[80];                             //member2 of struct
    int roll;                                     //member3 of struct
    int english,maths,physics,chemistry,nepali,total;   //marks member declaration
    float percentage;                       //member for total and percentage

}access[100];                 //'access' is a variable to access members. This can hold maximum 100 members.it is called array of struct.
int main()
{
   int n,i,loc=0;                                         //variable declaration
   char naam[70];
    printf("enter total value of n\n");
   scanf("%d",&n);                                  //accepts total value
   for(i=0;i<n;i++)
   {                                                 //execution of loop with inputs
    puts("enter roll number \n");
   scanf("%d",&access[i].roll);                      //gets data in different member
   puts("enter name \n");
   scanf("%s",access[i].name);                       //gets name
   puts("enter address \n");
   scanf("%s",access[i].address);                    //gets address
   printf("enter english marks\n");
   scanf("%d",&access[i].english);                  //gets respective marks
   printf("enter maths marks\n");
   scanf("%d",&access[i].maths);
   printf("enter Physics marks\n");
   scanf("%d",&access[i].physics);
   printf("enter chemistry marks\n");
   scanf("%d",&access[i].chemistry);
   printf("enter nepali marks\n");
   scanf("%d",&access[i].nepali);
   access[i].total=access[i].english+access[i].maths+access[i].physics+access[i].chemistry+access[i].nepali;//finds total and stores
      access[i].percentage=(float)access[i].total/5;               //finds percentage and stores in respective member
   }
   printf("entered data are\n");                  //loop execution to display data
   for(i=0;i<n;i++)
   {
   printf("roll=%d,name=%s,address=%s, english=%d,maths=%d,physics=%d,chemistry=%d,nepali=%d\n",access[i].roll,access[i].name,access[i].address,access[i].english
          ,access[i].maths,access[i].physics,access[i].chemistry,access[i].nepali);
   }                                                //displays data

   printf(" data with total and percentage are\n");     // displays data with total and percentage
  for(i=0;i<n;i++)
   {
   printf("roll=%d,\tname=%s,\taddress=%s,\tenglish=%d,\tmaths=%d,\tphysics=%d,\tchemistry=%d,\tnepali=%d,\ttotal=%d,\tpercentage=%f\n",access[i].roll,access[i].name,access[i].address,access[i].english
          ,access[i].maths,access[i].physics,access[i].chemistry,access[i].nepali,access[i].total,access[i].percentage);
   }
   printf("to be searched is\n");
   scanf("%s",naam);         //data input to be searched
   for(i=0;i<n;i++)
   {
       if(strcmp(naam,access[i].name)==0)//data comparison
        {
           loc=1;
           break;                    //terminating the loop
        }
   }
   if(loc==1)
   {
       printf("data found and location=%d",i);//printing the location of data
       printf("and data are,roll=%d,\tname=%s,\taddress=%s,\tenglish=%d,\tmaths=%d,\tphysics=%d,\tchemistry=%d,\tnepali=%d,\ttotal=%d,\tpercentage=%f\n",access[i].roll,access[i].name,access[i].address,access[i].english
          ,access[i].maths,access[i].physics,access[i].chemistry,access[i].nepali,access[i].total,access[i].percentage);
")
   }
   else
   {
       printf("not found\n");
   }

    return 0;
}