Showing posts with label c programs. Show all posts
Showing posts with label c programs. Show all posts

Tuesday, 26 August 2014

,

C program to Display Factors of a Number

C program to Display Factors of a Number : This program takes a positive integer from an user and displays all the factors of that number.


C program to Display Factors of a Number


C program to Display Factors of a Number



#include <stdio.h>
int main()
{
  int n,i;
  printf("Enter a positive integer: ");
  scanf("%d",&n);
  printf("Factors of %d are: ", n);
  for(i=1;i<=n;++i)
  {
      if(n%i==0)
         printf("%d ",i);
  }
  return 0;
}



Output

Enter a positive integer:50
Factors of 50 are: 2 5 5

Explanation

In this program, an integer entered by user is stored in variable n. Then, for loop is executed with initial condition i=1 and checked whether n is perfectly divisible by i or not. If n is perfectly divisible by i then, i will be the factor of n. In each iteration, the value of i is updated(increased by 1). This process goes not until test condition i<=n becomes false,i.e., this program checks whether number entered by user is perfectly divisible by all numbers from 1 to n and all displays factors of that number.

Publisher: Anand - 10:55
,

C Program to Calculate Difference Between Two Time Period

In this program, user is asked to enter two time periods and these two periods are stored in structure variables. This program calculates the difference between these two time period. To perform this task, a function is created which calculates the difference and the result is displayed in main( ) function without returning it (Using call by reference).
C Program to Calculate Difference Between Two Time Period

C Program to Calculate Difference Between Two Time Period


#include <stdio.h>
struct TIME{
  int seconds;
  int minutes;
  int hours;
};
void Difference(struct TIME t1, struct TIME t2, struct TIME *diff);
int main(){
    struct TIME t1,t2,diff;
    printf("Enter start time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d%d%d",&t1.hours,&t1.minutes,&t1.seconds);
    printf("Enter stop time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d%d%d",&t2.hours,&t2.minutes,&t2.seconds);
    Difference(t1,t2,&diff);
    printf("\nTIME DIFFERENCE: %d:%d:%d - ",t1.hours,t1.minutes,t1.seconds);
    printf("%d:%d:%d ",t2.hours,t2.minutes,t2.seconds);
    printf("= %d:%d:%d\n",diff.hours,diff.minutes,diff.seconds);
    return 0;
}
void Difference(struct TIME t1, struct TIME t2, struct TIME *differ){
    if(t2.seconds>t1.seconds){
        --t1.minutes;
        t1.seconds+=60;
    }
    differ->seconds=t1.seconds-t2.seconds;
    if(t2.minutes>t1.minutes){
        --t1.hours;
        t1.minutes+=60;
    }
    differ->minutes=t1.minutes-t2.minutes;
    differ->hours=t1.hours-t2.hours;
}

Output

C Program to Calculate Difference Between Two Time Period

Publisher: Anand - 10:38
,

C Program to Store Information of a Student Using Structure

In this program, a structure(studentinfo) is created which contains name, roll and marks as its data member. Then, a structure variable( s ) is created. Then, data (name, roll and marks) is taken from user and stored in data members of structure variable s. Finally, the data entered by user is displayed.


C Program to Store Information of a Student Using Structure

C Program to Store Information of a Student Using Structure



#include <stdio.h>
struct studentinfo
{
    char name[50];
    int roll;
    float marks;
};
int main(){
    struct studentinfo std;
    printf("Enter information of students:\n\n");
    printf("Enter name: ");
    scanf("%s",std.name);
    printf("Enter roll number: ");
    scanf("%d",&std.roll);
    printf("Enter marks: ");
    scanf("%f",&std.marks);
    printf("\nDisplaying Information\n");
    printf("Name: %s\n",std.name);
    printf("Roll: %d\n",std.roll);
    printf("Marks: %.2f\n",std.marks);
    return 0;
}



Output

C Program to Store Information of a Student Using Structure

Publisher: Anand - 09:05
,

C program to Calculate the Power of a Number

C program to Calculate the Power of a Number :This program below takes two integers from user, a base number and an exponent. For example: In case of 23, 2 is the base number and 3 is the exponent of that number. And this program calculates the value of baseexponent.

C program to Calculate the Power of a Number


C program to Calculate the Power of a Number


#include <stdio.h>
int main()
{
  int base, exp;
  long long int value=1;
  printf("Enter base number and exponent respectively: ");
  scanf("%d%d", &base, &exp);
  while (exp!=0)
  {
      value*=base;  /* value = value*base; */
      --exp;
  }
  printf("Answer = %d", value);
}

Output

Enter base number and exponent respectively:2 4
 
Answer = 16

Explanation
This program takes base number and exponent from user and stores in variable base and exp respectively. Let us suppose, user entered 2 and 4 respectively. Then,while loop is executed with test condition exp!=0. This test condition will be true and value becomes 2 after first iteration and value of exp will be decreased to 2. This process goes on and after fourth iteration, value will be equal to 16(2*2*2*2) and exp will be equal to 0 and while loop will be terminated. Here, we have declared variable value of type long long int because power of a number can be very large.
This program can only calculate the power if base power and exponent are integers. If you need to the calculate power of a floating point number then, you can use pow() function
Publisher: Anand - 06:46

C Program to Access Elements of an Array Using Pointer

Here is simple C program is given which access the element of an array using pointer.This program accept five integer value and access  these value by using pointer and print these value on output screen.

C Program to Access Elements of an Array Using Pointer

C Program to Access Elements of an Array Using Pointer




#include <stdio.h>
int main()
{
   int val[5], i;
   printf("\nEnter elements: ");
   for(i=0;i<5;++i)
     scanf("%d",val+i);
   printf("\nYou entered: ");
   for(i=0;i<5;++i)
      printf("%d\n",*(val+i));
   return 0;
}


Output

 Enter elements: 23 43 54 55 21
 You entered: 23 43 54 55 21
 
Publisher: Anand - 05:57

Monday, 25 August 2014

,

C Program to Accept Paragraph using scanf

C Program to Accept Paragraph using scanf: I f  you think that scanf does not accept paragraph then your are wrong,because here we learn a way from which we can insert paragraph with space.

C Program to Accept Paragraph using scanf


C Program to Accept Paragraph using scanf

 
 #include<stdio.h>
 
int main() {
   char pgraph[400];
 
   printf("Enter Paragraph : ");
   scanf("%[^\t]s", pgraph);
 
   printf("Accepted Paragraph : %s", pgraph);
 
   return 0;
}

Note: Press Tab to Stop Accepting Characters

Output :
 Enter Paragraph :Geek Coderz is a blog which covers the stuff related to progr amming languages and tutorials. 
Accepted Paragraph : Geek Coderz is a blog which covers the stuff related to pr ogramming languages and tutorials. 


Explanation :


    scanf("%[^\t]s", pgraph);

  1. Here scanf will accept Characters entered with spaces.
  2. It also accepts the Words , new line characters .
  3. %[^\t]s  represent that all characters are accepted except tab(t) whenever t will encountered then the process of accepting characters will be terminated.
Drawbacks :
  1. Paragraph Size cannot be estimated at Compile Time
  2. It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow ?
// Accept only 100 character
 scanf("%10[^\t]s", pgraph);
Publisher: Anand - 20:12
,

C Program to Add two numbers without using arithmetic Operators

C Program to Add two numbers without using arithmetic Operators

C Program to Add two numbers without using arithmetic Operators

Program 1 : Using Recursive Function


#include<stdio.h>
int add(int, int);
int main()
{
   int num1, num2; 
   printf("\n ");
   scanf("%d %d", &num1, &num2); 
   printf("\nAddition of two number is : %d", add(num1, num2));
   return (0);
}
int add(int num1, int num2)
{
   if (!num1)
      return num2;
   else
      return add((num1 & num2) << 1, num1 ^ num2);

}
Output: 
Enter the two Numbers : 12 43
Addition of two number is : 55

program 2 : Using While Loop

 #include<stdio.h>
int main() {
   int num1 = 20, num2 = 8, i;
   while (num2 > 0) {
      num1++;
      num2--;
   }
   printf("%d", num1);
   return (0);
}


Output: 

Sum is :28

program 3 : Using While Loop

  #include<stdio.h>
int main() {
    int num1 = 20, num2 = 8, i;
    while (num2--) {
        num1++;
    }
    printf("Sum is : %d", num1);
    return (0);
}

Output: 
Sum is :28
program 4 : Using for Loop

  #include<stdio.h>
int sum(int, int);
int main() {
   int a, b;
   printf("Enter the two Numbers: ");
   scanf("%d %d", &a, &b);
   printf("Addition of two number is : %d", add(a, b));
   return(0);
}
int add(int num1, int num2) {
   int i;
   for (i = 0; i < num2; i++)
      num1++;
   return num1;
}

Output
 Enter the two Numbers:32 45
Addition of two number is :77













Publisher: Anand - 19:41