background img

The New Stuff

 Variables in C


HI Guys Ready For The New C tutorial 3

  • All Variables used In C language Should be declared at Start Of the Function (main Function this rule is specifically for the c language in other languages like c++,java etc it is not necessary to write variable name at start of The function)

    •  To declare a variable means to specify the type of information a 

      variable Supposed to Store.

      Integer -int (% d)where %d is known as format specifier for int values 

      Real or fraction -float,double(%f)Format specifier for float values

      Character-char(%c)where %c is a Format specifier For character(alphabetical)

      All the statement within a language are terminated by semicolon (;).

      because of semicolon rule sometimes c language is knoen as free-form language .

      suppose we now write a program named as abcd.c

      int main()

      {

      int x,y,z;---->declaration of variables at the start of the main function

      x=10;

      y=20;                -->>definition of a function

      z=x+y;

      return o;

      }

      now what does some output come on the screen

      no its not because we haven't read how to print something on screen.

      The solution of this problem is Printf

      Printf-Printf is a Predefined Function.This function can be called to get something printed on monitor.

      printf stands for print as per specified format.


       and %d is Format specifier 

      still the program not compile because we know computer does not understand English language it understand machine language 

      SO wait for c tutorial 4 for knowing how computer then knows to read english


Beginning C tutorial 3-Variables in C

 Variables in C


HI Guys Ready For The New C tutorial 3

  • All Variables used In C language Should be declared at Start Of the Function (main Function this rule is specifically for the c language in other languages like c++,java etc it is not necessary to write variable name at start of The function)

    •  To declare a variable means to specify the type of information a 

      variable Supposed to Store.

      Integer -int (% d)where %d is known as format specifier for int values 

      Real or fraction -float,double(%f)Format specifier for float values

      Character-char(%c)where %c is a Format specifier For character(alphabetical)

      All the statement within a language are terminated by semicolon (;).

      because of semicolon rule sometimes c language is knoen as free-form language .

      suppose we now write a program named as abcd.c

      int main()

      {

      int x,y,z;---->declaration of variables at the start of the main function

      x=10;

      y=20;                -->>definition of a function

      z=x+y;

      return o;

      }

      now what does some output come on the screen

      no its not because we haven't read how to print something on screen.

      The solution of this problem is Printf

      Printf-Printf is a Predefined Function.This function can be called to get something printed on monitor.

      printf stands for print as per specified format.


       and %d is Format specifier 

      still the program not compile because we know computer does not understand English language it understand machine language 

      SO wait for c tutorial 4 for knowing how computer then knows to read english



Syntax Of Writting Functions In C :

Function:A Function Can be defined As A subprogram For a program  .
Before Learning about Functions You  should Learn Some Basics 
[] named as Square Brackets
{} Curly  braces 
() parenthesis

For example open a Notepad File WIth name as abc.c

GO to search then type cmd and then type notepad abc.c(here .c is for the  extension )


Void lmn()

{

.....

....   ---> It is known as Definition of a function

......

}

int main() -->> it is necessary for the Execution of program

{

....
}

According to traditional Turbo c Programming For the main Function Before Main Void comes But 
For the Sake OF portability BEfore Main Int Comes

For the Program At least 1 Function Is Necessary Which is main Function  For the Execution Of the Program

 Now Lets Start With a new Concept Which is Keywords

Can I ask you a Question if you have to name Your Dog From the Words if,you,we,me,to,i then what would you do?
ans.Obcourse not because these are special world of grammer Which are not supposed To give name to anyOne 

Same Case In here For the Programming language Concept There are Some Reserve Words In Programming Language  which Are not supposed To give an Unique Identity to Functions 

They are 

  • auto        else        long           switch
    break       enum        register       typedef
    case        extern      return         union
    char        float       short          unsigned
    const       for         signed         void
    continue    goto        sizeof         volatile
    default     if          static         while
    do          int         struct         _Packed
    double
Suppose You write 
void int()--Wrong
{
}

it won't work as int is a reserve word and it can't used to give identity to Function


Now C is a Case-Sensitive Language

All keywords In c are LowerCase for Eg in Visual C++ thats not a case sensitive thing you can write in lowecase as well as in upper case

We can Give the names to function in lowecase as well as Upper Case But At the time of call 
we Should write What we have GIven name to it
For Eg.
Void Lmn()
{
}
void lmn() /* Uniqueness depends On Spelling as well as Case Sensitive
{
}

Beggining C Tutorial-2 Syntax Of Writting Functions

Syntax Of Writting Functions In C :

Function:A Function Can be defined As A subprogram For a program  .
Before Learning about Functions You  should Learn Some Basics 
[] named as Square Brackets
{} Curly  braces 
() parenthesis

For example open a Notepad File WIth name as abc.c

GO to search then type cmd and then type notepad abc.c(here .c is for the  extension )


Void lmn()

{

.....

....   ---> It is known as Definition of a function

......

}

int main() -->> it is necessary for the Execution of program

{

....
}

According to traditional Turbo c Programming For the main Function Before Main Void comes But 
For the Sake OF portability BEfore Main Int Comes

For the Program At least 1 Function Is Necessary Which is main Function  For the Execution Of the Program

 Now Lets Start With a new Concept Which is Keywords

Can I ask you a Question if you have to name Your Dog From the Words if,you,we,me,to,i then what would you do?
ans.Obcourse not because these are special world of grammer Which are not supposed To give name to anyOne 

Same Case In here For the Programming language Concept There are Some Reserve Words In Programming Language  which Are not supposed To give an Unique Identity to Functions 

They are 

  • auto        else        long           switch
    break       enum        register       typedef
    case        extern      return         union
    char        float       short          unsigned
    const       for         signed         void
    continue    goto        sizeof         volatile
    default     if          static         while
    do          int         struct         _Packed
    double
Suppose You write 
void int()--Wrong
{
}

it won't work as int is a reserve word and it can't used to give identity to Function


Now C is a Case-Sensitive Language

All keywords In c are LowerCase for Eg in Visual C++ thats not a case sensitive thing you can write in lowecase as well as in upper case

We can Give the names to function in lowecase as well as Upper Case But At the time of call 
we Should write What we have GIven name to it
For Eg.
Void Lmn()
{
}
void lmn() /* Uniqueness depends On Spelling as well as Case Sensitive
{
}


Today the Programmers Demands Speed For the Execution Of their Application ThereFore they prefer C/C++ Because It directly Run On the Operating System On The Other hand Java And DOT net Are 

High And Middle Level Languages But It has Performance or Speed Of Execution Problem Because It Does n't Run Directly On the Operating System(It Runs On a Layer )

So We Have A Brief Discussion On the Programming Languages And That's Why We can Start Learning The Taste OF A Revolutionary Language C

Basic Programming is Based On Turbo c and It is good But it is Not Portable(used to write code in for any platform)So We have to use a Compiler That is Based ON ANSI Standard And It gives Portability

Rules Of A Portable C language Based On ANSI Standards:
1.A Program needs To be written Within A file.
2.A program may Contain Several Subprograms(Functions)
3.A Function Contain Set Of Instructions
4.Every Function needs To give A Unique Identity.
5.A program Also needs To given an Unique Identity.
6.Every Program Which is Supposed to be Executed should contain a Function called main Function
7.main Function is necessary For the Execution of a program.
8.We can write main any where in the program.
9.we can now use int main() instead void main() for the portable experience.


Begging C Tutorial 1:What is C and How It comes in competition With other Languages

Today the Programmers Demands Speed For the Execution Of their Application ThereFore they prefer C/C++ Because It directly Run On the Operating System On The Other hand Java And DOT net Are 

High And Middle Level Languages But It has Performance or Speed Of Execution Problem Because It Does n't Run Directly On the Operating System(It Runs On a Layer )

So We Have A Brief Discussion On the Programming Languages And That's Why We can Start Learning The Taste OF A Revolutionary Language C

Basic Programming is Based On Turbo c and It is good But it is Not Portable(used to write code in for any platform)So We have to use a Compiler That is Based ON ANSI Standard And It gives Portability

Rules Of A Portable C language Based On ANSI Standards:
1.A Program needs To be written Within A file.
2.A program may Contain Several Subprograms(Functions)
3.A Function Contain Set Of Instructions
4.Every Function needs To give A Unique Identity.
5.A program Also needs To given an Unique Identity.
6.Every Program Which is Supposed to be Executed should contain a Function called main Function
7.main Function is necessary For the Execution of a program.
8.We can write main any where in the program.
9.we can now use int main() instead void main() for the portable experience.



Printf function questions and answer with solution

Printf objective types interview questions and answers  


(1)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
getch();
}
What will output when you compile and run the above code?
(a)Garbage value garbage value garbage value
(b)5 6 11
(c)11 6 5
(d)Compiler error
Answer: (c)
(2)
#include<stdio.h>
void main()
{
char *str="CQUESTIONBANK";
clrscr();
printf(str+9);
getch();
}
What will output when you compile and run the above code?
(a)CQESTIONBANK
(b)CQUESTION
(c)BANK
(d)Compiler error
Answer: (c)
(3)
#include<stdio.h>
void main()
{
clrscr();
printf("%d",printf("CQUESTIONBANK"));
getch();
}
What will output when you compile and run the above code?
(a)13CQUESTIONBANK
(b)CQUESTIONBANK13
(c)Garbage CQUESTIONBANK
(d)Compiler error
Answer: (b)
(4)
#include<stdio.h>
#include<conio.h>
void main()
{
short int a=5;
clrscr();
printf("%d"+1,a);
getch();
}
What will output when you compile and run the above code?
(a)6
(b)51
(c)d
(d)Compiler error
Answer: (c)
(5)
#include<stdio.h>
void main()
{
int i=85;
clrscr();
printf("%p %Fp",i,i);
getch();
}
What will output when you compile and run the above code?
(a)85 85
(b)0055 034E:0055
(c)0055 FFFF:0055
(d)Compiler error
Answer: (b)
 (6)
#include<stdio.h>
static struct student
{
int a;
    int b;
    int c;
int d;
}s1={6,7,8,9},s2={4,3,2,1},s3;
void main()
{
s3=s1+s2;
clrscr();
printf("%d %d %d %d",s3.a,s3.b,s3.c,s3.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)4321
(c)10101010
(d)Compiler error
Answer: (d)
(7)
#include<stdio.h>
extern struct student
{
int a;
    int b;
    int c;
int d;
}s={6,7,8,9};
void main()
{
clrscr();
printf("%d %d %d %d",s.a,s.b,s.c,s.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (a)
(8)
#include<stdio.h>
struct student
{
static int a;
register int b;
auto int c;
extern int d;
}s={6,7,8,9};
void main()
{
printf("%d %d % %d",s.a,s.b,s.c,s.d);
}
What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (d)
(9)
#include<stdio.h>
struct student
{
int roll;
int cgpa;
int sgpa[8];
};
void main()
{
struct student s={12,8,7,2,5,9};
int *ptr;
ptr=(int *)&s;
clrscr();
printf("%d",*(ptr+3));
getch();
}
What will output when you compile and run the above code?
(a)8
(b)7
(c)2
(d)Compiler error
Answer: (c)
(10)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2={"anil"};
}g3={10,200};
void main()
{
struct game g1=g3;
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?
(a)10 200 anil
(b)200 10 anil
(c)10 200 null
(d)Compiler error
Answer: (d)
(11)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2;
}g1;
void main()
{
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?
(a)Garbage_value garbage_value garbage_value
(b)0 0 (null)
(c)Run time error
(d)Compiler error

Printf function questions and answer with solution

Printf function questions and answer with solution

Printf objective types interview questions and answers  


(1)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
getch();
}
What will output when you compile and run the above code?
(a)Garbage value garbage value garbage value
(b)5 6 11
(c)11 6 5
(d)Compiler error
Answer: (c)
(2)
#include<stdio.h>
void main()
{
char *str="CQUESTIONBANK";
clrscr();
printf(str+9);
getch();
}
What will output when you compile and run the above code?
(a)CQESTIONBANK
(b)CQUESTION
(c)BANK
(d)Compiler error
Answer: (c)
(3)
#include<stdio.h>
void main()
{
clrscr();
printf("%d",printf("CQUESTIONBANK"));
getch();
}
What will output when you compile and run the above code?
(a)13CQUESTIONBANK
(b)CQUESTIONBANK13
(c)Garbage CQUESTIONBANK
(d)Compiler error
Answer: (b)
(4)
#include<stdio.h>
#include<conio.h>
void main()
{
short int a=5;
clrscr();
printf("%d"+1,a);
getch();
}
What will output when you compile and run the above code?
(a)6
(b)51
(c)d
(d)Compiler error
Answer: (c)
(5)
#include<stdio.h>
void main()
{
int i=85;
clrscr();
printf("%p %Fp",i,i);
getch();
}
What will output when you compile and run the above code?
(a)85 85
(b)0055 034E:0055
(c)0055 FFFF:0055
(d)Compiler error
Answer: (b)
 (6)
#include<stdio.h>
static struct student
{
int a;
    int b;
    int c;
int d;
}s1={6,7,8,9},s2={4,3,2,1},s3;
void main()
{
s3=s1+s2;
clrscr();
printf("%d %d %d %d",s3.a,s3.b,s3.c,s3.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)4321
(c)10101010
(d)Compiler error
Answer: (d)
(7)
#include<stdio.h>
extern struct student
{
int a;
    int b;
    int c;
int d;
}s={6,7,8,9};
void main()
{
clrscr();
printf("%d %d %d %d",s.a,s.b,s.c,s.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (a)
(8)
#include<stdio.h>
struct student
{
static int a;
register int b;
auto int c;
extern int d;
}s={6,7,8,9};
void main()
{
printf("%d %d % %d",s.a,s.b,s.c,s.d);
}
What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (d)
(9)
#include<stdio.h>
struct student
{
int roll;
int cgpa;
int sgpa[8];
};
void main()
{
struct student s={12,8,7,2,5,9};
int *ptr;
ptr=(int *)&s;
clrscr();
printf("%d",*(ptr+3));
getch();
}
What will output when you compile and run the above code?
(a)8
(b)7
(c)2
(d)Compiler error
Answer: (c)
(10)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2={"anil"};
}g3={10,200};
void main()
{
struct game g1=g3;
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?
(a)10 200 anil
(b)200 10 anil
(c)10 200 null
(d)Compiler error
Answer: (d)
(11)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2;
}g1;
void main()
{
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?
(a)Garbage_value garbage_value garbage_value
(b)0 0 (null)
(c)Run time error
(d)Compiler error

C programming tutorial for beginners with examples. It is basic online tutorial of c language. You can free download in PDF format. It explains concept of c language by objective questions and explanation.

It is C programming code examples. It has C language basic and simple source code by examples. It has arranged just like c tutorials with examples. This post contains a big collection of examples of c program. If you have any doubt in the following c program examples you are free to ask.

Find out the perfect number using c program





Code 1:
1. C program to check perfect number
#include<stdio.h>
int main(){
  int n,i=1,sum=0;
  printf("Enter a number: ");
  scanf("%d",&n);
  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);
  return 0;
}
Sample output:
Enter a number: 6
6 is a perfect number
Code 2:
1. C program to find perfect numbers
2. C perfect number code
3. Perfect number program in c language
#include<stdio.h>
int main(){
  int n,i,sum;
  int min,max;
  printf("Enter the minimum range: ");
  scanf("%d",&min);
  printf("Enter the maximum range: ");
  scanf("%d",&max);
  printf("Perfect numbers in given range is: ");
  for(n=min;n<=max;n++){
    i=1;
    sum = 0;
    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }
    if(sum==n)
      printf("%d ",n);
  }
  return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6
Code 3:
3. C program to print perfect numbers from 1 to 100
#include<stdio.h>
int main(){
  int n,i,sum;
 
  printf("Perfect numbers are: ");
  for(n=1;n<=100;n++){
    i=1;
    sum = 0;
    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }
    if(sum==n)
      printf("%d ",n);
  }
  return 0;
}
Output:
Perfect numbers are: 6 28




Definition of perfect number or What is perfect number? 

Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number.

Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128




 

Find out the perfect number using c program

C programming tutorial for beginners with examples. It is basic online tutorial of c language. You can free download in PDF format. It explains concept of c language by objective questions and explanation.

It is C programming code examples. It has C language basic and simple source code by examples. It has arranged just like c tutorials with examples. This post contains a big collection of examples of c program. If you have any doubt in the following c program examples you are free to ask.

Find out the perfect number using c program





Code 1:
1. C program to check perfect number
#include<stdio.h>
int main(){
  int n,i=1,sum=0;
  printf("Enter a number: ");
  scanf("%d",&n);
  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);
  return 0;
}
Sample output:
Enter a number: 6
6 is a perfect number
Code 2:
1. C program to find perfect numbers
2. C perfect number code
3. Perfect number program in c language
#include<stdio.h>
int main(){
  int n,i,sum;
  int min,max;
  printf("Enter the minimum range: ");
  scanf("%d",&min);
  printf("Enter the maximum range: ");
  scanf("%d",&max);
  printf("Perfect numbers in given range is: ");
  for(n=min;n<=max;n++){
    i=1;
    sum = 0;
    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }
    if(sum==n)
      printf("%d ",n);
  }
  return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6
Code 3:
3. C program to print perfect numbers from 1 to 100
#include<stdio.h>
int main(){
  int n,i,sum;
 
  printf("Perfect numbers are: ");
  for(n=1;n<=100;n++){
    i=1;
    sum = 0;
    while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
    }
    if(sum==n)
      printf("%d ",n);
  }
  return 0;
}
Output:
Perfect numbers are: 6 28




Definition of perfect number or What is perfect number? 

Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is
1 + 2+ 3 =6

Note: 6 is the smallest perfect number.

Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128




 

Popular Posts