0% found this document useful (0 votes)
59 views

C Programming: Coding

This document contains 20 questions and answers related to C programming concepts like input/output, arithmetic operations, conditional statements, loops, strings, and functions. The questions cover basic topics like calculating sums and averages, finding greater of three numbers, checking eligibility to vote, and printing odd/even numbers using for loops. More advanced topics include switch statements to print days/months, string handling, and comparing two strings. Well-structured code snippets are provided for each question to demonstrate the concept.

Uploaded by

joker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

C Programming: Coding

This document contains 20 questions and answers related to C programming concepts like input/output, arithmetic operations, conditional statements, loops, strings, and functions. The questions cover basic topics like calculating sums and averages, finding greater of three numbers, checking eligibility to vote, and printing odd/even numbers using for loops. More advanced topics include switch statements to print days/months, string handling, and comparing two strings. Well-structured code snippets are provided for each question to demonstrate the concept.

Uploaded by

joker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

C PROGRAMMING

Coding

Abstract
[Draw your reader in with an engaging abstract. It is typically a short summary of the document.
When you’re ready to add your content, just click here and start typing.]

Satwinder Singh
[email protected]
Contents
Q1. Sum of two number(pre defined)..............................................................................................2
Q2. Sum of two number(post defined)............................................................................................3
Q3. Add,Sub,multi,sub(mini calculator)...........................................................................................4
Q4. For loop......................................................................................................................................0
Q5. Odd number using for loop........................................................................................................1
Q6. Even number using for loop.......................................................................................................2
Q7. for loop(post input)...................................................................................................................3
Q8. Voting system(using if-else)......................................................................................................4
Q9. Voting system 2(using if-else)...................................................................................................0
Q10. Find greater number...............................................................................................................1
Q11. Percentage of total marks.......................................................................................................2
Q12. Find area of rectangle..............................................................................................................3
Q13. Switch case statement............................................................................................................4
Q14. Switch case statement(days)..................................................................................................5
Q15. Switch case statement(months).............................................................................................7
Q16. Switch case statement(months).............................................................................................9
Q1. Sum of two number(pre defined).
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int x=2,y=4,sum;
sum=x+y;
printf("sum of x&y is %d",sum);
getch();
}

Result:
Q2. Sum of two number(post
defined).
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr()
int x,y,sum;
printf("enter value of x&y\n");
scanf("%d",&x);
scanf("%d",&y);
sum=x+y;
printf("sum of x&y is %d",sum);
getch();
}

Result:
Q3. Add,Sub,multi,sub(mini
calculator).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,result,n;
printf("1.addition\n2.subtraction\n3.multiplication\n4.division\n");
printf("enter the choice no.");
scanf("%d",&n);
printf("enter the first no.\n");
scanf("%d",&a);
printf("enter the second no.\n");
scanf("%d",&b);
switch (n)
{
case 1:
result=a+b;
printf("result=%d",result);
break;
case 2:
result=a-b;
printf("result=%d",result);
break;
case 3:
result=a*b;
printf("result=%d",result);
break;
case 4:
result=a/b;
printf("result=%d",result);
break;
default:
printf("wrong input");
}
Getch();

Result:
Q4. For loop.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=0;i<=10;i++)
printf(" values=%d\n",i);
getch();
}

Result:
Q5. Odd number using for loop.
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j;
printf("enter a no.");
scanf("%d",&j);
for(i=1;i<=j;i+=2)
{
printf("%d\n",i);
}
getch();
}

Result:
Q6. Even number using for loop.
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j;
printf("enter a no.");
scanf("%d",&j);
for(i=1;i<=j;i+=2)
{
printf("%d\n",i);
}
getch();
}

Result:
Q7. for loop(post input).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
printf("enter a no.");
scanf("%d",&j);
for(i=0;i<=j;i++)
printf("enter the value=%d\n",i);
getch();
}

Result:
Q8. Voting system(using if-else).
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int age;
printf("enter the age\n");
scanf("%d",&age);
if(age<18)
{
printf("you are not eligible for voting\n");
}
else
{
printf("you are eligible for voting\n");
}
getch()
}

Result:
Q9. Voting
system 2(using if-else).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int age;
printf("enter the age\n");
scanf("%d",&age);
if(age<18)
{
printf("you are not eligible for voting\n");
printf("you have to wait for %d years",18-age);
}
else
{
printf("you are eligible for voting\n");
}
getch();
}

Result:
Q10. Find greater number.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z;
printf("enter the value of x=\n");
scanf("%d",&x);
printf("enter the value of y=\n");
scanf("%d",&y);
printf("enter the value of z=\n");
scanf("%d",&z);
if(x>y)
printf("x is greater");
else if(y>z)
printf("y is greater");
else
printf("z is greater");
getch();
}

Result:
Q11. Percentage of total marks.
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int english,punjabi,hindi,science,sum,total=400;
float per;
printf("enter the marks of english=");
scanf("%d",&english);
printf("enter the marks of punjabi=");
scanf("%d",&punjabi);
printf("enter the marks of hindi=");
scanf("%d",&hindi);
printf("enter the marks of science=");
scanf("%d",&science);
sum=english+punjabi+hindi+science;
printf("total marks=%d\n",sum);
per=(sum*100)/total;
printf("percentage=%f",per);
getch();
}

Result:
Q12. Find area of rectangle.
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a,b;
printf("enter the length of rectangle\n");
scanf("%d",&a);
printf("enter the breadth of rectangle\n");
scanf("%d",&b);
printf("area of rectangle=%d",a*b);
getch();
}

Result:
Q13. Switch case statement.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf("enter a no.");
scanf("%d",&a);
switch(a)
{
case 1:
printf("you are in heaven");
break;
case 2:
printf("you are in hell");
break;
case 3:
printf("you are on earth");
break;
default:
printf("wrong input");
}
getch();
}

Result:
Q14. Switch case statement(days).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf("enter the no.");
scanf("%d",&n);
switch (n)
{
case 1:
printf("monday");
break;
case 2:
printf("tuesday");
break;
case 3:
printf("wednesday");
break;
case 4:
printf("thursday");
break;
case 5:
printf("friday");
break;
case 6:
printf("saturday");
break;
case 7:
printf("sunday");
break;
}
getch();
}

Result:
Q15. Switch case statement(months).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf("enter the no.");
scanf("%d",&n);
switch (n)
{
case 1:
printf("january");
break;
case 2:
printf("february");
break;
case 3:
printf("march");
break;
case 4:
printf("april");
break;
case 5:
printf("may");
break;
case 6:
printf("june");
break;
case 7:
printf("july");
break;
case 8:
printf("august");
break;
case 9:
printf("september");
break;
case 10:
printf("october");
break;
case 11:
printf("november");
break;
case 12:
printf("december");
break;
default:
printf("wrong input");
}
getch();
}

Result:
Q16. Sum,div,mul,sub using switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,result,n;
printf("1.addition\n2.subtraction\n3.multiplication\n4.division\n");
printf("enter the choice no.");
scanf("%d",&n);
printf("enter the first no.\n");
scanf("%d",&a);
printf("enter the second no.\n");
scanf("%d",&b);
switch (n)
{
case 1:
result=a+b;
printf("result=%d",result);
break;
case 2:
result=a-b;
printf("result=%d",result);
break;
case 3:
result=a*b;
printf("result=%d",result);
break;
case 4:
result=a/b;
printf("result=%d",result);
break;
default:
printf("wrong input");
}
getch();
}

Result:
Q17. Biodata using String char.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char name[15],age[3],school[10],course[10],year[4],pno[10];
printf("Enter your name\n");
gets(name);
printf("Enter your Age\n");
gets(age);
printf("Enter your school name\n");
gets(school);
printf("Enter your course name\n");
gets(course);
printf("Enter joining year\n");
gets(year);
printf("Enter phone number\n");
gets(pno);
printf("your entered details are\n");
printf("Name is %s\n",name);
printf("age is %s\n",age);
printf("School name is %s\n",school);
printf("Course name is %s\n",course);
printf("Year of joining is %s\n",year);
printf("Phone no. is %s\n",pno);
getch();
}
Result:
Q18. Odd,Even Series using Switch case n
forloop.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
printf("enter 1 for even and 2 for odd series\n");
scanf("%d",&n);
switch (n)
{
case 1:
for(i=0;i<=10;i+=2)
printf("%d\n",i);
break;
case 2:
for(i=1;i<=10;i+=2)
printf("%d\n",i);
break;
case 3:
for(i=0;i<=10;i++)
printf("%d\n",i);
break;
}
getch();
}
Result:
Q19. Simple string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[20];
printf("Enter a string:");
gets(str);
printf("%s",str);
getch();
}

Result:
Q20. Comparison of two strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s1[20],s2[20];
int result;
printf("enter string 1: \n");
gets(s1);
printf("enter string 2:\n");
gets(s2);
result=strcmp(s1,s2);
if(result==0)
{
printf("both strings are equal");
}
else
{
printf("both string are not equal");
}
getch();
}

Result:
Q21. Find out simple interest.
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
float principle,time,rate,si;
printf("enter amount\n");
scanf("%f",&principle);
printf("enter time\n");
scanf("%f",&time);
printf ("enter interest rate\n");
scanf("%f",&rate);
si=(principle*time*rate)/100;
printf("simple interest=%f",si);
getch();
}

Result:
Q22. Find out simple interest and total
amount.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
float principle,time,rate,si,total;
printf("enter amount\n");
scanf("%f",&principle);
printf("enter time\n");
scanf("%f",&time);
printf ("enter interest rate\n");
scanf("%f",&rate);
si=(principle*time*rate)/100;
printf("simple interest=%f\n",si);
total=principle+si;
printf("%f",&total);
getch();
}

Result:

You might also like