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

AdvC DA Mod PDF

The document contains 18 code snippets demonstrating various programming concepts in C language. The snippets cover topics like input/output, conditional statements, loops, functions, arrays, strings and more. They provide examples of how to take user input, check conditions, iterate through loops, perform calculations and print outputs.

Uploaded by

Sanchit Anand
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

AdvC DA Mod PDF

The document contains 18 code snippets demonstrating various programming concepts in C language. The snippets cover topics like input/output, conditional statements, loops, functions, arrays, strings and more. They provide examples of how to take user input, check conditions, iterate through loops, perform calculations and print outputs.

Uploaded by

Sanchit Anand
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

1. Get a number from user. If the number is less than or equal to 10.

Print the
message “good”

#include <stdio.h>
int main()
{
float n;
printf("enter a number: ");
scanf("%f",&n);
if(n<=10)
{
printf("GOOD");
}
return 0;

2. Write a program which takes a character input and checks whether it is


vowel or consonant

#include <stdio.h>
int main()
{
char l;
printf("enter a letter ");
scanf("%c",&l);
if(l>='A' && l<='Z' || l>='a' && l<='z')
{
if (l=='a'||l=='e'||l=='i'||l=='o'||l=='u'||l=='A'||l=='E'||l=='I'||l=='O'||l=='U')
{
printf("its a VOWEL \n");
}
else
{
printf("its a CONSONANT \n");
}
}
else{ printf("different character..."); }
return 0;
}
3. Read three numbers from the user and find the biggest of three using
simple (multiple) if statements.

#include <stdio.h>
int main()
{
int n1,n2,n3;
printf("enter three numbers..");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
{
printf("n1 is the biggest number %d",n1);
}
If(n1<n3)
{
printf("n3 is the biggest number %d",n3);
}
}
if(n2>n3)
{
printf("n2 is the biggest number %d",n2);
}
If(n3>n2)
{
printf("n3 is the biggest no. %d",n3);
}

return 0;
}

4. Write a c program to check given year is leap year or not

#include <stdio.h>
int main()
{
int year;
printf("enter the year to see if its a leap year");
scanf("%d",&year);
if(year%4==0 && year%100!=0)
{
printf("its leap year...:%d",year);
}
else
{
printf("not a leap year");
}
return 0;
}

5. The National Earthquake Information Center has the following criteria to


determine the earthquake damages. Here is the given Richter scale serve as
an input data and the characterization as output information.

#include<stdio.h>
int main()
{
float n;
printf("enter data: ");
scanf("%f",&n);
if(n<5.0)
{
printf("little or no damage");
}
else if(n>=5.0 && n<5.5)
{
printf("some damage");
}
else if(n>=5.5 && n<6.5)
{
printf("serious damage");
}
else if(n>=6.5 && n<7.5)
{
printf("disaster");
}
else
printf("Higher Catastrophe");
}

6. Write a c program to check given number is palindrome number or

#include <stdio.h>
int main()
{
int n,temp,rem,rev=0;
printf("enter a number to check if its palindrome!");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(temp==rev)
{
printf("its palindrome!!");
}
else
{
printf("NOT palindrome...");
}
return 0;
}
7. Write a c program to check given number is Armstrong number or not.

#include<stdio.h>
#include<math.h>
int main()
{
int n,rem,sum=0,temp;
printf("enter a number to check if its armstrong");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+pow(rem,3);
n=n/10;
}
if(temp==sum)
printf("armstrong");
else
printf("its NOT armstrong");
return 0;
}
8. Write a C program to convert a binary number to decimal number.

#include<stdio.h>
void main()
{
int num, bin, dec= 0, base = 1, rem;
printf("Enter a binary number \n");
scanf("%d", &num);
bin= num;
while (num > 0)
{
rem = num % 10;
dec= dec + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", bin);
printf("Its decimal equivalent is = %d \n", dec);
}

9. Write a C program to find out the sum of series 1^2 + 2^2 + …. + n^2

#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("enter the number n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf("the sum of the series:\n 1^2 + 2^2 + …. + n^2 = %d",sum);
return 0;
}

10. Write a C program to read the age of 100 persons and count the
number of persons in the age group 50 to 60. Use for and continue
statement
#include<stdio.h>
int main()
{
int age[100],count=0;
printf("enter the age of people:");
for(int i=0;i<100;i++)
{
scanf("%d",&age[i]);
}
for(int i=0;i<100;i++)
{
if(age[i]>=50 && age[i]<=60)
{
count++;
}
else
continue;

}
printf("Number of people with age group 50-60 years: %d",count);
return 0;
}
11. Write a C program to calculate and print the sum of first 5 terms of
following: /1+ (1+2) + (1+2+3) + (1+2+3+4) +…
#include<stdio.h>
int main()
{
int sum=0,n,i,j;
printf("to find sum of series: 1+(1+2)+(1+2+3)+(1+2+3+4)+..");
printf("take n from user: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
sum=sum+j;
}
}
printf("the sum of series is= %d",sum);
return 0;
}

12. Write a C program to print the following series


1
2 3
4 5 6
7 8 9 10

#include<stdio.h>

int main(){

int i,j,k=1;

for(i=1;i<=4;i++){

for(j=1;j<=i;j++)

{ printf("%d\

t",k); k++;
}

printf("\n");

return 0;

13. Write a program to read 3 digit numbers and print the digits in
words using switch case

#include<stdio.h>
#include<ctype.h>
int main()
{
int n,num,a=100,b=100;
printf("enter a 3 digit no:");
scanf("%d",&n);
for(int i=0;i<3;i++)
{
num=n/a;
switch(num)
{
case 0:
printf("zero ");
break;
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
}
a=a/10;
n=n%b;
b=b/10;
}
return 0;
}

14. Write a program to count the number of vowels in a string using


switch- case control structure

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str[50];
int count;
count=0;
printf("enter the string to search for vowels");
gets(str);
for(int i=0;i<strlen(str);i++)
{
switch(str[i])
{
case 'a':
count++;
break;
case 'e':
count++;
break;
case 'i':
count++;
break;
case 'o':
count++;
break;
case 'u':
count++;
break;
case 'A':
count++;
break;
case 'E':
count++;
break;
case 'I':
count++;
break;
case 'O':
count++;
break;
case 'U':
count++;
break;
}
}
if(count>0)
printf("no of VOWELS: %d",count);
else
printf("no vowels are present!!!");
return 0;
}

15. Write a menu driven program to perform mathematical functions like,


1.sin(x), 2.cos(x), 3.tan(x), 4.log(x), 5.log10(x), 6.sqrt(x). Using switch case.
Read x from the user. 16. Continue the menu until the user says no using go
to.
#include<stdio.h>

#include<math.h>

#include<ctype.h>

int main()

int b;

float num;

char ans;

printf("Enter a number on which u want to perform the operation\n");

scanf("%f",&num);

do{

printf("Enter a number to perform the following option\n");


printf(" 1. sin(x)\n 2. cos(x)\n 3. tan(x)\n 4. log(x)\n 5. log10(X)\n 6. sqrt(x)\n");

scanf("%d", &b);

switch(b)

case 1: printf("%f", sin(num));

break;

case 2: printf("%f", cos(num));

break;

case 3: printf("%f", tan(num));

break;

case 4: printf("%f", log(num));

break;

case 5: printf("%f", log10(num));

break;

case 6: printf("%f", sqrt(num));

break;

default: printf("Invalid input number");

break;

}
printf("\nDo you want to continue?\n");

fflush(stdin);

scanf("%c",&ans);

}while(ans =='y' || ans=='Y');

if(ans=='n')

printf("thanks for being with us");

else if(ans!='y'&&ans!='n')

printf("enter in either n or y");

return 0;

}
17. Write a C Program to read student’s grade from keyboard from 1 to 5)
and prints it’s description.1.Fail 2.Bad 3.Good 4.VeryGood 5.Excellent
,default: false grade

#include<stdio.h>
int main()
{
char grade;
printf("enter your grade =");
scanf("%c",&grade);
if(isalpha(grade)==0)
printf("enter in alphabets!");
switch(grade)
{
case 'a':
printf("excellent");
break;
case 'b':
printf("very good");
break;
case 'c':
printf("good");
break;
case 'd':
printf("bad");
break;
case 'f':
printf("FAIL");
break;
default:
printf("false grade");
}
return 0;
}
18. Get a single digit number from the user (0-9) one after another, until the
user wishes. Finally display the count of each numbers and display it using
switch and goto.

#include<stdio.h>

#include<string.h>

int main()

int count, sum = 0;

char ans;

int num;

int one = 0, two = 0, three = 0, four = 0, five = 0;

int six = 0, seven = 0, eight = 0, nine = 0, zero = 0;

lable :

printf("\nEnter number: ");


scanf("%d", &num);

switch(num)

case 1: one++; sum++;break;

case 2: two++; sum++;break;

case 3: three++; sum++;break;

case 4: four++; sum++;break;

case 5: five++; sum++;break;

case 6: six++; sum++;break;

case 7: seven++; sum++;break;

case 8: eight++; sum++;break;

case 9: nine++; sum++;break;

case 0: zero++; sum++;break;

default:;

printf("continue? write y for yes and n for no:");

scanf(" %c",&ans);

if(ans=='y')

{ goto

lable;

else if(ans=='n'){

printf("\nTotal Number :\t%d\n", sum);

printf("\nCount of one = %d", one);

printf("\nCount of two = %d", two);

printf("\nCount of three = %d", three);

printf("\nCount of four = %d", four);

printf("\nCount of five = %d",five);

printf("\nCount of six = %d", six);

printf("\nCount of seven = %d", seven);

printf("\nCount of eight = %d", eight);


printf("\nCount of nine = %d", nine);

printf("\nCount of zero = %d\n", zero);

else

printf("enter only in y or n!! please run again");

return 0;}

Challenging C Programs

1. Write a C program to display alphabets as given


below az by cx dw ev fu gt hs ir
jq kp lo mn nm ol pk qj ri
sh tg uf ve wd xc yb za

#include<stdio.h>
int main()
{
int i,j=122,newline=1;
for(i=97;i<=122;i++)
{
printf("%c%c ",i,j);
if(newline==9||newline==18)
printf("\n");
j--;
newline++;
}
return 0;
}

2. Use the following formula. //e=1+1/1!+1/2!+1/3!+.........+1/n!. using a


suitable loop construct.
#include <stdio.h>

int main()

int n;

printf("enter n: ");

scanf("%d",&n);

float sum = 1;

int fact = 1;

for (int i = 1; i <= n; i++)

fact *= i;

sum += 1.0/fact;

printf("%f",sum);

return 0;

3. Write programs to evaluate the following functions to 0.0001% accuracy.


sinx =x3/3!+x5/5!-x7/7!+………………….
#include<stdio.h>

#include<conio.h>

#include<math.h>

double fact(double power)

double f=1;

int k;

for(k=1;k<=power;k++)

f=f*k;

return f;

void main()

int i=1;

double x,term,deno,lob,sin,power=3;

printf("Enter the angle in terms of degree-");

scanf("%lf",&x);

x=3.142/180*x;

term=x;

sin=x;

while(term>=0.0001)

lob=pow(x,power);

deno=fact(power);
term=lob/deno;

power+=2; if(i

%2==1)

sin=sin-term;

else

sin=sin+term;

i++;

printf("the result= %lf",sin);

4. Write a C program, for all positive integers i,j,k, and l from 1 through 50,
finds and /prints all combinations of i,j,k and l such that i+j+k=l and i < j < k <
l.
#include<stdio.h>
int main()
{
int i,j,k,l;
for(l=1;l<=50;l++)
for(k=1;k<l;k++)
for(j=1;j<k;j++)
for(i=1;i<j;i++)
{
if((i+j+k)==l) printf("%d+%d+%d=
%d \n",i,j,k,l);
}
return 0;
}

5. Write a C program to print all integers that are not divisible by 2 or 3 and
lie between 1 and 100. Program should also account /the number of such
integers and print the result
#include<stdio.h>
int main()
{
int i,count=0,sum=0;
for(i=1;i<=100;i++)
{
if(((i%2)!=0) || ((i%3)!=0))
{
count++;
printf("%d \n",i);
}
}
printf("the count= %d",count);
return 0;
}

6. Given a set of 10 two-digit integer containing both positive and negative


values, write a program using for loop to compute the sum of all positive
values and print the sum and the number of values added. The C
program should use scanf to read the values and terminate when the
sum exceeds 999. Do not use goto statement.

#include<stdio.h>
int main()
{
signed int n[100];
int i,sum=0;
for(i=1;i<=10;i++)
{
scanf("%d",&n[i]);
if(n[i]>0)
{
sum=sum+n[i];

}
}
if(sum>=999)
printf("the sum exeeds 999");
else
printf("%d",sum);
return 0;
}

7. The equation x2+y2 = r2 represents a circle with center at origin and radius
r. Write a C program that reads r from the keyboard and prints the number of
points with integer coordinated that lie within the circle
#include<stdio.h>

#include<math.h>

void main()

float r,dist;

printf("Enter the radius-");

scanf("%f",&r);

int c=0;

for(int i=0;i<r;i++)

for(int j=0;j<r;j++)

if(i==0 && j==0)

continue;

dist=sqrt(pow(i,2)+pow(j,2));

if(dist<r)

printf("(%d,%d) lie within circle\n",i,j);

c++;
}

printf("The number of points are %d\n",c);

8. If a number 972 is entered through the keyboard, your program should


print “Nine Seven Two”. Write a C program such that it does for any positive
integer

#include <stdio.h>

#include

<string.h>

#include <ctype.h>

int main()

char n[100],c=0;

scanf("%s", n);

for(int i=0;i<strlen(n);i++)

if(isdigit(n[i]))

c=1;

else{

printf("invalid input");

c=0;

break;
}

if(c==1){

for(int i=0;i<strlen(n);i++)

switch(n[i])

case '0':

printf("zero ");

break;

case '1':

printf("one ");

break;

case '2':

printf("two ");

break;

case '3':

printf("three ");

break;

case '4':

printf("four ");

break;

case '5':

printf("five ");

break;

case '6':

printf("six ");

break;

case '7':

printf("seven ");

break;
case '8':

printf("eight ");

break;

case '9':

printf("nine ");

break;

return 0;

}}

9. The cost of a Scooter is Rs25000/-. Three optional accessories are supplied


at different costs as follows. Mirror:Rs.85 Crash Guard:Rs225 Side box: Rs 300
If A,B, and C represents the three accessories, the total cost of the scooter is
25000+A*85+B*225+C*300 where A,B and C are either 0 or 1 depending upon
whether the option is required or not. Write a C program to print the total
cost as per the illustration given below.

#include<stdio.h>

void main(){

int base=25000,tot;

printf("base price mirror crash guard side box total\n" );


for(int a=0;a<=1;a++)

{ for(int b=0;b<=1;b+

+){

for(int c=0;c<=1;c++)

{ tot=base+a*85+b*25+c*30

0;

printf("%d %d %d %d %d\n",base,a,b,c,tot);

Additional C Programs

19. Write a program to print all possible combinations of the digit 2, 5 and 7
using for loop only

#include<stdio.h>

int main()

int a[3]={2,5,7};

int i,j,k,m;

for(i=0;i<3;i++)

{ for(j=0;j<3;j++){

for(k=0;k<3;k++){

if(i!=j && j!=k && k!=i)

{ m=(100*a[i])+(10*a[j])
+a[k];
printf("%d \n",m);

return 0;

20. By a research study it is found that the intelligence of person can be


calculated with the following formula, i = 2 + (y+0.5x). Write a C program to
generate a table of values of i, y and x, where y varies from 1 to 6, and for
each value of y, x varies from 5.5 to 12.5 in steps of 0.5

#include<stdio.h>

int main(){

float i,y,x;

for(y=1;y<=6;y++){
for(x=5.5;x<=12.5;x+=0.5){

i=2+(y+(0.5)*x);

printf("value of x %f\n value of y %f\n value of i %f\n",x,y,i);

return 0;

21. Write a menu driven c program with the following options 1. odd or even
2. Prime or not 3. Factorial of a number 4. Exit

#include<stdio.h>

void oddeven()

int x;

printf("enter the number");

scanf("%d",&x);

if(x%2==0)

printf("it is even");

else
printf("it is odd");

void prime()

int num,i,ctr=0;

printf("Input a number: ");

scanf("%d",&num);

for(i=2;i<=num/2;i++){

if(num % i==0){

ctr++;

break;

if(ctr==0 && num!= 1)

printf("%d is a prime number.\n",num);

else

printf("%d is not a prime number",num);

void fact()

int i,j,n,fac=1;

printf("enter the number");

scanf("%d",&n);

for(i=n;i>0;i--)

fac=fac*i;

printf("factorial is %d",fac);

int main(){
int ch;

do{

printf("\n enter your choice: 1.odd and even 2.prime or not 3.factorial of a number 4.exit\n");

scanf("%d",&ch);

switch(ch)

{ case

1:oddeven();

break;

case 2:prime();

break;

case 3:fact();

break;

case 4:break;

default:printf("wrong choice!!!!!!!!!!!!!");

break;

}while(ch!=4);

return 0;

22. Assume a given series 1!/2! + 2!/3! + 3!+4!...n!/(n+1)!-----Write a C


program to calculate its sum

#include<stdio.h>

int main()

float sum=0.0;
int n,i,j;

printf(" enter n to find sum of series: (1!/2!)+(2!/3!)+....+(n!/(n+1)!)\n");

scanf("%d",&n);

for(i=1;i<=n;i++)

float fac=1,fac2;

for(j=1;j<=i;j++)

fac=fac*j;

fac2=fac*(j+1);

sum=sum+(fac/fac2);

printf("\n the sum is: %f",sum);

return 0;

23. Write a C program to swap two given numbers with and without using
the third variable (Hint: using bitwise operators (ex-or ^), using + and -, using
* and / (without using third variable)
#include<stdio.h>

int main(){

int a,b,c,ch;

printf("if you want to swap using two variables enter 1 \n if you want to swap using three variables
enter 2");

scanf("%d",&ch);

if(ch==1){

printf("enter the two numbers ");

scanf("%d %d",&a,&b);

printf("initially\n a= %dand b= %d\n",a,b);

a=a+b; //alternate:a=a*b;

b=a-b; // alternate:b=a/b;

a=a-b; // alternate:a=a/b;

printf("after swapping\n a= %dand b= %d\n",a,b);

else if(ch==2){

printf("enter the two numbers ");

scanf("%d %d",&a,&b);

printf("initially\n a= %dand b= %d\n",a,b);

c=a;

a=b;

b=c;

printf("after swapping\n a= %dand b= %d\n",a,b);

else

printf("wrong choice");
return 0;

You might also like