Monday, November 13, 2017

Add/Sum two numbers using pointers in C/C++ Language

1 comments

Problem

Take two integer inputs from user. Create Sum function which should receive two integers using pointers. Calculate the sum and display the result in main function.

Code

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;


int sum (int *, int *);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;
cout<<"enter 2nd number: ";
cin >> b;

c = sum (&a,&b);

cout << "Sum = "<< c << endl;

cout<<"Press any key to close.";

getch();

return 0;
}

int sum (int *a, int *b)
{
int c;
c = (*a + *b);
return c;
}

Output

Add/Sum two numbers using pointers in C/C++ Language
Add/Sum two numbers using pointers in C/C++ Language

Read More..

Saturday, November 4, 2017

Calculate Square of 3 input integers using function and pointers

0 comments

Problem

Write a program to calculate square of an integer, input by the user. Declare a function with the name of square and should receive 3 integers. The function should then calculate the square of these three integers and return the square. Display the result.

Code

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;

void square (int *, int *, int *);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;

cout << "Enter 2nd number: ";
cin >> b;

cout << "Enter 3rd number: ";
cin >> c;

square (&a, &b, &c);

cout << "Square of Ist number = "<< a;
cout << "\nSquare of 2nd number = "<< b;
cout << "\nSquare of 3rd number = "<< c << endl ;


cout<<"Press any key to close.";

getch();

return 0;
}

void square (int *a, int *b, int *c)
{
*a = (*a * *a);
*b = (*b * *b);
*c = (*c * *c);
}

Output

Calculate Square of 3 input integers using function and pointers
Calculate Square of 3 input integers using function and pointers

Read More..

Wednesday, September 17, 2014

program that asks the user to input n number of records that he/she wants to enter by using malloc function, then display the user input values

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 a program that first prompts the user to input the n number of student records that he/she wants to input in that program. Then use malloc function to dynamically allocate memory that has n number of elements (type of each element is struct student). Then input records from user and display them on screen.
Hint:  p = (struct student*) malloc(sizeof (struct student) * n);

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;
 float cgpa;
};
main()
{
 struct student *s;
 int i,how_many;

 printf ("Number of student data you want to enter");
 scanf ("%d",&how_many);

 s = (struct student*) malloc(sizeof(struct student) * how_many);

 for (i=0;i<how_many;i++)
 {
  fflush(stdin);
  printf ("enter name: ");
  gets(&s[i].name);

  printf ("Enter Roll no: ");
  scanf ("%d",&s[i].roll_no);

  fflush(stdin);
  printf ("enter department: ");
  gets (&s[i].department);

  fflush (stdin);
  printf ("enter course: ");
  gets (&s[i].course);

  printf ("enter year: ");
  scanf ("%d",&s[i].year);

  printf ("enter cgpa: ");
  scanf ("%f",&s[i].cgpa);
 }

 printf("\nAnd here is whhat you have entered\n\n");
 for (i=0;i<how_many;i++)
  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");
}

Sample Output


using malloc function

Read More..

Sunday, September 14, 2014

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..

Thursday, September 11, 2014

write text in a file using c language

0 comments

Problem

Write a program that asks a user to enter file name to be created. If file is created successfully then display a message "File created" otherwise display error message and exit from program. Then program asks the user to enter any text to be written on that file. Output should be like:
  1. Enter the name of file: abc.txt
    File created
  2. Enter text for the file
    This is the new file...

Solution - Code

#include<stdio.h>
#include<stdlib.h>
int main ()
{
 char s[80],a[30];
 FILE *fp;

 printf ("Enter file name : ");
 gets (a);

 fp = fopen (a,"w");

 if (fp == NULL)
 {
  printf ("Not found");
  exit (0);
 }

 printf ("\n\nFile Created Successfully");

 printf ("\nNOTE: If you want to exit just press enter twice.\n\n");
 printf ("Enter Text : \n");

 while (strlen(gets(s)) > 0 )
 fputs (s,fp);

 fclose(fp);

 system("pause");
 return 0;
}

Sample Output

writing into file in c language

Read More..

display contents of a file on the screen using fgets function

0 comments

Problem

Write a program that asks the user to input a file name. The program opens that file in read mode and displays it content on the screen. Use fgets function.

Solution - Code

#include<stdio.h>
#include<stdlib.h>
int main ()
{
 char a[10],a1[10],ch;
 FILE *fp,*ft;

 printf ("Enter file name : ");
 gets (a);

 fp = fopen (a,"r");

 if (fp == NULL)
 {
  printf ("Not found");
  exit (0);
 }

 while (1)
 {
  ch = fgetc(fp);
  if ( ch == EOF )
   break;
  printf ("%c",ch);
 }

 printf ("\n");
 system("pause");
}

Sample Output

display file content on screen

Read More..

take input (from user) a poem and write it into a file using puts function

0 comments

Problem

Write a C program that prompt the user to input a poem line by line. The program creates a file named poem.txt and writes that poem in that file. Use fputs function.

Solution - Code

#include<stdio.h>
#include<stdlib.h>

int main()
{
 char s[80];
 FILE *ft;
 int i;

 printf ("NOTE: If you want to exit just press enter twice.\n\n");
 ft = fopen ("Poem.txt","w");

 printf ("Enter Poem : ");
 while (1)
 {

  gets(s);

  if (strlen(s) == 0)
   break;
  else
   fputs(s,ft);
 }

 fclose(ft);
 return 0;
}

Sample Output

input poem using fputs function

Read More..

input characters (entered by the user) into a file using fputc function

0 comments

Problem

Write a program that will take characters from the keyboard, one at a time, and writes them to a file. Use fputc function.

Solution - Code

#include<stdio.h>
#include<stdlib.h>

int main()
{
 char ch;
 FILE *ft;
 int i;

 printf ("NOTE: If you want to exit, just press 1 and hit enter.\n\n");
 ft = fopen ("P3.txt","w");

 while (1)
 {
  printf ("Enter Character : ");
  scanf("%c",&ch);
  fflush (stdin);
  if ( ch == '1' )
   break;
  else
   fputc (ch,ft);
 }

 fclose (ft);
 system ("pause");
 return 0;
}

Sample Output

write character by character into a file

Read More..

create a copy of a file using fgetc and fputc functions

0 comments

Problem

Write a program that creates a copy of a file. The program first asks the user to input the "first file name" whose copy should be created and "second file" name for copy file. Make sure that file exist with the name "first file name" in the same folder where your C program is present. Use fgetc and fputc functions.

Solution - Code

#include<stdio.h>
#include<stdlib.h>

int main()
{
 char a[10],a1[10],ch;
 FILE *fp,*ft;

 printf ("Enter file name : ");
 gets (a);

 fp = fopen (a,"r");

 if (fp == NULL)
 {
  printf ("Not found");
  exit (0);
 }

 printf ("Enter file name : ");
 gets (a1);

 ft = fopen (a1,"w");
 if (ft == NULL)
 {
  printf ("Error in opening");
  exit (0);
 }

 while (1)
 {
  ch = fgetc(fp);
  if ( ch == EOF )
   break;
  else
   fputc (ch,ft);
  printf ("%c",ch);
 }

 fclose (fp);
 fclose (ft);

 system ("pause");
 return 0;
}

Sample Output

copy file using fgetc and fputc functions

Read More..

display c source file on the console using fgetc function

0 comments

Problem

Write a C program to open any C source file and displays its content on console. Use fgetc function.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main ()
{
 FILE *fp;
 char ch;

 fp = fopen ("p2.txt","r");

 while (1)
 {
  ch = fgetc(fp);
  if ( ch == EOF )
   break;

  printf ("%c",ch);
 }

 fclose (fp);
 system ("pause");
 return 0;
}

Sample Output

display source file using fgetc function

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..

determine the number of characters in a string

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function.The function then counts the number of characters in that string and displays it to the user.

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

Solution - Code

#include<stdio.h>
#include<conio.h>
int mystrlen(char *p);
main()
{
 int count;
 char p[]="Honesty is the best policy.";
 count = mystrlen(p);
 printf("\n\nNumber of characters in the string = %d\n\n",count);
 system("pause");
}
int mystrlen(char *p)
{
 printf ("\n");
 int count = 0;
 while (*p != '\0' )
 {
  printf ("%c",*p);
  p++;
  count++;
 }
 return (count);
}

Sample Output


number of characters in a string

Read More..

print a string in a user defined function, where string is declared in main function

0 comments

Problem

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

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p);
main()
{
 char p[]="Mr.Maaz Ali is a good boy.";
 display(p);
 system("pause");
}
void display (char *p)
{
 printf("\n%s\n\n",p);
}

Sample Output

print string in a function, defined in main

Read More..

Friday, September 5, 2014

display all elements of an array, use pointers to find maximum and minimum element in an array.

0 comments

Problem

Write three user define functions:
  1. void display(int *p, int size);
    The first parameter is the base address of an array and the second parameter is the size of array
    (No of elements in an array). The function displays all the elements in the array. Call this
    function from main function.
  2. int max_value (int *p, size);
    this function returns the maximum element of the array pointed by the pointer p. second
    parameter specify the size of array.
  3. int min_value (int *p, size);
    this function returns the minimum element of the array pointed by the pointer p. second
    parameter specify the size of array.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display(int *,int);
int max_value (int *,int);
int min_value (int *,int);
main()
{
 int max,min,size,i,x,sum=0,avg;
 int num[5];
 size = 5;
 display(num,size);
 max = max_value (num,size);
 printf ("Maximum element : %d\n",max);
 min = min_value (num,size);
 printf ("Minimum element : %d\n",min);
 system("pause");
}
void display(int *p,int size)
{
 int i,num[size],sum=0,avg;
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  sum = sum + num [i];
 }
 avg =(sum/5);
 printf ("sum = %d\n",sum);
 printf ("average = %d\n",avg);
}
int max_value (int *p,int size)
{
 int num[size];
 int i,max=num[0];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  if (max < num[i])
  max=num[i];
 }
 return max;
}
int min_value (int *p,int size)
{
 int num[size];
 int i,min=num[4];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=5;i>=0;i--)
 {
  if (min > num[i])
  min=num[i];
 }
 return min;
}

Sample Output

find maximum and minimum element c language

Read More..

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