0% found this document useful (0 votes)
7 views35 pages

C Programming Basics and Examples

The document is a course outline for CSE311: Introduction to Computer Language, authored by Md. Monir Hossain. It includes a list of recommended textbooks and online resources, as well as numerous C programming examples covering various topics such as number systems, area calculations, string manipulations, and control structures. Each example is presented with code snippets for practical implementation.

Uploaded by

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

C Programming Basics and Examples

The document is a course outline for CSE311: Introduction to Computer Language, authored by Md. Monir Hossain. It includes a list of recommended textbooks and online resources, as well as numerous C programming examples covering various topics such as number systems, area calculations, string manipulations, and control structures. Each example is presented with code snippets for practical implementation.

Uploaded by

Helen Helen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSE311: Introduction to

Computer Language

Md. Monir Hossain


Lecturer
Department of Computer Science & Engineering
E-mail: [Link]@[Link]
16 sept, 2023
Recommended Textbook & Resources

1. Programming With C, Byron S Gottfried(Second Edition)

2. Tutorialspoint
Link: [Link]

3. Javapoint
Link: [Link]

4. Programiz
Link: [Link]
C program for practice
1. Number System Conversion(Decimal to Octal, hexadecimal
and vice versa)

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
printf("%o",n);
getch();
}
2. C program to find area of a triangle using Heron’s formula

#include<stdio.h>
int main()
{
int a,b,c;
float Area,s;
printf("Enter sides of a triangle\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("%.2f",Area);
getch();
}
3. Find Area of Triangle using Formula

#include <stdio.h>
void main()
{
float base,height;
printf("Enter Base and Height: ");
scanf("%f %f",&base,&height);
float area = (base * height) / 2;
printf("Area of Triangle is %0.2f",area);
}
4. C Program to Find ASCII Value of a Character

#include<stdio.h>
int main()
{
char i;
scanf("%c",&i);
printf("Ascii character= %d",i);
getch();
}
5. C Program to Check Whether a Character is a Vowel
or Consonant
#include<stdio.h>
int main()
{
char n;
scanf("%c",&n);
if(n=='A' || n=='E' || n=='I' || n=='O' || n=='U' || n=='a' || n=='e' || n=='i' ||
n=='o' || n=='u')
printf("Vowel");
else
printf("Consonant");
}
6. C program to calculate GPA
#include<stdio.h>
int main()
{
int marks;
printf("Enter Your mark: ");
scanf("%d",&marks);
if(marks>=80 && marks<=100)
printf("He got A Plus");
else if(marks>=70 && marks<80)
printf(" He got A");
else if(marks>=60 && marks<70)
printf(" He got B");
else if(marks>=50 && marks<60)
printf(" He got C");
else
printf("Failed!");
getch();
}
7. Program to print the Floyd's triangle

#include<stdio.h>
int main()
{
int r,c,count=0,n;
scanf("%d",&n);
for(r=1;r<=n;r++)
{
for(c=1;c<=r;c++)
printf("%d",++count);
printf("\n");
}
}
8. C program to Find the Largest Number Among Three Numbers

#include<stdio.h>
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("A is Largest");
else if(b>a && b>c)
printf("B is Largest ");
else
printf("C is Largest ");
getch();
}
9. C Program to Check Leap Year

#include <stdio.h>
int main() {
int year;
year = 2016;
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;
}
10. C Program to Display Fibonacci Sequence
#include<stdio.h>
int main()
{
int first=0,second=1,fibo,n,i,sum=0;
scanf("%d",&n);
for(i=0; i<n; i++)
{
if(i<=1)
fibo=i;
else
{ fibo=first+second;
first=second;
second=fibo;
}
printf("%d ",fibo);
}
}
11. C Program to Display Fibonacci Sequence(Using Array)
#include<stdio.h>
int main()
{
int a[100],i,n;
printf("how many numbers?\n");
scanf("%d",&n);
a[0]=0;
a[1]=1;
for(i=2; i<n; i++)
{
a[i]=a[i-1]+a[i-2];
}
for(i=0; i<n; i++)
printf(" %d",a[i]);
getch();
}
12. C Program to Find Factorial of a Number

#include<stdio.h>
int main()
{
int i,n,fact=1;
printf("Enter factorial number= ");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("The factorial value is=%d",fact);
getch();
}
13. Check Armstrong number
#include<stdio.h>
int main() {
int num,sum=0,temp,r;
scanf("%d",&num);
temp=num;
while(temp!=0)
{ r=temp%10;
sum=sum+r*r*r;
temp=temp/10;
}
if(sum==num)
printf("Armstrong number");
else
printf("Not Armstrong number");
getch();
14. Infinite loop

#include <stdio.h>
int main ()
{
for( ; ; )
{
printf("This loop will run forever.\n");
}
return 0;
}
15. C Program To Find Maximum & Minimum Element In Array
#include<stdio.h>
int main() {
int n,a[100],i;
printf("how many numbers?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("Maximum Number is= %d",max);
getch();
}
16. C Program to Generate Multiplication Table
#include<stdio.h>
int main()
{
int i,n,r=0;
printf("Enter your value= ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
r=i*n;
printf("%d*%d = %d\n",n,i,r);
}
getch();
}
17. Perfect Number Program in C
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("Enter any number: ");
scanf("%d",&n);
for(i=1; i<n; i++)
{
if(n%i==0)
sum=sum+i;
}
if(n==sum)
printf("perfect number");
else
printf("not perfect number");
getch();
}
18. C Program to Check Whether a Number is Palindrome or Not
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
scanf("%d",&num);
temp=num;
while(temp!=0)
{
r=temp%10;
sum=sum*10+r;
temp=temp/10;
}
if(num==sum)
printf("palindrome");
else
printf("not palindrome");
getch();
}
19. Prime Number program in C
#include<stdio.h>
int main() {
int i,count=0,n;
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
count++;
break;
}
}
if(count==0)
printf("Prime number");
else
printf("not prime number");
getch();
}
20. C Program to Print Pyramids and Patterns
#include<stdio.h>
int main(){
int r,c,n;
printf("Enter your number:");
scanf("%d",&n);
for(r=1; r<=n; r++) //how many row print & upper part
{ for(c=1; c<=n-r; c++) //space print
printf(" ");
for(c=1; c<=r; c++) //* print
printf("* ");
printf("\n");}
for(r=n-1; r>=1; r--) //how many row print and lower part
{ for(c=1; c<=n-r; c++) //space print
printf(" ");
for(c=1; c<=r; c++) //* print
printf("* ");
printf("\n"); }
}
21. C Program to Check if a Given String is Palindrome
#include<stdio.h>
int main() {
char str1[100];
char str2[100];
int i=0,j,len=0;
printf("Enter your String: ");
gets(str1);
while(str1[i]!='\0')
{ i++;
len++;
}
for(j=0,i=len-1; i>=0; i--,j++)
{
str2[j]=str1[i];
}
str2[j]='\0';
puts(str1);
puts(str2);
int d=strcmp(str1,str2);
if(d==0)
printf("Palindrome");
else
printf("Not palindrome");
getch(); }
22. C Program to Find GCD and LCM of Two Integers
#include<stdio.h>
int main()
{
int num1,num2,n1,n2,rem,gcd,lcm;
scanf("%d%d",&num1,&num2);
n1=num1;
n2=num2;
while(n2!=0)
{
rem=n1%n2;
n1=n2;
n2=rem;
}
gcd=n1;
lcm=(num1*num2)/gcd;
printf("GCD=%d\n",gcd);
printf("LCM=%d",lcm);
getch();
}
23. C Program to Find the Length of a String

#include<stdio.h>
int main()
{
char s[100];
gets(s);
int len=strlen(s);
printf("Length= %d",len);
getch();
}
24. C Program to Swap two Numbers
#include<stdio.h>
int main()
{
int num1,num2;
printf("Enter num1 and num2: ");
scanf("%d%d",&num1,&num2);
num1=num1-num2;
num2=num1+num2;
num1=num2-num1;
printf("After Swapping numbers=%d%d\n",num1,num2);
getch();
}
25. C program to reverse a string
#include<stdio.h>
int main() {
char str1[100];
printf("Enter your String: ");
gets(str1);
char str2[100];
int i=0,j,len=0;
while(str1[i]!='\0')
{
i++;
len++;
}
for(j=0,i=len-1; i>=0; i--,j++)
{
str2[j]=str1[i];
}
str2[j]='\0';
printf("str1= %s\n",str1);
printf("str2= %s\n",str2);
getch();
}
26. Pattern generating
#include<stdio.h>
int main ()
{ int a,b;
for( a = 1; a < 6; a++ )
{
for ( b = 1; b <= a; b++ )
{
printf("%d",b);
}
printf("\n");
}
return 0;
}
27. Factorial using recursion

#include<stdio.h>
int main()
{
int i;
printf("Enter your value: ");
scanf("%d",&i);
int result=fact(i);
printf("The result is=
%d",result); getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}
28. Diamond Pattern
#include<stdio.h>
int main()
{
int r,c,n;
printf("Enter your number:");
scanf("%d",&n);
for(r=1; r<=n; r++) //how many row
print & upper part { for(c=1; c<=n-r;
c++) //space print
printf(" ");
for(c=1; c<=r; c++) //* print
printf("* ");
printf("\n");}
for(r=n-1; r>=1; r--) //how many row
print and lower part { for(c=1; c<=n-r;
c++) //space print
printf(" ");
for(c=1; c<=r; c++) //* print
printf("* ");
printf("\n"); }
}
29. Write a C program to compute the sum of the first 10
natural numbers.
#include <stdio.h>
int main()
{
int j, sum = 0;

printf("The first 10 natural number is :\n");

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


{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}
30. Accept digit and display in the word(Continued…)
#include <stdio.h>
void main()
{
int cdigit;

printf("Input Digit(0-9) : ");


scanf("%d",&cdigit);
switch(cdigit)
{
case 0:
printf("Zero\n");
break;

case 1:
printf("one\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("invalid digit. \nPlease try again ....\n");
break;
}
}

Common questions

Powered by AI

To check if a number is a palindrome in C, you reverse the number mathematically and compare it to the original. For strings, the process involves reversing the string using an auxiliary buffer and comparing it to the original using functions like `strcmp`. The main difference lies in handling digit extraction and string manipulation techniques .

Generating a diamond pattern of stars in C requires nested loops to manage the spaces and stars for both the upper and lower halves of the diamond. For the upper half, the outer loop manages the rows, and inner loops handle spaces and stars. The roles are reversed for the lower half, to decrement the number of stars per row symmetrically after reaching the midpoint .

In C, recursion for factorial calculation involves a function calling itself with decremented values until it reaches the base case. The recursive function `fact(int n)` checks if `n` is 1, returning 1 for the base case. Otherwise, it returns `n * fact(n-1)`, effectively multiplying the current number by the factorial of the number one less than it .

Floyd's Triangle is implemented using two nested loops. The outer loop iterates over the rows, while the inner loop iterates over the columns, incrementing and printing a counter that starts at 1. The significance of Floyd’s Triangle lies in its simple representation and in teaching nested loop control, useful for understanding iterative processes .

An Armstrong number is a number that is equal to the sum of cubes of its digits. In C, the program loops through each digit of the number, computes its cubic value, and accumulates these values. After looping through all digits, it compares the accumulated sum with the original number to determine if it is an Armstrong number .

A year is a leap year in a C program if it is divisible by 4 and not divisible by 100, or it is divisible by 400. This can be expressed with the logical condition: `((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)` .

To determine if a character is a vowel in C, you can use an `if` statement checking if the character is 'a', 'e', 'i', 'o', 'u' (in both lowercase and uppercase). If the character matches any of these, it's a vowel; otherwise, it's a consonant .

The Euclidean algorithm computes the GCD by repeatedly setting the larger number to the remainder of the larger number divided by the smaller number, swapping the values until the remainder is zero. The last non-zero remainder is the GCD. This is implemented by iterating the process until the remainder becomes zero .

To reverse a string and check if it's a palindrome, a C program uses a loop to fill a reversed version of the string into another array. The program then compares the reversed string with the original using `strcmp`. If they match, the string is a palindrome .

Heron's formula calculates the area of a triangle using its three side lengths. The semi-perimeter `s` is calculated as `(a + b + c) / 2`. The area is then computed with `sqrt(s * (s - a) * (s - b) * (s - c))`, where `a`, `b`, and `c` are the lengths of the triangle's sides. This formula is implemented directly in a C program using the `sqrt()` function for computation .

You might also like