PROGRAMMING QUESTIONS-1
PROGRAMMING QUESTIONS-1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("Enter the no 1:");
scanf("%d",&a);
printf("\nEnter the no 2:");
scanf("%d",&b);
if(a>b)
{
printf("%d is greater than %d",a,b);
}
printf("%d is greater than %d",b,a);
return 0;
}
Write a C program to find maximum between three numbers using conditional operator.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
printf("Enter the no 1:");
scanf("%d",&a);
printf("\nEnter the no 2:");
scanf("%d",&b);
printf("\nEnter the no 3:");
scanf("%d",&c);
if(a>b && a>c)
{
printf("\n%d is greater than %d and %d",a,b,c);
}
else if(b>a && b>c)
{
printf("\n%d is greater than %d and %d",b,a,c);
}
else
printf("\n%d is greater than %d and %d",c,a,b);
return 0;
}
int main()
{
int no;
printf("Enter the number:");
scanf("%d",&no);
if(no%2==0)
{
printf("\n%d is an even number",no);
}
else
return 0;
}
Write a C program to check whether year is leap year or not using conditional
operator.
Write a C program to check whether character is an alphabet or not using
conditional operator.
int main()
{
int a,b,sum=0;
printf("Enter the number 1:");
scanf("%d",&a);
printf("Enter the number 2:");
scanf("%d",&b);
sum=a+b;
printf("Sum of numbers is: %d",sum);
return 0;
}
Write a C program to enter two numbers and perform all arithmetic operations.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,sum=0;
printf("Enter the number 1:");
scanf("%d",&a);
printf("Enter the number 2:");
scanf("%d",&b);
printf("\nSum of numbers is: %d",a+b);
printf("\nSubtract of numbers is: %d",a-b);
printf("\nProduct of numbers is: %d",a*b);
printf("\nDivision of numbers is: %d",a/b);
return 0;
}
Write a C program to enter length and breadth of a rectangle and find its
perimeter.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float per=0.00;
float l,b;
printf("Enter the length:");
scanf("%f",&l);
printf("Enter the breadth:");
scanf("%f",&b);
per=2*(l+b);
printf("\nPerimeter of an rectangle is: %f",per);
return 0;
}
Write a C program to enter length and breadth of a rectangle and find its area.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float area=0.00;
float l,b;
printf("Enter the length:");
scanf("%f",&l);
printf("Enter the breadth:");
scanf("%f",&b);
area=l*b;
printf("\nArea of an rectangle is: %f",area);
return 0;
}
Write a C program to enter radius of a circle and find its diameter, circumference
and area.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float area=0.00, circum=0.00;
float r;
printf("Enter the radius of a circle:");
scanf("%f",&r);
circum=2*3.14*r;
area=3.14*r*r;
printf("\nDiameter of circle is: %f",2*r);
printf("\nCircumference of circle is: %f",circum);
printf("\nArea of circle is: %f",area);
return 0;
}
Write a C program to enter length in centimeter and convert it into meter and
kilometer.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float dist_cm=0.00;
float dist_m=0.00;
float dist_km=0.00;
printf("Enter the distance in centimeters:");
scanf("%f",&dist_cm);
dist_m=dist_cm/100.00;
dist_km=dist_cm/100000.00;
printf("\nDistance in centimeter is: %f cm",dist_cm);
printf("\nDistance in meter is: %f m",dist_m);
printf("\nDistance in kilometer is: %f km",dist_km);
return 0;
}
int main()
{
float cel=0.00;
float fah=0.00;
printf("Enter the temperature in celsius:");
scanf("%f",&cel);
fah=((cel*9)/5);
printf("The temperature in fahrenheit is: %f fah",fah);
return 0;
}
int main()
{
float cel=0.00;
float fah=0.00;
printf("Enter the temperature in fahrenheit:");
scanf("%f",&fah);
cel=((32-fah)*5)/9;
printf("The temperature in celsius is: %f cel",cel);
return 0;
}
int main()
{
int x=2,y=4,z=0;
z=pow(x,y);
printf("%d",z);
return 0;
}
Write a C program to enter any number and calculate its square root.
Write a C program to enter two angles of a triangle and find the third angle.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ang1,ang2,sum=180,ang3;
printf("Enter the angle 1:\n");
scanf("%d",&ang1);
printf("Enter the angle 2:\n");
scanf("%d",&ang2);
ang3=sum-(ang1+ang2);
printf("Angle 3 is:%d",ang3);
return 0;
}
Write a C program to enter base and height of a triangle and find its area.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int base,height,area;
printf("Enter the base of the triangle:\n");
scanf("%d",&base);
printf("Enter the height of the triangle:\n");
scanf("%d",&height);
area=(base*height)/2;
printf("Area of the triangle is: %d",area);
return 0;
}
int main()
{
float side,area;
printf("Enter the side of an equilateral triangle:\n");
scanf("%f",&side);
area=(1.732*side*side)/4;
printf("Area of the triangle is: %f",area);
return 0;
}
Write a C program to enter marks of five subjects and calculate total, average and
percentage.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int eng, hin, math, sci, sst, total;
float average, per;
printf("Enter the marks in English:\n");
scanf("%d",&eng);
printf("Enter the marks in Hindi:\n");
scanf("%d",&hin);
printf("Enter the marks in Maths:\n");
scanf("%d",&math);
printf("Enter the marks in Science:\n");
scanf("%d",&sci);
printf("Enter the marks in Social Science:\n");
scanf("%d",&sst);
total=eng+hin+math+sci+sst;
average=total/5;
per=(total*100)/125;
printf("Total is: %d",total);
printf("Average is: %f",average);
printf("Percentage is: %f",per);
return 0;
}
int main()
{
float prin,rate,time,inter,amount=0.00;
printf("Enter the principle:\n");
scanf("%f",&prin);
printf("Enter the time:\n");
scanf("%f",&time);
printf("Enter the rate of interest:\n");
scanf("%f",&rate);
inter=(prin*time*rate)/100;
amount=prin+inter;
printf("Simple interest is %f\n",inter);
printf("Total amount is %f:\n",amount);
return 0;
}
int main()
{
float prin,rate,time,inter1,inter2,amount=0.00;
printf("Enter the principle:\n");
scanf("%f",&prin);
printf("Enter the time:\n");
scanf("%f",&time);
printf("Enter the rate of interest:\n");
scanf("%f",&rate);
inter1=((rate/100)+1);
inter2=prin*pow(inter1,time);
amount=prin+inter2;
printf(" Interest is %f\n",inter2);
printf("Total amount is %f\n",amount);
return 0;
}
BITWISE EXERCISE
Write a C program to check Least Significant Bit (LSB) of a number is set or not.
Write a C program to check Most Significant Bit (MSB) of a number is set or not.
Write a C program to get nth bit of a number.
Write a C program to set nth bit of a number.
Write a C program to clear nth bit of a number.
Write a C program to toggle nth bit of a number.
Write a C program to get highest set bit of a number.
Write a C program to get lowest set bit of a number.
Write a C program to count trailing zeros in a binary number.
Write a C program to count leading zeros in a binary number.
Write a C program to flip bits of a binary number using bitwise operator.
Write a C program to count total zeros and ones in a binary number.
Write a C program to rotate bits of a given number.
Write a C program to convert decimal to binary number system using bitwise
operator.
Write a C program to swap two numbers using bitwise operator.
Write a C program to check whether a number is even or odd using bitwise operator.
int main()
{
float prin,rate,time,inter1,inter2,amount=0.00;
printf("Enter the principle:\n");
scanf("%f",&prin);
printf("Enter the time:\n");
scanf("%f",&time);
printf("Enter the rate of interest:\n");
scanf("%f",&rate);
inter1=((rate/100)+1);
inter2=prin*pow(inter1,time);
amount=prin+inter2;
printf(" Interest is %f\n",inter2);
printf("Total amount is %f\n",amount);
return 0;
}
int main()
{
int no;
printf("Enter the number:\n");
scanf("%d",&no);
if(no%5==0)
{
if(no%11==0)
printf("%d is a number divisible by both 5 and 11",no);
else
printf("%d is a number divisible by 5 only",no);
else if(no%11==0)
{
printf("%d is a number divisible by 11 only",no);
}
else
printf("%d is neither divisible by 11 nor 5",no);
return 0;
}
int main()
{
int no;
printf("Enter the number:\n");
scanf("%d",&no);
if(no%2==0)
{
printf("%d is an even number",no);
}
else
printf("%d is an odd number",no);
return 0;
}
int main()
{
int angle1,angle2,angle3,sum=0;
printf("Enter angle 1:\n");
scanf("%d",&angle1);
printf("Enter angle 2:\n");
scanf("%d",&angle2);
printf("Enter angle 3:\n");
scanf("%d",&angle3);
sum=angle1+angle2+angle3;
if(sum==180)
{
printf("These angles form triangle.");
}
else
printf("These angles do not form triangle");
return 0;
}
Write a C program to input all sides of a triangle and check whether triangle is
valid or not.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int s1,s2,s3;
printf("Enter side 1:\n");
scanf("%d",&s1);
printf("Enter side 2:\n");
scanf("%d",&s2);
printf("Enter side 3:\n");
scanf("%d",&s3);
if(s1+s2>s3 && s2+s3>s1 && s3+s1>s2)
{
printf("These sides form triangle.");
}
else
printf("These sides do not form triangle");
return 0;
}
Write a C program to check whether the triangle is equilateral, isosceles or
scalene triangle.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int s1,s2,s3;
printf("Enter side 1:\n");
scanf("%d",&s1);
printf("Enter side 2:\n");
scanf("%d",&s2);
printf("Enter side 3:\n");
scanf("%d",&s3);
if(s1+s2>s3 && s2+s3>s1 &&s3+s1>s2)
{
printf("These sides form triangle.");
}
else
printf("These sides do not form triangle");
return 0;
}
Write a C program to find all roots of a quadratic equation.
Write a C program to input marks of five subjects Physics, Chemistry, Biology,
Mathematics and Computer. Calculate percentage and grade according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
#include <stdio.h>
#include <stdlib.h>
int main()
{
int phy, chem, math, bio, comp, total;
float per;
printf("Enter the marks in Physics:\n");
scanf("%d",&phy);
printf("Enter the marks in Chemistry:\n");
scanf("%d",&chem);
printf("Enter the marks in Maths:\n");
scanf("%d",&math);
printf("Enter the marks in Biology:\n");
scanf("%d",&bio);
printf("Enter the marks in Social Computer:\n");
scanf("%d",&comp);
total=phy+chem+math+bio+comp;
per=(total*100)/125;
printf("\nTotal is: %d",total);
printf("\nPercentage is: %f",per);
if(per>90)
{
printf("\nPerfect, you secured grade 'A'");
}
else if(per>80 && per<=90)
{
printf("\nVery Good, you secured grade 'B'");
}
else if(per>70 && per<=80)
{
printf("\nGood, you secured grade 'C'");
}
else if(per>60 && per<=70)
{
printf("\nAverage, you secured grade 'D'");
}
else if(per>40 && per<=60)
{
printf("\nBelow average, you secured grade 'E'");
}
else
printf("\nSorry, you are fail, you secured grade 'F'");
return 0;
}
Write a C program to input basic salary of an employee and calculate its Gross
salary according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
#include <stdio.h>
#include <stdlib.h>
int main()
{
float basic_sal=0.00,gross_sal=0.00,hra,da;
printf("\nEnter Basic salary:");
scanf("%f",&basic_sal);
if(basic_sal<10000)
{
hra=0.2*basic_sal;
da=0.8*basic_sal;
gross_sal=basic_sal+hra+da;
printf("\nGross salary is: %f",gross_sal);
}
else if(basic_sal<20000 && basic_sal>10000)
{
hra=0.25*basic_sal;
da=0.9*basic_sal;
gross_sal=basic_sal+hra+da;
printf("\nGross salary is: %f",gross_sal);
}
else
hra=0.3*basic_sal;
da=0.95*basic_sal;
gross_sal=basic_sal+hra+da;
printf("\nGross salary is: %f",gross_sal);
return 0;
}
Write a C program to input electricity unit charges and calculate total electricity
bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float unit=0.00,bill=0.00;
printf("\nEnter Units Consumed:");
scanf("%f",&unit);
if(unit<=50)
{
bill=unit*0.50 + 0.20*unit;
printf("\nTotal Bill is: %f",bill);
}
else if(unit<150 )
{
bill=25+ (unit-50)*0.75 + 0.20*unit;
printf("\nTotal Bill is: %f",bill);
}
else if(unit<250 )
{
bill=100 + (unit-150)*1.20 + 0.20*unit;
printf("\nTotal Bill is: %f",bill);
}
else
bill=220+ (unit-250)*1.50 + 0.20*bill;
printf("\nTotal Bill is: %f",bill);
return 0;
}
Write a C program to print all natural numbers from 1 to n. - using while loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1,n;
printf("Enter the natural number up to which you want to print: \n");
scanf("%d",&n);
while(i!=n+1)
{
printf("\n%d",i);
i=i+1;
}
return 0;
}
Write a C program to print all natural numbers in reverse (from n to 1). - using
while loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the last natural number which you want to print: \n");
scanf("%d",&n);
int i=n;
while(i!=0)
{
printf("\n%d",i);
i=i-1;
}
return 0;
}
int main()
{
int i=2;
while(i!=100)
{
printf("\n%d",i);
i=i+2;
}
return 0;
}
int main()
{
int i=1;
while(i!=101)
{
printf("\n%d",i);
i=i+2;
}
return 0;
}
int main()
{
int i=1,n,sum=0;
printf("Enter n:");
scanf("%d",&n);
while(i!=n+1)
{
sum=sum+i;
i=i+1;
}
printf("%d",sum);
return 0;
}
int main()
{
int i=0,n,sum=0;
printf("Enter n:");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i=i+2;
}
printf("%d",sum);
return 0;
}
int main()
{
int i=1,n,sum=0;
printf("Enter n:");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i=i+2;
}
printf("%d",sum);
return 0;
}
int main()
{
int i=1,n;
printf("Enter n:");
scanf("%d",&n);
while(i<=10)
{
printf("\n%d * %d = %d",n,i,n*i);
i++;
}
return 0;
}
Write a C program to count number of digits in a number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,n;
printf("Enter n:");
scanf("%d",&n);
while(n!=0)
{
n=n/10;
i=i+1;
}
printf("%d",i);
return 0;
}
while(n>=10)
{
n=n/10;
}
printf(" first digit is = %d",n);
printf("\n last digit is = %d",l);
return 0;
}
int main()
{
int i=0,n,l;
printf("Enter n:");
scanf("%d",&n);
l=n%10;
while(n>=10)
{
n=n/10;
}
printf(" first digit is = %d",n);
printf("\n last digit is = %d",l);
printf("\n Sum of the first and last digit of a number is :%d",l+n);
return 0;
}
int main()
{
int i=0,n,l;
printf("Enter n:");
scanf("%d",&n);
while(n>0)
{
i=n%10;
l=l+i;
n=n/10;
}
printf(" Sum of digits = %d",l);
return 0;
}
int main()
{
int i=1,n,l=1;
printf("Enter n:");
scanf("%d",&n);
while(n>1)
{
i=n%10;
l=l*i;
n=n/10;
}
printf(" Sum of digits = %d",l);
return 0;
}
int main()
{
int rem,n,rev=0;
printf("Enter n:");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf(" Reverse of digits = %d",rev);
return 0;
}
int main()
{
int no,rem,rev,original;
printf("Enter the number:\n");
scanf("%d",&no);
original=no;
while(no!=0)
{
rem=no%10;
rev=10*rev+rem;
no=no/10;
}
if(rev==original)
{
printf("The no is a palindrome number");
}
else
printf("The number is not a palindrome number");
return 0;
}
int main()
{
int no,rem,rev,rem1;
printf("Enter the number:\n");
scanf("%d",&no);
while(no!=0)
{
rem=no%10;
rev=10*rev+rem;
no=no/10;
}
while(rev!=0)
{
rem1=rev%10;
switch(rem1)
{
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;
}
rev=rev/10;
};
return 0;
}
int main()
{
int no,pow=1,i,value=1;
printf("Enter the number:\n");
scanf("%d",&no);
printf("Enter the power:\n");
scanf("%d",&pow);
for(i=1;i<=pow;i++)
{
value=value*no;
}
printf("Final value is = %d",value);
return 0;
}
int main()
{
int no,value=1,i;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
if(no%i==0)
{
printf("\n%d is a factor of %d",i,no);
}
}
return 0;
}
Write a C program to calculate factorial of a number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int no,value=1,i;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=no;i>=1;i--)
{
value=value*i;
}
printf("Factorial is = %d",value);
return 0;
}
int main()
{
int no1,no2,min,i,hcf;
printf("Enter the numbers:\n");
scanf("%d %d",&no1,&no2);
min=(no1<no2)?no1:no2;
for(i=1;i<=min;i++)
{
if( no1%i==0 && no2%i==0 )
hcf=i;
}
printf("The HCF of numbers %d and %d is %d",no1,no2,hcf);
return 0;
}
int main()
{
int no1,no2,min,i,hcf,lcm;
printf("Enter the numbers:\n");
scanf("%d %d",&no1,&no2);
min=(no1<no2)?no1:no2;
for(i=1;i<=min;i++)
{
if( no1%i==0 && no2%i==0 )
hcf=i;
}
lcm=(no1*no2)/hcf;
printf("The LCM of numbers %d and %d is %d",no1,no2,lcm);
printf("The HCF of numbers %d and %d is %d",no1,no2,hcf);
return 0;
}
int main()
{
int no,i,c=0;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
if( no%i==0 )
c++;
}
if(c>2)
{
printf("%d is a composite number",no);
}
else
printf("%d is a prime number",no);
return 0;
}
Write a C program to find all prime numbers between given interval using functions.
Write a C program to print all strong numbers between given interval using
functions.
Write a C program to print all Armstrong numbers between given interval using
functions.
Write a C program to print all perfect numbers between given interval using
functions.
ARRAY EXERCISES
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,arr[5];
int main()
{
int i,arr[10],b[10],no,j=0;
printf("Enter the values in an array:");
for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
if(arr[i]<0)
{
b[j]=arr[i];
j++;
}
}
no=j;
for(i=0;i<j;i++)
{
printf("\n %d",b[i]);
}
return 0;
}
int main()
{
int i,arr[10],sum=0;
int main()
{
int i,arr[10],sum=0,max=0,min=0;
return 0;
}
int main()
{
int i,arr[10],sum=0,max=0,seclarge=0;
return 0;
}
Write a C program to count total number of even and odd elements in an array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,arr[10],count=0,count1=0;
return 0;
}
int main()
{
int i,arr[10],count=0,count1=0;
}
printf("Total number of negative numbers are:%d",count);
return 0;
}
int main()
{
int i=0,j=0,arr[10],array[10];
array[j]=arr[i];
j++;
}
for(i=0;i<j;i++)
{
printf("\n%d",array[i]);
}
return 0;
}
void main()
{
int a[100],i,n,pos;
printf("\nEnter no of elements\n");
scanf("%d",&n);
MATRICES EXERCISES
STRING EXERCISE
Write a C program to trim leading white space characters from given string.
Write a C program to trim trailing white space characters from given string.
Write a C program to trim both leading and trailing white space characters from
given string.
Write a C program to remove all extra blank spaces from given string.
POINTERS EXERCISE
Write a C program to create a file and write contents, save and close the file.
Write a C program to read file contents and display on console.
Write a C program to read numbers from a file and write even, odd and prime numbers
to separate file.
Write a C program to append content to a file.
Write a C program to compare two files.
Write a C program to copy contents from one file to another file.
Write a C program to merge two file to third file.
Write a C program to count characters, words and lines in a text file.
Write a C program to remove a word from text file.
Write a C program to remove specific line from a text file.
Write a C program to remove empty lines from a text file.
Write a C program to find occurrence of a word in a text file.
Write a C program to count occurrences of a word in a text file.
Write a C program to count occurrences of all words in a text file.
Write a C program to find and replace a word in a text file.
Write a C program to replace specific line in a text file.
Write a C program to print source code of same program.
Write a C program to convert uppercase to lowercase character and vice versa in a
text file.
Write a C program to find properties of a file using stat() function.
Write a C program to check if a file or directory exists.
Write a C program to rename a file using rename() function.
Write a C program to list all files and sub-directories recursively.
patterns
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=row;col++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Enter the number of rows:10
*
**
***
****
*****
******
*******
********
*********
**********
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=(n+1)-row;col++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Enter the number of rows:10
**********
*********
********
*******
******
*****
****
***
**
*
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=0;row<=n;row++)
{
for(col=0;col<=n;col++)
{
if(col<=((n-row)-1))
printf(" ");
else
printf(" *");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=0;row<=n;row++)
{
for(col=0;col<=n;col++)
{
if(col<=((n-row)-1))
printf(" ");
else
printf("*");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=0;row<=n;row++)
{
for(col=0;col<=n;col++)
{
if(col<=((n-row)-1))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=row;col++)
{
printf("%d ",col);
}
printf("\n");
}
return 0;
}
Enter the number of rows:10
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n,space;
printf("Enter the number of rows:");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(col=1;col<=(n+1)-row;col++)
{
printf("%d ",col);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int height,space,i,j,k;
printf("Enter the height of the triangle: ");
scanf("%d",&height);
space=height;
for( i=1;i<=height;i++)
{
for( j=1;j<=space-1;j++)
{
printf(" ");
}
for( k=1;k<=2*i-1;k++)
{
printf("*");
}
space--;
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int height,space,i,j,k,m=0;
printf("Enter the height of the triangle: ");
scanf("%d",&height);
space=height;
for( i=1;i<=height;i++)
{
for( j=1;j<i;j++)
{
printf(" ");
}
for( k=1;k<=(2*height-1)-2*m;k++)
{
printf("*");
}
m++;
printf("\n");
}
return 0;
}