Unit 3 - Control Structure in C
3.1 - Write a program to select and print the largest of the three nos. using nested-if-else statement. [S-14]
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, z;
printf("Enter three numbers in x, y and z \n");
scanf("%d %d %d",&x, &y, &z);
printf("The largest number is: ");
if (x > y)
{
if(x > z)
{
printf("x = %d",x);
}
else
{
printf("z = %d",z);
}
}
else
{
if(y > z)
{
printf("y = %d",y);
}
else
{
printf("z = %d",z);
}
}
getch();
}
3.2 - Write a program to perform addition, multiplication, subtraction and division with switch statement. [S-14]
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, ch;
printf("\n enter 1 for addition \n enter 2 for substraction \n enter 3 for multiplication \n enter 4 for division \n");
scanf("%d", &ch);
if (ch>0 && ch<5)
{
printf(" enter two numbers : \n");
scanf ("%d %d", &a,&b);
}
switch (ch)
{
case 1:
c=a+b;
printf("\n addition = %d",c);
break;
case 2:
c = a-b;
printf("\n subtraction = %d",c);
break;
case 3:
c=a*b;
printf("\n multiplication = %d",c);
break;
case 4:
c=a/b;
printf("\n division = %d",c);
break;
default:
printf("\n invalid choice");
break;
}
getch();
}
3.3 - Write a program in C to generate Fibonacci series like following: 1, 1, 2, 3, 5, 8, 13……. Generate 20 such numbers. [S-11, W-10]
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
3.4 - Write a program to find the sum of first N odd numbers. [W-13]
#include<stdio.h>
#include<conio.h>
int main()
{
int i, num, sum = 0;
printf("Please Enter the Maximum Limit Value : ");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
if(i%2!=0)
{
sum = sum + i;
}
}
printf("\nThe Sum of Odd Numbers from 1 to %d = %d", num, sum);
return 0;
}
3.5 - Write a C Program to check whether the given number is prime or not. [W-13, W-15, W-16]
#include <stdio.h>
int main()
{
int n, i , flag = 0;
printf("Enter a positive number : ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(n==1)
{
printf("1 is neither prime nor composite.");
}
else
{
if(flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
}
return 0;
}
3.6 - Write a program to print the following pattern. [S-14]
*
* *
* * *
* * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, rows;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (k = i; k < rows; k++)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
3.7 - Write a program to display the following pattern using nested for loops. [S-11]
* * * * *
* * * *
* * *
* *
*
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows : ");
scanf("%d",&rows);
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
}
3.8 - Write a program to print following pattern. [S-13]
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
for(i=5; i>=1; i--)
{
for(k=i; k<=4; k++)
{
printf(" ");
}
for(j=i; j>=1; j--)
{
printf("%d", j);
}
printf("\n");
}
getch();
}
3.9 - Write a program to print the following pattern. [S-11]
1
0 1
1 0 1
0 1 0 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,rows;
printf("Enter number of rows : ");
scanf("%d", &rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",(i+j+1)%2);
}
printf("\n");
}
getch();
}
3.10 - Write a program to print the following pattern. [W-15]
1
2 2
3 3 3
4 4 4 4
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, rows;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (k = i; k < rows; k++)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("%d ",i);
}
printf("\n");
}
getch();
}
3.11 - Write a program to display following pattern using nested for loops. [S-10, W13]
1
1 3
1 3 5
1 3 5 7
For n lines.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ",2*j-1);
}
printf("\n");
}
getch();
}
3.12 - Write a C program to display the following triangle for N lines. [S-10]
1
A B
1 2 3
A B C D
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, rows, x=1;
char ch='A';
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
x=1,ch='A';
for (k = i; k < rows; k++)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
if(i%2==0)
{
printf("%c ",ch);
ch++;
}
else
{
printf("%d ",x);
x++;
}
}
printf("\n");
}
getch();
}
3.13 - Write a C program to display the following triangle for N lines. [S-10]
1
A B
2 3 4
C D E F
5 6 7 8 9
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, rows, x=1;
char ch='A';
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (k = i; k < rows; k++)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
if(i%2==0)
{
printf("%c ",ch);
ch++;
}
else
{
printf("%d ",x);
x++;
}
}
printf("\n");
}
getch();
}
3.14 - Write a program to reverse the number. (e.g. 456 -> 654) [S-13, S-19]
#include<stdio.h>
#include<conio.h>
int main()
{
int x, rev = 0, remainder;
printf("Enter a number : ");
scanf("%d",&x);
while (x != 0)
{
remainder = x % 10;
rev = rev * 10 + remainder;
x = x/10;
}
printf("Reversed number = %d",rev);
return 0;
}
3.15 - Write a C program to find out Armstrong Numbers between 0 and 999. Example : 153 is an Armstrong number because 13+53+33=153 OR Write a program to print all Armstrong numbers in a given range. Armstrong number is equal to sum of cubes of its individual digits. For example 153 = 1^3 +5^3 + 3^3. So, 153 is Armstrong number. [W-10, S-19]
#include<stdio.h> int main() { int lower,upper,n,x,s,r; printf("Enter lower limit : "); scanf("%d",&lower); printf("Enter upper limit : "); scanf("%d",&upper); printf("Armstrong numbers : \n"); for(n=lower;n<=upper;n++) { x=n; s=0; while(x!=0) { r=x%10; s=s+r*r*r; x=x/10; } if(s==n) printf("%d\n",n); } }
3.16 - Write a C program to print multiple of N from given range of unsigned integers. For example, if N=5 and range is [17, 45] it prints 20,25,30,35,40,45. [S-11]
#include<stdio.h> int main() { int n,a,b,i,j,k; printf("Enter number for multiplication N = "); scanf("%d",&n); printf("Enter two numbers for range : "); scanf("%d %d",&a,&b); for(i=a;i<=b;i++) { if(i%n==k) printf("%d,",i); } }
3.17 - Write a function program to find whether the string is a palindrome or not. [W-13, S-17]
#include <stdio.h>
#include <string.h>
int main()
{
char str[20];
int i, length, flag=0;
printf("Enter a string : ");
scanf("%s", str);
length = strlen(str);
for(i=0; i < length ; i++)
{
if(str[i] != str[length-i-1])
{
flag = 1;
break;
}
}
if(flag)
{
printf("%s is not a palindrome.", str);
}
else
{
printf("%s is a palindrome.", str);
}
return 0;
}
3.18 - Write a menu driven C program for simple calculator. Also draw flowchart. [W-16]
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, ch;
printf("\n enter 1 for addition \n enter 2 for substraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulo \n");
scanf("%d", &ch);
if (ch>0 && ch<6)
{
printf(" enter two numbers : \n");
scanf ("%d %d", &a,&b);
}
switch (ch)
{
case 1:
c=a+b;
printf("\n addition = %d",c);
break;
case 2:
c=a-b;
printf("\n subtraction = %d",c);
break;
case 3:
c=a*b;
printf("\n multiplication = %d",c);
break;
case 4:
c=a/b;
printf("\n division = %d",c);
break;
case 5:
c=a%b;
printf("\n modulo = %d",c);
break;
default:
printf("\n invalid choice");
break;
}
getch();
}
3.19 - Develop a simple program to Add, subtract and multiply two numbers using switch statement. [W-17]
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, ch;
printf(" enter 1 for addition \n enter 2 for substraction \n enter 3 for multiplication \n");
scanf("%d", &ch);
if (ch>0 && ch<=3)
{
printf("Enter two numbers : \n");
scanf ("%d %d", &a,&b);
}
switch (ch)
{
case 1:
c=a+b;
printf("\n addition = %d",c);
break;
case 2:
c=a-b;
printf("\n subtraction = %d",c);
break;
case 3:
c=a*b;
printf("\n multiplication = %d",c);
break;
default:
printf("\n invalid choice");
break;
}
getch();
}
3.20 - Write a program to find sum of first N odd numbers. Ex. 1+3+5+7+… +N. [W-17, W-19]
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input number of terms : ");
scanf("%d",&n);
printf("\nThe odd numbers are : ");
for(i=1;i<=n;i++)
{
printf("%d+",2*i-1);
sum = n * n; //sum = sum + 2*i-1;
}
printf("\nThe Sum of odd Natural Number upto %d terms : %d \n",n,sum);
}
3.21 - Write a program to print multiplication table of any number. [S-17]
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer : ");
scanf("%d", &n);
for (i = 1; i <= 10; i++)
{
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
3.22 - Write a program to display given pattern. [S-17]
*
# #
* * *
# # # #
* * * * *
# # # # # #
* * * * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
if(i%2==0)
{
printf("# ");
}
else
{
printf("* ");
}
}
printf("\n");
}
getch();
}
3.23 - Write a program to accept start number and end number from the user and print all the numbers in the range. [S-17]
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, num;
printf("Enter range a and b : ");
scanf("%d %d", &a, &b);
for(num = a; num <= b; num++)
{
printf("%d ", num);
}
getch();
}
3.24 - Write a program in ‘C’ to print the following pattern. [S-17]
1
23
456
78910
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows, no=1;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d",no);
no++;
}
printf("\n");
}
getch();
}
3.25 - Write a program in ‘C’ to print the following pattern using loop statement. [S-18, W-19]
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ",i);
}
printf("\n");
}
getch();
}
3.26 - Write a C program to input an integer number and check last digit of number is even or odd. [W-18, S-19]
#include <stdio.h>
int main()
{
int num;
printf("Enter any number : ");
scanf("%d", &num);
if(num % 2 == 0) //if((num%10)%2==0)
{
printf("Last digit of number is even.");
}
else
{
printf("Last digit of number is odd.");
}
return 0;
}
3.27 - Write a program to find 1+1/2+1/3+1/4+….+1/n. [S-19, S-20]
#include<stdio.h>
int main()
{
int n,i;
float sum=0;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1.0/i);
}
printf("\nSum of the Series = %f",sum);
return 0;
}
3.31 - Write a program to check whether entered character is vowel or not? [S-19]
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character : ");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c isn't a vowel.\n", ch);
return 0;
}
No comments:
Post a Comment
If you have any doubts or suggestions, Please let me know