Wednesday, September 17, 2014

write a structure for student data, also define functions to display and search that data

0 comments

Problem

Student structure contains the following contents
Roll number (int), Name (string), Department (string), Course (string), Year of joining (string), cgpa (float)
Write the definition of the functions whose prototype is given below:
  1. void display(struct strudent s);
    This function display the content of struct student s
  2. void display(struct strudent *p);
    This function displays the content of a structure pointed by pointer *p
  3. void search(struct strudent *p, int roll_no);
    The first parameter is the base address of an array of type struct student and second parameter is the roll no of the structure. This function search's the record from array of structure (first parameter) whose Roll number is same as roll_no (second parameter)

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
}s[5];
int i;
void searchstudent (struct student *, int );
void disp();
void display (struct student *);
main()
{
 int rl,p;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
 }

 disp();
 display(s);

 printf ("Enter number you want to search : ");
 scanf("%d",&rl);

 searchstudent (s ,rl);

 system("pause");
}
//************
void disp()
{
 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered.\n\n");
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);
 }
}
//************************
void display (struct student *p)
{
 printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
}
void searchstudent (struct student *p, int roll_no)
{
 int i,found = 0;
 for(i=0;i<5;i++)
 {
  if(p->roll_no == roll_no)
  {
   printf("record found");
   printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
   found = 1;
  }
 }
 if (found == 0)
  printf("Not found");
}

Sample Output


display & search data using structure

Read More..

Sunday, September 14, 2014

write a program that allows the user to input data and search

0 comments

Problem

Create a structure to specify data on students given below:
Roll number, Name, Department, Course, Year of joining
Create an array of structure. Use a loop to input values and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void searchstudent (int roll_no);
struct book
{
 int roll;
 char name[20];
 char department[20];
 char course[20];
 int year;
}b[2];
int main ()
{
 int i, roll_no;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

 printf ("SEARCH DATA(enter roll number): ");
 scanf ("%d",&roll_no);

 searchstudent(roll_no);

 printf ("\npress any key to close.");
 getch();
}
void searchstudent (int roll_no)
{
 int i;

 for (i=0;i<2;i++)
 {
  if (roll_no == b[i].roll)
   printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);
 }
}

Sample Output

search of a student using structure

Read More..

create an array of structure to display user input values

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create an array of that structure. Use a loop to input values and then display those values.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int roll;
char name[20];
char department[20];
char course[20];
int year;
}b[2];
int main ()
{
 int i;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

 printf ("\npress any key to close.");
 getch();
}

Sample Output

using array of structure to display user input data

Read More..

Display the user input names by allocating memory dynamically (malloc function)

0 comments

Problem

Write a program that asks the user to input name of students and then displays these names on computer screen. You have to write a program such that it should work with any numbers of names. e.g. users can input 6 names or 100 names.
  1. For such problems declare an array of pointers to characters.
  2. Input a name from user in a temp array of size 50 using gets( ) function. Then allocate a dynamic memory to store that name and assign the base address of dynamic memory to the first index of array of pointers.   
  3. Do the processes in b again and again until the user input an empty string.
  4.  Then displays all the names using array of pointers

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
int main ()
{
 char *name[30];
 char temp[30];
 int i, k;

 printf ("Enter names: ");

 for (i=0;i<1000;i++)
 {
  fflush(stdin);

  gets (temp);

  if (strlen(temp) != 0)
  {
   name[i] = (char *) malloc (strlen(temp)+1);
   strcpy (name[i],temp);
  }
  else
   break;
 }

 printf ("\n\nNames that you have entered are: ");

 for (k=0;k<i;k++)
 {
  printf ("\n%s",name[k]);
 }

 printf ("\n\npress any key to close.");
 getch();
}

Sample Output

dynamic memory allocating using malloc function

Read More..

write a program to calculate sum and average by allocating dynamic memory (malloc function)

0 comments

Problem

Write a program that asks the user to input marks of some subjects. The program calculates sum and average of those marks and prints them on screen. As a programmer you are not sure that a user wants to input marks of how many subjects. So, your program first asks the user for how many subject you want to input marks. The program allocates dynamic memory using malloc function.
Prototype of malloc function is:
void* malloc (int n);

where n is the number of bytes to be allocated you have to type cast void* pointer to int* since you know that you will be storing int values in that allocated memory.

Note: Use free function when the program ends.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
int main ()
{
 int sum=0, avg, i;
 int how_many;
 int *p;

 printf ("Enter number of subjects whose marks you want to input: ");
 scanf ("%d",&how_many);

 p = (int *)malloc(how_many * 4);

 if (p == NULL)
 {
  printf(" Out of memory!\n");
  printf ("Press any key to close.");
  exit(0);
 }

 for (i=0;i<how_many;i++)
 {
  printf ("Enter marks of %dth subject: ",i+1);
  scanf ("%d",&p[i]);

  sum = (sum + p[i]);
 }

 avg = (sum / how_many);

 printf ("Sum = %d\n\n",sum);
 printf ("Average = %d\n\n",avg);

 free (p);

 printf ("press any key to close.");
 getch();
}

Sample Output


calculate sum & average using malloc function

Read More..

Tuesday, September 9, 2014

compare two dates input by the user, using structure

0 comments

Problem

Write a program that compares two given dates. To store date use structure say date that contains three members namely date, month and year. If the dates are equal then display message as "Equal" otherwise "Unequal".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct dt_cmp
{
 int day;
 int month;
 int year;
};
main()
{
 struct dt_cmp s[2];
 int i;

 for (i=0;i<2;i++)
 {
  printf ("Enter day : ");
  scanf ("%d",&s[i].day);
  printf ("Enter month : ");
  scanf ("%d",&s[i].month);
  printf ("Enter year : ");
  scanf ("%d",&s[i].year);
 }

 if ( (s[0].day == s[1].day) && (s[0].month == s[1].month) && (s[0].year == s[1].year) )
  printf ("Equal\n");
 else
  printf ("Unequal\n");

 system("pause");
}

Sample Output

compare dates using structres

Read More..

search for a user (student) input data by using an array of structure, add user roll number, name, department, course, and year of joining by declaring a function name searchstudent(int)

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create above program by declaring an array of structure., input some value from user and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
};
void searchstudent (struct student *, int );

int main()
{

 struct student s[5];
 int i,rl,p;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
 }

 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered\n\n");
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);
 }

 printf ("Enter number you want to search : ");
 scanf("%d",&rl);

 searchstudent (s ,rl);

 system("pause");
}
void searchstudent (struct student *p, int roll_no)
{
 int i,found = 0;
 for(i=0;i<5;i++)
 {
  if(p->roll_no == roll_no)
  {
   printf("record found");
   printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
   found = 1;
  }
  if (found == 0)
   printf("Not found");
 }

}

Sample Output

search user roll# name using array of structure

Read More..

Display user input data by using an array of structure, add user roll number, name, department, course, and year of joining

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create above program by declaring an array of structure., input some value from user and then display those values.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct stud
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
}
int main()
{
 struct stud s[5];
 int i;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
}

 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered\n\n");
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);
 }

 system("pause");
 return 0;
}

Sample Output

display user roll# name using array of structure

Read More..

Sunday, September 7, 2014

find how many times a character appears in a string

0 comments

Problem

write a c program that will ask the user to input a character and then the program will check whether the character (how many times) exists in the following string or not.

Note: use pointers to find the character in the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int countchar(char *p, char a);
main()
{
 int count;
 char a;
 char p[]="OMG! its hot.";
 printf("Enter the character : ");
 scanf("%c",&a);
 count = countchar(p,a);
 printf("\n\nCharacter %c occured in the array %d times.\n",a,count);
 system("pause");
}
int countchar(char *p, char a)
{
 int count=0;
 while (*p != '\0')
 {
  if (a == *p)
   count++;
  p++;
 }
 return (count);
}

Sample Output


find character in a string

Read More..

print a string character by character in a user defined function where the string is declared in main

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function and print the string in that function character by character.

Note: use pointers to print the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p, char end);
main()
{
 char p[]="We are gonna be the best programmers ever.";
 char end= '\0';
 display(p,end);
 printf ("\n\n",*p);
 system("pause");
}
void display(char *p, char end)
{
 printf ("\n",*p);
 while (*p != end)
 {
  printf ("%c",*p);
  p++;
 }
}

Sample Output


display string character by character

Read More..

Friday, September 5, 2014

check if the number is in the array or not

0 comments

Problem

Write a program that asks the user to input 10 integers and stored them in an array and then again asks user to input any single number that store in variable “Num” and program check whether the Num is in the array or not and then print "Number “Num” is in the array" or “Num” is not in the array".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int arr[10];
 int i,num;
 for (i=0;i<10;i++)
 {
  printf ("Enter number : ");
  scanf ("%d",&arr[i]);
 }
 printf("\nEnter number to check: ");
 scanf ("%d",&num);
 for (i=0;i<10;i++)
 {
  if (num==arr[i])
  {
   printf ("Entered number is in the array\n");
   break;
  }   if (i == 9)
   printf ("Entered number is not in the array\n");
 }
 system("pause");
}

Sample Output

check if number exists in an array

Read More..

how much money will you receive on day 15 if you are given 0.20 paisa's on 1st day and double on each day

0 comments

Problem

Suppose you are given ten paisa on day 1 and on day 2 you are given twice as much. If each day you are given twice as much money as on the previous day, then on day 15, how much money you will receive? Build a C program to find the solution.
Total money for day 1 is Rs. 0.20
Total money for day 2 is Rs. 0.40
Total money for day 3 is Rs. 0.80
Total money for day 4 is Rs. 1.60
………….

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int i;
 float x=0.20;
 printf("Total money for day 1 = Rs.0.20\n");
 for(i=1;i<=15;i++)
 {
  x = (x*2);
  printf("Total money for day %d = %f\n",i,x);
 }
 system("pause");
}

Sample Output

calculate amount for day 15


Read More..

Tuesday, September 2, 2014

print the following pattern on the screen (console)

0 comments

Problem

Write a program to generate the following pattern on screen.
A
A B
A B C
A B C D
A B C D E
A B C D E F

Solution - Code

#include<stdio.h>
int main()
{
 int i,j;
 for(i=1;i<=6;i++)
 {
  char ch='A';
  for(j=1;j<=i;j++)
  {
   printf("%c",ch);
   ch++;
  }
  printf("\n");
 }
 system("pause");
 return 0;
}

Sample Output


print pattern c language

Read More..

print the following combination on the screen using for loop

0 comments

Problem

Create a program that prints the following combination on the screen (console) using for loop.
1 1
1 2
2 1
3 2

Solution - Code

#include<stdio.h>
int main()
{
 int i,j;
 for(i=1;i<=3;i++)
 {
  for(j=1;j<=2;j++)
  {
   if(i==2 && j==2 || i==3 && j==1)
   continue;
   else
   {
    printf("%d%d\n",i,j);
   }
  }
 }
 system("pause");
 return 0;
}

Sample Output

combination using loop c language

Read More..

print the following pattern on the screen using loops

0 comments

Problem

create a c language program to generate following pattern on the console (screen) using for loop.
*
* *
* * *
* * * *
* * * * *

Solution - Code

#include<stdio.h>
int main()
{
 int i,j;
 i=1;
 j=1;
 for(i=1;i<=5;i++)
 {
  for(j=1;j<=i;j++)
  {
   printf("*");
  }
  printf("\n");
 }
 system("pause");
 return 0;
}

Sample Output


Read More..

Program that prints multiplication table of a number (multiplicand and multiplier input by the user)

1 comments

Problem

Create a program that prints the multiplication table of a number entered by the user (user enter x and y and your program will print the table of x upto y). The table should get displayed in the following form.
2* 1 = 2
2* 2 = 4

Solution - Code

#include<stdio.h>
int main()
{
 int x,y,i;
 printf("Enter x and y: ");
 scanf("%d%d",&x,&y);
 i=1;
 while(i<=y)
 {
  printf("%d*%d=%d\n",x,i,x*i);
  i = i + 1;
 }
 system("pause");
 return 0;
}

Sample Output


Read More..

Generate all possible combinations of 1, 2 and 3 (using for loop)

2 comments

Problem

create a program that generates all possible combinations of 1, 2 and 3 using for loop.

Solution - Code

#include<stdio.h>
int main()
{
 int i,j,k;
 for(i=1;i<=3;i++)
 {
  for(j=1;j<=3;j++)
  {
   {
   for(k=1;k>=3;k++)
    if(j==k)
    continue;
    printf("%d%d%d\n",i,j,k);
   }
   if(i==j)
   continue;
   printf("%d%d%d\n",i,j,k);
  }
 }
 system("pause");
 return 0;
}

Sample Output


generate combinations c language

Read More..

ask user to input starting number and ending number with incremental number and display the numbers between them

0 comments

Problem

write a program which prompts the user for 3 inputs (shown below - determines how and what to count). The user’s answers should be stored in variables. Your counting program should make use of the for loop.
  • Ask the user to enter beginning number to start counting from
  • Ask the user to enter ending number to stop counting at
  • and also ask the user to enter the incremental number

Solution - Code

#include<stdio.h>
int main()
{
 int i,x,y,z;
 printf("Enter beginning and ending number: ");
 scanf("%d%d",&x,&z);
 printf("Enter increment = ");
 scanf("%d",&y);
 for(i=x;i<=z;i=i+y)
 {
  printf("%d\n",i);
 }
 system("pause");
 return 0;
}

Sample Output


starting and ending number with incremental number in c language

Read More..

Monday, September 1, 2014

print ASCII characters in c language using while loop

0 comments

Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.

Code

#include<stdio.h>
int main()
{
int i,x;
i=0;
while(i<=255)
{
printf("%c\t",i);
i=i+1;
}
system("pause");
return 0;
}

Sample Output

print ASCII characters in c language

Read More..

print hearts and faces on the entire screen using loops

0 comments

Write the program to fill the entire screen by printing smiling face and heart alternatively. The smiling face has an ASCI value 1and heart has 3.

Code

#include<stdio.h>
int main()
{
int i,x,y;
x=1;
y=3;
i=1;
while(i<=1000)
{
printf("%c%c",x,y);
i=i+1;
}
system("pause");
return 0;
}

Sample Output

print face/heart c language

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact