Friday, May 8, 2015

C++


Write to read name of 10 student & display them in dictionary order.

#include<stdio.h>
#include<conio.h>
                Main()
{
                Char a [10][20], t[20];
                Int I;
                /* Reading 10 string */
                For (i=0; i<10; i++)
                {
                Printf(“in enter %d string”, i+1);
                Gets(a[i]);
                }
                For (i=0; i<9; i++)
                {
                For (j=i+1; J<10; j++)
                {
                If (stremp (a[i], a[j]) >0)
                {
                Strcpy(t, a[i]);
                Strcpy(a[i], a[j]);
                Strcpy(a[j], t);
                }
                }
]



Read name roll no, total marks of a student & display.
                Struct s
                {
                Char name [50];
                Int roll no;
                Float marks[50];
                }stu;
                Main()
                {
                Printf(“in enter name”);
                Gets(Stu.name);
                Printf(“\n enter roll no”);
                Scanf(“%d” , & enter roll no);
                Printf(“\n enter percentage”);
                Scanf(“%f”, &stu. Per);

                /* display  data */
               
                Printf (“in name= %C, roll no =% d, percentage =%f, stu.name, stu,=.roll no, stu.per);
                Getch();
                }




Copying  a structure.

Write a program to reads name roll no. marks of 5 subject of a student copy to the next structure variable & display.

#include<stdio.h>
#include<conio.h>
Struct s
{
Char name [50];
Int roll no;
Float m[5];
}
Main ()
{
Struct s stu, b;
Int i;
Printf(“enter name”);
Gets(stu.name);
Printf(“enter roll no”);
Scanf(“%d”, & stu.roll no);
For(i=0; i<5; i++)
{
Printf(“in enter % d subject mark”, i+1);
Scanf(“%f”, stu. M[i]);
}

/* copying structure */

  B=stu;
Printf(“in name =%s”, b.name);
Printf(“in roll no=%d”,b.rollno);
For (i=0; i<5; I++)
Printf(“in %f”, b.m[i])
}







Structure Initialization
  Struct student
{
Char name[20];
Int rollno;
Float marks;
};

Struct student st{“Harry”, 10, 56.5};
This line is equivalent to
                Struct student st;
                St. name =’Harry”;
                St. rollno = 100;

                St. ,marks = 56.5;

No comments:

Post a Comment